简体   繁体   English

IllegalArgumentException:颜色参数超出预期范围:红色绿色蓝色

[英]IllegalArgumentException: Color parameter outside of expected range: Red Green Blue

when I tested my code with JUnit, the following error occured: 当我用JUnit测试代码时,发生以下错误:

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

When I run the JUnit test, eclipse marks up the line with 当我运行JUnit测试时,eclipse用

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? 谁能帮我解决这个问题?

You are calling the Color constructor with three float parameters so the values are allowed to be between 0.0 and 1.0. 您正在使用三个float参数来调用Color构造函数,因此允许值在0.0到1.0之间。

But color.getRed() (Blue, Green) can return a value up to 255. So you can get the following: 但是color.getRed()(蓝色,绿色)最多可以返回255。因此,您可以获得以下内容:

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

Which is far to high for 1.0f and even for 252 which the Color(int,int,int) constructor allows. 对于1.0f甚至对于Color(int,int,int)构造函数允许的252来说,这都是很高的。 So scale your factors like dasblinkenlight said and cast the greyscale to an int or else you will call the wrong constructor of Color.` 因此,请像dasblinkenlight所说的那样缩放因子,并将灰度转换为int,否则将调用错误的Color构造函数。

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

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

相关问题 使用Color.brighter方法打印红色,绿色和蓝色值 - Using the Color.brighter method to print red, green, and blue values 从Android Color对象获取红色,蓝色或绿色通道 - Get Red, Blue or Green channel from Android Color object 将红色、绿色和蓝色转换为 RGB - Converting Red, Green and Blue to RGB Java - 红色,绿色,蓝色到getRGB - Java - Red, Green, Blue to getRGB Eclipse中的red(),green(),blue()方法 - red(), green(), blue() method in Eclipse Java BufferedImage分别获得红色,绿色和蓝色 - Java BufferedImage getting red, green and blue individually 将色彩空间为8BitARGB的字节数组转换为BufferedImage时,哪个字节(Alpha,Red,Green,Blue)是错误的。TYPE_4BYTE_ABGR - Which byte(Alpha, Red, Green, Blue) is wrong when converting byte array with color space of 8BitARGB to BufferedImage.TYPE_4BYTE_ABGR 如何获得 colors 的五个输入的序列。 颜色应该只有红色,绿色和蓝色需要colors的图案。在Java中编程? - How to get a sequence of five inputs of colors. Color should be only Red,Green and Blue need a pattern of colors.Programming in Java? Java中的图像反量化在图像上以红色,绿色或蓝色给出随机点 - Image dequantization in Java giving ramdom points in red , green or blue on Image 为什么我的 if 语句不适用于 (red || green || blue) &gt; 255 - Why doesn't my if statement work with (red || green || blue) > 255
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM