2017-06-02 138 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);  
}