Friday, October 21, 2011

Computing RGB composite Color Value in C#

If you still have to deal with the Win32Api in C#, you may need to get the old-style RGB composite color values from a .NET Color value. The ToArgb method doesn't work like you want, so it has to be computed:

  private int ConvertColorToRGB(Color col)
    {
    return (col.R) | (col.G << 8) | (col.B << 16);
    }
 
If you are using VB.NET, it still has an RGB function to call or:

   Public Function ConvertColorToRGB(col As Color) As Integer

        Return CInt(col.R) Or (CInt(col.G) << 8) Or (CInt(col.B) << 16)

    End Function