Converts a scalar normalized to de range [0:1] into a short rainbow of rgba values. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Converts a scalar normalized to de range [0:1] into a short rainbow of rgba values.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float value = 2.45678f;
        float[] rgba = new float[] { 34.45f, 35.45f, 36.67f, 37.78f,
                37.0000f, 37.1234f, 67.2344f, 68.34534f, 69.87700f };
        short_rainbow(value, rgba);
    }//from w ww .j av  a 2s . c om

    /**
     * Converts a scalar normalized to de range [0:1] into
     * a short rainbow of rgba values. 
     * See: http://www.particleincell.com/blog/2014/colormap/
     * @param value
     */
    public static void short_rainbow(float value, float[] rgba) {
        /*plot short rainbow RGB*/
        float a = (1 - value) / 0.25f; //invert and group
        final int X = (int) Math.floor(a); //this is the integer part
        float Y = (a - X); //fractional part from 0 to 1

        rgba[3] = 1;

        switch (X) {
        case 0:
            rgba[0] = 1;
            rgba[1] = Y;
            rgba[2] = 0;
            break;
        case 1:
            rgba[0] = 1 - Y;
            rgba[1] = 1;
            rgba[2] = 0;
            break;
        case 2:
            rgba[0] = 0;
            rgba[1] = 1;
            rgba[2] = Y;
            break;
        case 3:
            rgba[0] = 0;
            rgba[1] = 1 - Y;
            rgba[2] = 1;
            break;
        case 4:
            rgba[0] = 0;
            rgba[1] = 0;
            rgba[2] = 1;
            break;
        }
    }
}

Related Tutorials