使用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看到我更新的答案,這應該適合你。 –