Implementing ToArgb()

2019-04-05 06:06发布

问题:

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...

回答1:

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));


回答2:

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.



回答3:

Just a short note:

Retrieving the integer representation of a "Color" object seems to be 4 times faster for me when calling color.ToArgb() directly instead of using the bitshift-operations in the marked answer. So if you have access to .ToArgb(), always use that!

The thread creator stated he hasn't access to it, so of course the marked answer is still correct, juse don't get confused by the claimed "fast operations" in it.

Just keep in mind: The color of the Color instance is internally already saved as int value, so ToArgb() is just returning it, while accessing each single byte of it to (by properties .A, .R., .G., .B) and then re-ensemble them by bitshifting is kinda moving in a circle.