开发者

Map double value to Color

开发者 https://www.devze.com 2023-02-21 19:12 出处:网络
I would like to do a map of values to color.For exemple, value from 0 to 25 will represent a variation of blue, from the lighter to the darker.

I would like to do a map of values to color. For exemple, value from 0 to 25 will represent a variation of blue, from the lighter to the darker. I have never done it, so i would like so开发者_StackOverflow中文版me advice and explanation on the basics. Thanks for help.


The first step is to choose your colors for fixed points, such as white for 0 and blue for 25 as you suggest. You could then add green for 50, red for 75 and so on.

Each fixed point maps to an R G B value. If we use floats between 0 and 1 the math is easier:

Value   Red   Green   Blue
  0     0.33   0.33   0.34
 25     0.00   0.00   1.00
 50     0.00   1.00   0.00

If you keep the R+G+B constant like this, the intensity of the color will be constant (see: http://en.wikipedia.org/wiki/HSL_and_HSV )

Now you establish which two fixed colors you are between. You get RGB values for the low end of the range and for the high end. Lets work with a value of 15 as an example. 15 is between 0 and 25, so:

rLow = 0.33f; gLow = 0.33f; bLow = 0.34f;
rHigh = 0.0f; gHigh = 0.0f; bHigh = 0.0f;

float prop = ( x - vLow) / ( vHigh - vLow );
[e.g.      = ( 15 - 0 ) / ( 25 - 0 ) = 0.6 ]

Then for each color you do:

float rVal = rLow + prop * ( rHigh - rLow );
[e.g.      = 0.33 + 0.6 * ( 0.00 - 0.33 ) = 0.132 ]

Similarly you interpret for green and blue:

float gVal = gLow + prop * ( gHigh - gLow );
float bVal = bLow + prop * ( bHigh - bLow );

The blue example looks like:

[e.g.      = 0.34 + 0.6 * ( 1.00 - 0.34 ) = 0.736 ]

You can then create the Java Color object like this:

Color col = new Color(rVal,gVal,bVal);

and use it how you wish. :-)


Here is my solution on gisthub for double to color mapping wiht a blue-red gradient.

result example: values 0 - 100 with stepcount 1

Map double value to Color


Well in c++, do something like

vector<Color> myvector;

for(int i = 0; i < 25; i++){
    myvector.push_bacK(Color(0,0,(int)(round(double(255/(i+1))));
}

and your vector will contain shades of blue...


If you are using RGB, the higher the value -> the lighter the color will be

(255,255,255) is white

(0,0,0) is black

Since 255 is the largest parameter, you can divide it by how many blue variations you want

Note: (R,G,B) are Integer parameters, I suggest finding a good denominator like 17 which gives exactly 15 different variations of blue only color

0

精彩评论

暂无评论...
验证码 换一张
取 消