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...
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));
Sounds like you're asking two questions here, let me take another stab at it. Regardless, you'll want to look into using the BitConverter
class.
From this page:
The byte-ordering of the 32-bit ARGB value is AARRGGBB.
So, to implement ToArgb()
, you could write an extension method that does something like this:
public static int ToArgb(this System.Windows.Media.Color color)
{
byte[] bytes = new byte[] { color.A, color.R, color.G, color.B };
return BitConverter.ToInt32(bytes, 0);
}
And to "decompose the Int returned by ToArgb() to use with FromArgb()", you could do this:
byte[] bytes = BitConverter.GetBytes(myColor.ToArgb());
byte aVal = bytes[0];
byte rVal = bytes[1];
byte gVal = bytes[2];
byte bVal = bytes[3];
Color myNewColor = Color.FromArgb(aVal, rVal, gVal, bVal);
Hope this helps.
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