Java中的渐变颜色查找 [英] Gradient color lookup in Java

查看:91
本文介绍了Java中的渐变颜色查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个0-1值列表.我想通过使用将这些浮点值转换为RGB值的渐变将该列表转换为图像. 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?

推荐答案

假设您有一个由64 000个由RGB值组成的三元组组成的数组,如下所示:

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[
    }

并且假设您有一个BufferedImage,它的大小为320x200(64 000像素),类型为INT_ARGB(每个值8位+ Alpha级别8位):

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 );

然后,您可以将float数组转换为RGB值,并执行以下操作来填充图像:

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 :)

这篇关于Java中的渐变颜色查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆