我寫了一種方法將三種顏色的漸變應用於圖像。 顏色是藍色 - >黃色 - >紅色。 我的顏色從黃色到紅色平滑過渡,但如果亮度低於0.5,從藍色→黃色的過渡是從藍色到綠色到黃色的顛簸,所以它不是光滑的。 我想對圖像具有類似熱影像的效果。在圖像上應用色彩插值
我不知道我在做什麼錯,我寫了下面的方法。 我計算應用於顏色模板的原始像素的亮度。
public void applyGradient()
{
size = width * height;
float r, g, b;
float redTemp, yellowTemp, blueTemp;
float luminance = 0;
float result;
index = 0;
redTemp = 0xff000000 | (255 << 16) | (0 << 8) | 0;
yellowTemp = 0xff000000 | (255 << 16) | (255 << 8) | 0;
blueTemp = 0xff000000 | (0 << 16) | (0 << 8) | 255;
while(index < size)
{
r = (rgbInput[index]& 0x00FF0000) >> 16;
g = (rgbInput[index]& 0x0000FF00) >> 8;
b = (rgbInput[index]& 0x000000FF);
luminance = ((r+b+g)/3)/255;
if (luminance < 0.5)
{
result = (float) ((yellowTemp * luminance * 2.0) + blueTemp * (0.5 - luminance) * 2.0);
}
else
{
result = (float) (redTemp * (luminance - 0.5) * 2.0 + yellowTemp * (1.0 - luminance) * 2.0);
}
output[index] = (int)result;
index++;
}
}
我發現,如果我使所有變量浮動,然後我得到良好的黃色紅色分佈,如果亮度> 0.5。但如果它在下面,它需要從藍色變成黃色,我會從藍色變成綠色變黃。我如何順利地從藍色變爲黃色:s。從黃色到紅色似乎不成問題。 – LordrAider 2012-01-11 11:39:57
從黃色到紅色的計算與從藍色到黃色的計算不同。嘗試:結果=(int)((yellowTemp *(1.0-亮度)* 2.0)+ blueTemp *(0.5-亮度)* 2.0);或者其中的一些變化。 – 2012-01-11 21:43:20
也無法正常工作:(當從黃色變爲紅色,從藍色變爲黃色時,必須有不同的東西:)。 – LordrAider 2012-01-12 09:26:08