0

I've written a class based on JPanel which displays an waterfall diagram, based on data I am getting from a FFT analysis of an audio signal.

The question is: How to determine the color to use?

My current function to do this looks like this:

/**
* 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);
} 

So my objective is to change this function in a way that the color depends on the currentValue. currentValues from low->high should corespond with colors black -> green -> yellow -> red. How to implement this in my function?

gorootde
  • 4,003
  • 4
  • 41
  • 83
  • black -> green -> yellow -> red. (should be blue instead of black, but doeasn't matter), this question in this form isn't answerable, because you can do that with all 1. J(Xxx)Chart/Graph(s), 2. with coordinates stored in array for paintComponent 3. or series of JProgressBars painted by JLayer/override ProgressBarUI – mKorbel Dec 02 '13 at 12:09
  • addendum - those ways (as I see) here are coded (for example) equalizers in Java – mKorbel Dec 02 '13 at 12:12
  • You can do that in those ways, correct. But you got the same problem How to determine the correct color your char / coordinate / progressbar has on a given point. – gorootde Dec 02 '13 at 12:22
  • But you got the same problem - I think that not, can be little bit complicated in the case that chart with max value contains all four Colors for paintComponent/JProgressBars, J(Xxx)Chart/Graph(s) can do that by default – mKorbel Dec 02 '13 at 12:25
  • 1
    Use a `List` and `Color.getHSBColor` for your color lookup table, for [example](http://stackoverflow.com/a/9875534/230513). – trashgod Dec 02 '13 at 12:26

0 Answers0