简体   繁体   English

Java中的渐变颜色查找

[英]Gradient color lookup in Java

I have a list of values from 0-1. 我有一个0-1值列表。 I want to convert this list to an image by using a gradient that converts these floating point values to RGB values. 我想通过使用将这些浮点值转换为RGB值的渐变将该列表转换为图像。 Are there any tools in Java that provide you with this functionality? Java中是否有任何工具可以为您提供此功能?

0 should be mapped 0 0应该被映射0
1 should be mapped 255 1应该被映射255

keep in mind that you need 3 of them to make a color 请记住,您需要其中3种来制作颜色

so multiply by 255 the floating number and cast it to int. 因此将浮点数乘以255,然后将其强制转换为int。

Perhaps GradientPaint can do what you want. 也许GradientPaint可以做您想要的。 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. GradientPaint实现线性渐变。

Say you have an array made of 64 000 triples corresponding to RGB values, like this: 假设您有一个由64 000个三元组组成的数组,对应于RGB值,如下所示:

    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): 并说您有一个BufferedImage,其大小为320x200(64 000像素),类型为INT_ARGB(每个值8位+ Alpha级别8位):

    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: 然后,您可以将float数组转换为RGB值,并执行以下操作来填充图像:

    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 :) 只是随机数生成器是如此之好,以至于在屏幕上看起来都是灰色的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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