In C#, how can I convert a Color
object into a byte
value?
For example, the color #FFF
would be converted to the value 255
Try this,
string colorcode = "#FFFFFF00";
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
also see this How to get Color from Hexadecimal color code using .NET?
You could use the ColorTranslator.FromHtml function:
Color color = ColorTranslator.FromHtml("#FFF");
You can get the byte values of a .NET Color
object with:
byte red = color.R;
byte green = color.G;
byte blue = color.B;
That gives you 3 bytes. I don't know how you expect to get a single byte value. Colors are (AFAIK) almost never represented by single bytes.
You can use ConvertFromString() method from ColorConverter class.
Attempts to convert a string to a Color.
Return Value
Type: System.Object
A Color that represents the converted text.
ColorConverter c = new ColorConverter();
Color color = (Color)c.ConvertFromString("#FFF");
Console.WriteLine(color.Name);