使用HSB模型,而不是RGB。在HSB中,您只需指定所需颜色的角度即可。 Color#getHSBColor(float h, float s, float b);
。 Here您可以尝试混合HSB(HSV)颜色。
下面是一个例子如何在指定的颜色间隔创建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);
}
我实际上有点,其中每一个与0和1之间的值相关联。当一个点的值为1,那么我必须赎回它,如果它有0我必须为我着色t绿色,在所有其他情况下,我必须在它们之间进行一次缩放,例如,如果值为0.5,则必须将其变为黄色。 – traveller
我希望我能更好地解释它。 – traveller
@traveller看到我更新的答案,这应该适合你。 –