Implementing ToArgb()

Number8 picture Number8 · Apr 22, 2010 · Viewed 14.8k times · Source

System.Drawing.Color has a ToArgb() method to return the Int representation of the color.
In Silverlight, I think we have to use System.Windows.Media.Color. It has A, R, G, B members, but no method to return a single value.
How can I implement ToArgb()? In System.Drawing.Color, ToArgb() consists of

return (int) this.Value;  

System.Windows.Media.Color has a FromArgb(byte A, byte R, byte G, byte B) method. How do I decompose the Int returned by ToArgb() to use with FromArgb()?

Thanks for any pointers...

Answer

Rene Schulte picture Rene Schulte · Apr 22, 2010

Short and fast. Without an extra method call, but with fast operations.

// To integer
int iCol = (color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;

// From integer
Color color = Color.FromArgb((byte)(iCol >> 24), 
                             (byte)(iCol >> 16), 
                             (byte)(iCol >> 8), 
                             (byte)(iCol));