2017-06-02 137 views
0

我有一个小问题,我试图绘制不同的颜色点,颜色必须取决于与最大范围和最小范围内的点相关的特定值。我希望颜色缩放可以在红色 - 黄色 - 绿色之间进行。特定范围的颜色映射

现在,我已经将红色与最大值相关联,而绿色与最小值相关联。对于所有其他中间点,我想从绿色变为黄色变为红色,问题我不知道如何设置所有变为黄色,因为现在我只能通过绿色和红色渐变。

代码:

red = (int) (value * (255)); 
green = 255 - red; 
blue = 0 ; 

Color color = new Color((int) red, (int) green, (int) blue); 
String hex = Integer.toHexString(color.getRGB() & 0xffffff); 

范围从1(MAX)变为0(MIN),因此该值内该范围[0,1]。

我该如何解决所有问题以获得黄色渐变?

回答

1

您可以尝试HSV色彩空间。通过改变色相组件,您可以按照自己想要的方式进行色彩转换。

float value = 0; //this is your value between 0 and 1 
float minHue = 120f/255; //corresponds to green 
float maxHue = 0; //corresponds to red 
float hue = value*maxHue + (1-value)*minHue; 
Color c = new Color(Color.HSBtoRGB(hue, 1, 0.5f)); 

这插补了最小和最大颜色的色调。如果您想在不具有相同值和饱和度的颜色之间插值。在这些之间进行插值。

+0

我实际上有点,其中每一个与0和1之间的值相关联。当一个点的值为1,那么我必须赎回它,如果它有0我必须为我着色t绿色,在所有其他情况下,我必须在它们之间进行一次缩放,例如,如果值为0.5,则必须将其变为黄色。 – traveller

+0

我希望我能更好地解释它。 – traveller

+0

@traveller看到我更新的答案,这应该适合你。 –

1

使用HSB模型,而不是RGB。在HSB中,您只需指定所需颜色的角度即可。 Color#getHSBColor(float h, float s, float b);Here您可以尝试混合HSB(HSV)颜色。

HSB Model

下面是一个例子如何在指定的颜色间隔创建n个不同的颜色:

public static Color[] intervalColors(float angleFrom, float angleTo, int n) { 
    float angleRange = angleTo - angleFrom; 
    float stepAngle = angleRange/n; 

    Color[] colors = new Color[n]; 
    for (int i = 0; i < n; i++) { 
     float angle = angleFrom + i*stepAngle; 
     colors[i] = Color.getHSBColor(angle, 1.0, 1.0);   
    } 
    return colors; 
} 
... 
    Color[] colors = intervalColors(0, 120, 10); // red to green 
    Arrays.sort(colors, Comparator.reversed()); // green to red 

要映射的色彩从范围< 0,1>为值:

/** 
* Remaps x from old-interval to new-interval. 
* DoubleInterval just wraps double values a, b. 
*/ 
public static double remap(double x, DoubleInterval oldDomain, DoubleInterval newDomain) { 
    double oldRange = oldDomain.size(); // oldDomain.b - oldDomain.a 
    double oldRangeValue = x - oldDomain.a; // lowerBound a is just an offset 
    double percentile = oldRangeValue/oldRange; 

    double newRange = newDomain.size(); // newDomain.b - newDomain.a 
    double newRangeValue = percentile * newRange; 
    double newVal = newRangeValue + newDomain.a; 
    return newVal; 
} 

/** 
* Returns color from specified color-interval that matches normValue <0,1>. 
* If normValue = 0, angleFrom = 0 (red), angleTo = 120 (green) then color = red. 
*/ 
public static Color intervalColor(float normValue, float angleFrom, float angleTo) { 
    double angleValue = remap(normValue, new DoubleInterval(0, 1), new DoubleInterval(angleFrom, angleTo)); 
    return Color.getHSBColor(angleValue, 1.0, 1.0);   
} 

/** 
* Returns inversion of specified value in given domain. 
* Example: if x = 0.3 and domain of x is <0,1> then inversion = 0.7 
*/ 
public static double invert(double x, DoubleInterval domain) { 
    return (domain.b - x) + domain.a; 
} 

/** 
* Returns color from specified color-interval that matches inverted normValue <0,1>. 
* If normValue = 0 and angleFrom = 0 (red) and angleTo = 120 (green) then color = green. 
*/ 
public static Color invertedIntervalColor(float normValue, float angleFrom, float angleTo) { 
    double invNormValue = invert(normValue, new DoubleInterval(0, 1)); 
    return intervalColor(invNormValue, angleFrom, angleTo);  
}