Open pixelzoom opened 3 years ago
There's a divide-by-zero problem in brighterColor
:
brighterColor( factor ) {
factor = this.checkFactor( factor );
const red = Math.min( 255, Math.floor( this.r / factor ) );
const green = Math.min( 255, Math.floor( this.g / factor ) );
const blue = Math.min( 255, Math.floor( this.b / factor ) );
return new Color( red, green, blue, this.a );
}
And 0
is a valid value for factor
:
checkFactor( factor ) {
assert && assert( factor === undefined || ( factor >= 0 && factor <= 1 ), `factor must be between 0 and 1: ${factor}` );
return ( factor === undefined ) ? 0.7 : factor;
}
Ha, looks like I introduced this problem back in 2013, see 026a12fe5a5da543d886adc4923a9542df0c77ce.
Suggestions for how to reimplement while still remaining compatible with Java's implementation?
Java only uses 0.7, from https://developer.classpath.org/doc/java/awt/Color-source.html. So adjusting the factor range for this method to not allow zero sounds best. Thoughts?
In java.awt.Color:
/** Amount to scale a color by when brightening or darkening. */
private static final float BRIGHT_SCALE = 0.7f;
BRIGHTNESS_SCALE
is used in method darker
, but 0.7f
is hardcoded in method brighter
. Go figure...
I don't see any "darker" or "brighter" methods in Java that take a factor, so I'm not sure how we're being Java compatible. So I guess 0.7f is the factor that is applied on every call to darker
or brighter
.
I see 10 uses of brighterColor
in PhET code. Only 2 of them are using a factor = 0.7, and 3 are relying on factor=0. So not supporting zero is going to be a pain.
I was going to suggest ditching brighterColor
and darkerColor
, and using colorUtilsBrighter
and colorUtilsDarker
. But there are 44 uses of darkerColor
.
Resolving this is going to take a lot of time, first fixing the problem, then inspecting sims to see if visual changes are acceptable. I don't have time for that, so I'm going to unassign. If @jonathanolson or someone else wants to work on this, go for it.
new Color( r, g, b ).brighterColor( 0 )
fails if any of the args to Color's constructor are0
.To demonstrate in a browser console:
According to the doc and assertions in Color.js, all of the arguments are valid in the above case that is failing.
I don't see a problem with any of these similar edge cases: