I have a ARGB value stored as an int type. It was stored by calling ToArgb.
I now want the byte values of the individual color channels from the int value.
for example
int mycolor = -16744448;
byte r,g,b,a;
GetBytesFromColor(mycolor,out a, out r, out g, out b);
How would you implement GetBytesFromColor?
To give the context I am passing a color value persisted in db as int to a silverlight application which needs the individual byte values to construct a color object.
System.Windows.Media.Color.FromArgb(byte a, byte r, byte g, byte b)
public void GetBytesFromColor(int color, out a, out r, out g, out b)
{
Color c = Color.FromArgb(color);
a = c.A;
r = c.R;
g = c.G;
b = c.B;
}
You are after the 4 successive 8-bit chunks from a 32-bit integer; so a combination of masking and shifting:
b = (byte)(myColor & 0xFF);
g = (byte)((myColor >> 8) & 0xFF);
r = (byte)((myColor >> 16) & 0xFF);
a = (byte)((myColor >> 24) & 0xFF);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With