我编写了一个基于 JPanel 的类,它显示了一个瀑布图,基于我从音频信号的 FFT 分析中获得的数据。
问题是:如何确定要使用的颜色?
我当前执行此操作的功能如下所示:
/**
* Returns the color for the given FFT result value.
* @param currentValue One data entry of the FFT result
* @return The color in which the pixel should be drawn
*/
public Color calcColor(float currentValue){
float max_color_value=255+255+255;//r+g+b max value
//scale this to our MAX_VALUE
float scaled_max_color_val=(max_color_value/MAX_VALUE)*currentValue;
//split into r g b parts
int r=0;
int g=0;
int b=0;
if(scaled_max_color_val < 255) {
b=(int)Math.max(0, scaled_max_color_val);
} else if(scaled_max_color_val > 255 && scaled_max_color_val < 255*2){
g=(int)Math.max(0, scaled_max_color_val-255);
} else if(scaled_max_color_val > 255*2 ){
r=(int)Math.max(0, scaled_max_color_val-255*2);
r=Math.min(r, 255);
}
return new Color(r, g, b);
}
所以我的目标是以颜色取决于currentValue
. currentValue
s 从低-> 高应该对应颜色黑色-> 绿色-> 黄色-> 红色。如何在我的函数中实现这一点?