I'm converting an RGB value to a single integer with the following:
public static int RGBtoInt(int red, int greed, int blue)
{
return blue + green + (green * 255) + (red * 65536);
}
but struggling to write an inverse method taking in an integer and returning the single RGB components.
Something of thematic nature with:
public static Vector3 IntToRgb(int value)
{
// calculations...
return new Vector3(red, green, blue);
}
The Color.FromArgb(int)
method isn't creating the RGB colour I need.
The RGBtoInt
function above matches the RGB integer values returned by OpenGL and I am looking for a reverse method. It's the same conversion method used here.
The conversion can be done as follows.
public static Vector3 IntToRgb(int value)
{
var red = ( value >> 0 ) & 255;
var green = ( value >> 8 ) & 255;
var blue = ( value >> 16 ) & 255;
return new Vector3(red, green, blue);
}
To my understanding, the initial conversion should be done as follows.
public static int RGBtoInt(int r, int g, int b)
{
return ( r << 0 ) | ( g << 8 ) | ( b << 16 );
}
Try this Color c = Color.FromArgb(someInt);
and then use c.R
, c.G
and c.B
for Red, Green and Blue values respectively
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With