|
1 | 1 | /* global QUnit */ |
2 | 2 |
|
3 | 3 | import { Color } from '../../../../src/math/Color.js'; |
| 4 | +import { ColorManagement } from '../../../../src/math/ColorManagement.js'; |
4 | 5 | import { eps } from '../../utils/math-constants.js'; |
5 | 6 | import { CONSOLE_LEVEL } from '../../utils/console-wrapper.js'; |
| 7 | +import { DisplayP3ColorSpace, SRGBColorSpace } from '../../../../src/constants.js'; |
6 | 8 |
|
7 | 9 | export default QUnit.module( 'Maths', () => { |
8 | 10 |
|
9 | 11 | QUnit.module( 'Color', () => { |
10 | 12 |
|
| 13 | + const colorManagementEnabled = ColorManagement.enabled; |
| 14 | + |
| 15 | + QUnit.testDone( () => { |
| 16 | + |
| 17 | + ColorManagement.enabled = colorManagementEnabled; |
| 18 | + |
| 19 | + } ); |
| 20 | + |
11 | 21 | // INSTANCING |
12 | 22 | QUnit.test( 'Instancing', ( assert ) => { |
13 | 23 |
|
@@ -84,11 +94,33 @@ export default QUnit.module( 'Maths', () => { |
84 | 94 |
|
85 | 95 | QUnit.test( 'setRGB', ( assert ) => { |
86 | 96 |
|
| 97 | + ColorManagement.enabled = true; |
| 98 | + |
87 | 99 | const c = new Color(); |
| 100 | + |
88 | 101 | c.setRGB( 0.3, 0.5, 0.7 ); |
89 | | - assert.ok( c.r == 0.3, 'Red: ' + c.r ); |
90 | | - assert.ok( c.g == 0.5, 'Green: ' + c.g ); |
91 | | - assert.ok( c.b == 0.7, 'Blue: ' + c.b ); |
| 102 | + |
| 103 | + assert.equal( c.r, 0.3, 'Red: ' + c.r + ' (srgb-linear)' ); |
| 104 | + assert.equal( c.g, 0.5, 'Green: ' + c.g + ' (srgb-linear)' ); |
| 105 | + assert.equal( c.b, 0.7, 'Blue: ' + c.b + ' (srgb-linear)' ); |
| 106 | + |
| 107 | + c.setRGB( 0.3, 0.5, 0.7, SRGBColorSpace ); |
| 108 | + |
| 109 | + assert.equal( c.r.toFixed( 3 ), 0.073, 'Red: ' + c.r + ' (srgb)' ); |
| 110 | + assert.equal( c.g.toFixed( 3 ), 0.214, 'Green: ' + c.g + ' (srgb)' ); |
| 111 | + assert.equal( c.b.toFixed( 3 ), 0.448, 'Blue: ' + c.b + ' (srgb)' ); |
| 112 | + |
| 113 | + c.setRGB( 0.614, 0.731, 0.843, DisplayP3ColorSpace ); |
| 114 | + |
| 115 | + assert.numEqual( c.r.toFixed( 2 ), 0.3, 'Red: ' + c.r + ' (display-p3, in gamut)' ); |
| 116 | + assert.numEqual( c.g.toFixed( 2 ), 0.5, 'Green: ' + c.g + ' (display-p3, in gamut)' ); |
| 117 | + assert.numEqual( c.b.toFixed( 2 ), 0.7, 'Blue: ' + c.b + ' (display-p3, in gamut)' ); |
| 118 | + |
| 119 | + c.setRGB( 1.0, 0.5, 0.0, DisplayP3ColorSpace ); |
| 120 | + |
| 121 | + assert.numEqual( c.r.toFixed( 3 ), 1.179, 'Red: ' + c.r + ' (display-p3, out of gamut)' ); |
| 122 | + assert.numEqual( c.g.toFixed( 3 ), 0.181, 'Green: ' + c.g + ' (display-p3, out of gamut)' ); |
| 123 | + assert.numEqual( c.b.toFixed( 3 ), - 0.036, 'Blue: ' + c.b + ' (display-p3, out of gamut)' ); |
92 | 124 |
|
93 | 125 | } ); |
94 | 126 |
|
@@ -251,18 +283,41 @@ export default QUnit.module( 'Maths', () => { |
251 | 283 |
|
252 | 284 | } ); |
253 | 285 |
|
254 | | - QUnit.todo( 'getRGB', ( assert ) => { |
| 286 | + QUnit.test( 'getRGB', ( assert ) => { |
255 | 287 |
|
256 | | - // getRGB( target, colorSpace = ColorManagement.workingColorSpace ) |
257 | | - assert.ok( false, 'everything\'s gonna be alright' ); |
| 288 | + ColorManagement.enabled = true; |
| 289 | + |
| 290 | + const c = new Color( 'plum' ); |
| 291 | + const t = { r: 0, g: 0, b: 0 }; |
| 292 | + |
| 293 | + c.getRGB( t ); |
| 294 | + |
| 295 | + assert.equal( t.r.toFixed( 3 ), 0.723, 'r (srgb-linear)' ); |
| 296 | + assert.equal( t.g.toFixed( 3 ), 0.352, 'g (srgb-linear)' ); |
| 297 | + assert.equal( t.b.toFixed( 3 ), 0.723, 'b (srgb-linear)' ); |
| 298 | + |
| 299 | + c.getRGB( t, SRGBColorSpace ); |
| 300 | + |
| 301 | + assert.equal( t.r.toFixed( 3 ), ( 221 / 255 ).toFixed( 3 ), 'r (srgb)' ); |
| 302 | + assert.equal( t.g.toFixed( 3 ), ( 160 / 255 ).toFixed( 3 ), 'g (srgb)' ); |
| 303 | + assert.equal( t.b.toFixed( 3 ), ( 221 / 255 ).toFixed( 3 ), 'b (srgb)' ); |
| 304 | + |
| 305 | + c.getRGB( t, DisplayP3ColorSpace ); |
| 306 | + |
| 307 | + assert.equal( t.r.toFixed( 3 ), 0.831, 'r (display-p3)' ); |
| 308 | + assert.equal( t.g.toFixed( 3 ), 0.637, 'g (display-p3)' ); |
| 309 | + assert.equal( t.b.toFixed( 3 ), 0.852, 'b (display-p3)' ); |
258 | 310 |
|
259 | 311 | } ); |
260 | 312 |
|
261 | 313 | QUnit.test( 'getStyle', ( assert ) => { |
262 | 314 |
|
| 315 | + ColorManagement.enabled = true; |
| 316 | + |
263 | 317 | const c = new Color( 'plum' ); |
264 | | - const res = c.getStyle(); |
265 | | - assert.ok( res == 'rgb(221,160,221)', 'style: ' + res ); |
| 318 | + |
| 319 | + assert.equal( c.getStyle(), 'rgb(221,160,221)', 'style: srgb' ); |
| 320 | + assert.equal( c.getStyle( DisplayP3ColorSpace ), 'color(display-p3 0.831 0.637 0.852)', 'style: display-p3' ); |
266 | 321 |
|
267 | 322 | } ); |
268 | 323 |
|
|
0 commit comments