Converting RGB to Hexadecimal in C#:
Let's write a C# program to convert RGB values to hexadecimal as shown below,
using System;
class Program
{
static void Main(string[] args)
{
int red = 120;
int green = 200;
int blue = 255;
string hexColor = RGBToHex(red, green, blue);
Console.WriteLine("RGB: ({0}, {1}, {2}) converts to Hexadecimal: {3}", red, green, blue, hexColor);
}
static string RGBToHex(int red, int green, int blue)
{
return String.Format("#{0:X2}{1:X2}{2:X2}", red, green, blue);
}
}
Output:
RGB: (120, 200, 255) converts to Hexadecimal: #78C8FF
Comments (0)