Color to uint and back

vckicks 0 Tallied Votes 2K Views Share

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);
}
subtercosm 0 Light Poster

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.