简体   繁体   中英

Gradient color lookup in Java

I have a list of values from 0-1. I want to convert this list to an image by using a gradient that converts these floating point values to RGB values. Are there any tools in Java that provide you with this functionality?

0 should be mapped 0
1 should be mapped 255

keep in mind that you need 3 of them to make a color

so multiply by 255 the floating number and cast it to int.

Perhaps GradientPaint can do what you want. It's unclear how you want a list of floating point values to be converted into a gradient. Normally a gradient consists of two colors and some mechanism that interpolates between those colors. GradientPaint implements a linear gradient.

Say you have an array made of 64 000 triples corresponding to RGB values, like this:

    final Random rand = new Random();
    final float[] f = new float[320*200*3];
    for (int i = 0; i < f.length; i++) {
        f[i] = rand.nextFloat(); // <-- generates a float between [0...1.0[
    }

And say you have a BufferedImage that has a size of 320x200 (64 000 pixels) of type INT_ARGB (8 bits per value + 8 bits for the alpha level):

    final BufferedImage bi = new BufferedImage( 320, 200, BufferedImage.TYPE_INT_ARGB );

Then you can convert you float array to RGB value and fill the image doing this:

    for (int x = 0; x < 320; x++) {
        for (int y = 0; y < 200; y++) {
            final int r = (int) (f[x+y*200*3] * 255.0);
            final int g = (int) (f[x+y*200*3+1] * 255.0);
            final int b = (int) (f[x+y*200*3+2] * 255.0);
            bi.setRGB( x, y, 0xFF000000 | (r << 16) | (g << 8) | b );
        }
    }

Note that would you display this image it would appear gray but if you zoom in it you'll see it's actually made of perfectly random colorful pixels. It's just that the random number generator is so good that it all looks gray on screen :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM