java - 特定范围的颜色映射

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

我怎样才能修正一切以获得黄色渐变?

最佳答案

使用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);      
}

关于java - 特定范围的颜色映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44326765/

相关文章:

java - 变化扩展了 Activity 类别

css - "Color Scheme"和 "Color Palette"有区别吗?

jquery - jqgrid设置单元格背景颜色

dart - 以编程方式使 dart 中的十六进制颜色变亮或变暗

IPython日晒?

java - POSTGRES 函数执行几次后需要很长时间

java - 如何使用processing.org计算文本位置x?

java - 如何使用 Android DataBinding 动态更改 android :layout property?

java - Android默认白色背景十六进制代码

python - 根据两个数据框值对绘图进行颜色编码