How to convert Integer values ​​to Red, Green, and Blue

I am converting the RGB value to one with the following:

public static int RGBtoInt(int red, int greed, int blue)
{
    return blue + green + (green * 255) + (red * 65536);
}

but struggles to write a reverse method that takes an integer and returns the individual RGB components.

Something of a thematic nature:

public static Vector3 IntToRgb(int value)
{
    // calculations...
    return new Vector3(red, green, blue); 
}

The method Color.FromArgb(int)does not create the RGB color that I need.

The function RGBtoIntabove matches the integer RGB values ​​returned by OpenGL, and I'm looking for the opposite method. In the same conversion method here .

+4
source share
2 answers

The conversion can be performed 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); 
}

As I understand it, 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 );
}
+8

Color c = Color.FromArgb(someInt); c.R, c.G c.B ,

+2

All Articles