It is necessary that the numbers from 0 to 255 correspond to such a color map enter image description here

and the result is three numbers of RGB, for example (0,0,255)

    2 answers 2

    This illustration from Wikipedia will help you ( link )

    Illustration showing the relationship between RGB and HSV

    Your example starts at 240 ° and goes left to 0 °.

    You need to divide the entire width into 4 areas.

    • 1 quarter: Blue 100%, Green grows from 0% to 100%
    • 2 quarter: Green 100%, Blue decreases from 100% to 0%
    • 3 quarter: Green 100%, Red grows from 0% to 100%
    • 4th quarter: Red 100%, Green reduced from 100% to 0%

    I think with the casting arithmetic you can handle

      A little work on your picture. enter image description here

      If we consider, it is clear that

      • blue color goes from 0 to 127 with intensity from 255 to 0.
      • green color goes from 0 to 127 with intensity from 0 to 255 and from 128 to 255 with intensity from 255 to 0
      • red color from 128 to 255 with intensity from 0 to 255 Based on this, we write the code:
      struct Color { public Color(int r, int g, int b) { R = r; G = g; B = b;} int R,G,B; } Color getColor(int color) { int red = (color >= 128) ? (color - 128)*2 : 0; int green = (color < 128) ? color*2 : 255 - (color - 128)*2; int blue = (color < 128) ? 255 - 2*color : 0; return new Color(red,green,blue); } 

      Pass a number from 0 to 255 to the function, we get the color in RGB.

      PS Possibly poorly handled boundary values ​​(0, 127 or 128 and 255).