IllegalArgumentException:颜色参数超出预期范围:红色绿色蓝色 [英] IllegalArgumentException: Color parameter outside of expected range: Red Green Blue

查看:4649
本文介绍了IllegalArgumentException:颜色参数超出预期范围:红色绿色蓝色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用JUnit测试我的代码时,发生以下错误:

when I tested my code with JUnit, the following error occured:

java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue

老实说,我不知道为什么。我的代码不是很长,所以我想发布更好的帮助。

Honestly, I don't know why. My code is not very long, so I would like to post it for better help.

BufferedImage img = ImageIO.read(f);
        for (int w = 0; w < img.getWidth(); w++) {
            for (int h = 0; h < img.getHeight(); h++) {
                Color color = new Color(img.getRGB(w, h));
                float greyscale = ((0.299f * color.getRed()) + (0.587f
                        * color.getGreen()) + (0.144f * color.getBlue()));
                Color grey = new Color(greyscale, greyscale, greyscale);
                img.setRGB(w, h, grey.getRGB());

当我运行JUnit测试时,eclipse将标记为

When I run the JUnit test, eclipse marks up the line with

Color grey = new Color(greyscale, greyscale, greyscale);

所以,我想问题可能是,我使用浮动数字,重新计算图像的红色,绿色和蓝色内容。

So, I suppose the problem might be, that I work with floating numbers and as you can see I recalculate the red, green and blue content of the image.

有人可以帮我解决这个问题吗?

Could anyone help me to solve that problem?

推荐答案

您正在调用具有三个float参数的Color构造函数,因此值允许在0.0和1.0之间。

You are calling the Color constructor with three float parameters so the values are allowed to be between 0.0 and 1.0.

.getRed()(Blue,Green)可以返回一个最大为255的值。所以你可以得到以下结果:

But color.getRed() (Blue, Green) can return a value up to 255. So you can get the following:

float greyscale = ((0.299f *255) + (0.587f * 255) + (0.144f * 255));
System.out.println(greyscale); //262.65

这对于1.0f甚至对于252来说是远远高于Color, int,int)构造函数允许。因此,缩放你的因素,如dasblinkenlight说和投射灰度到一个int,否则你会调用颜色的错误构造函数。

Which is far to high for 1.0f and even for 252 which the Color(int,int,int) constructor allows. So scale your factors like dasblinkenlight said and cast the greyscale to an int or else you will call the wrong constructor of Color.`

new Color((int)greyscale,(int)greyscale,(int)greyscale);

这篇关于IllegalArgumentException:颜色参数超出预期范围:红色绿色蓝色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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