944,044 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 3728
  • C# RSS
0

Color to uint and back

by on Dec 3rd, 2008
Convert a color to a uint representation and back with simple byte shifting.
C# Code Snippet (Toggle Plain Text)
  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. }
Comments on this Code Snippet
Feb 7th, 2009
0

Re: Color to uint and back

I like the part where you shift by 0 for consistency.
Light Poster
subtercosm is offline Offline
32 posts
since Jan 2009
Message:
Previous Thread in C# Forum Timeline: Performing MergeSort on a singly linked list
Next Thread in C# Forum Timeline: Capture your screen in a bitmap.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC