IllegalArgumentException: Color parameter outside of expected range: Red Green Blue

后端 未结 1 1339
暖寄归人
暖寄归人 2021-01-21 09:21

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

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


        
相关标签:
1条回答
  • 2021-01-21 10:09

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

    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
    

    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);
    
    0 讨论(0)
提交回复
热议问题