butterchicken
butterchicken

Reputation: 13883

Java Color creation throws IllegalArgumentException when used with integer arguments

The following code when executed on certain machines in our company causes an IllegalArgumentException to be thrown:

Color sludge = new Color(133, 133, 78);
//throws IAE with message "Color parameter outside of expected range: Red Green Blue"

An equivalent call using float arguments instead works:

Color sludge = new Color(0.522, 0.522, 0.306); // 133/255 = 0.522, 78/255 = 0.306

Why might this be the case? And why would it only affect certain machines?

Might it have something to do with the fact that these Color objects are defined in Spring like this:

<bean id="sludge" class="java.awt.Color">
    <constructor-arg value="133"/>
    <constructor-arg value="133"/>
    <constructor-arg value="78"/>
</bean>

Upvotes: 4

Views: 3613

Answers (2)

dfa
dfa

Reputation: 116334

being more pedant:

<bean id="sludge" class="java.awt.Color">
    <constructor-arg index="0" type="int"><value>133</value></constructor-arg>
    <constructor-arg index="1" type="int"><value>133</value></constructor-arg>
    <constructor-arg index="2" type="int"><value>78</value></constructor-arg>
</bean>

EDIT

check also this blog post

Upvotes: 4

Pierre
Pierre

Reputation: 35246

I'm NOT an expert with spring. but did you tried to set the type to int ?

<constructor-arg type="int" value="133">

?

Upvotes: 3

Related Questions