Color to uint and back

Please support our C# advertiser: Intel Parallel Studio Home
vckicks vckicks is offline Offline Dec 3rd, 2008, 3:27 pm |
0
Convert a color to a uint representation and back with simple byte shifting.
Quick reply to this message  
C# Syntax
  1. private uint ColorToUInt(Color color)
  2. {
  3. return (uint)((color.A << 24) | (color.R << 16) |
  4. (color.G << 8) | (color.B << 0));
  5. }
  6.  
  7. private Color UIntToColor(uint color)
  8. {
  9. byte a = (byte)(color >> 24);
  10. byte r = (byte)(color >> 16);
  11. byte g = (byte)(color >> 8);
  12. byte b = (byte)(color >> 0);
  13. return Color.FromArgb(a, r, g, b);
  14. }
0
subtercosm subtercosm is offline Offline | Feb 7th, 2009
I like the part where you shift by 0 for consistency.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC