954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Color to uint and back

0
By vckicks on Dec 4th, 2008 1:27 am

Convert a color to a uint representation and back with simple byte shifting.

private uint ColorToUInt(Color color)
{
      return (uint)((color.A << 24) | (color.R << 16) |
                    (color.G << 8)  | (color.B << 0));
}

private Color UIntToColor(uint color)
{
     byte a = (byte)(color >> 24);
     byte r = (byte)(color >> 16);
     byte g = (byte)(color >> 8);
     byte b = (byte)(color >> 0);
     return Color.FromArgb(a, r, g, b);
}

I like the part where you shift by 0 for consistency.

subtercosm
Light Poster
34 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You