特定范围的颜色映射

时间:2017-06-02 10:31:36

标签: java colors color-scheme

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

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

代码:

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]内。

如何修复所有内容以获得黄色渐变?

2 个答案:

答案 0 :(得分:2)

您可以尝试HSV色彩空间。通过改变Hue组件,您可以按照自己的方式进行颜色偏移。

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));

这会插入最小和最大颜色的色调。如果要在不具有相同值和饱和度的颜色之间进行插值。在这些之间进行插值。

答案 1 :(得分:2)

使用 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

将颜色映射到范围&lt; 0,1&gt;的值:

/**
 * 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);      
}