views:

234

answers:

2

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)
+3  A: 

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);
Marc Gravell
+1  A: 
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;
}
bassfriend
-1. Silverlight does not have this overload of FromArgb.
AnthonyWJones