Browse Source

Color.getHex() and .getStyle(): Fix rounding errors (#25832)

* Color.setHex(): Fix rounding errors

* Fix the real problem

* Similar fix in `.getStyle()`

* Fix for bitwise operators precedence

* Fix failing test (rounds in another direction?)
Levi Pesin 2 years ago
parent
commit
ccaa0fc7bf
2 changed files with 3 additions and 3 deletions
  1. 2 2
      src/math/Color.js
  2. 1 1
      test/unit/src/math/Color.tests.js

+ 2 - 2
src/math/Color.js

@@ -349,7 +349,7 @@ class Color {
 
 		ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
 
-		return clamp( _color.r * 255, 0, 255 ) << 16 ^ clamp( _color.g * 255, 0, 255 ) << 8 ^ clamp( _color.b * 255, 0, 255 ) << 0;
+		return Math.round( clamp( _color.r * 255, 0, 255 ) ) * 65536 + Math.round( clamp( _color.g * 255, 0, 255 ) ) * 256 + Math.round( clamp( _color.b * 255, 0, 255 ) );
 
 	}
 
@@ -429,7 +429,7 @@ class Color {
 
 		}
 
-		return `rgb(${( r * 255 ) | 0},${( g * 255 ) | 0},${( b * 255 ) | 0})`;
+		return `rgb(${ Math.round( r * 255 ) },${ Math.round( g * 255 ) },${ Math.round( b * 255 ) })`;
 
 	}
 

+ 1 - 1
test/unit/src/math/Color.tests.js

@@ -511,7 +511,7 @@ export default QUnit.module( 'Maths', () => {
 			const d = new Color( 1.0, 1.0, 1.0 );
 
 			assert.strictEqual( a.toJSON(), 0x000000, 'Check black' );
-			assert.strictEqual( b.toJSON(), 0x007F00, 'Check half-blue' );
+			assert.strictEqual( b.toJSON(), 0x008000, 'Check half-blue' );
 			assert.strictEqual( c.toJSON(), 0xFF0000, 'Check red' );
 			assert.strictEqual( d.toJSON(), 0xFFFFFF, 'Check white' );