ColorSpaces.tests.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Color } from '../../../../src/math/Color.js';
  2. import { ColorManagement } from '../../../../src/math/ColorManagement.js';
  3. import { LinearSRGBColorSpace } from '../../../../src/constants.js';
  4. import {
  5. DisplayP3ColorSpace,
  6. DisplayP3ColorSpaceImpl,
  7. LinearDisplayP3ColorSpace,
  8. LinearDisplayP3ColorSpaceImpl,
  9. LinearRec2020ColorSpace,
  10. LinearRec2020ColorSpaceImpl
  11. } from '../../../../examples/jsm/math/ColorSpaces.js';
  12. // Reference: https://apps.colorjs.io/convert/
  13. export default QUnit.module( 'Maths', () => {
  14. QUnit.module( 'ColorSpaces', () => {
  15. ColorManagement.define( {
  16. [ DisplayP3ColorSpace ]: DisplayP3ColorSpaceImpl,
  17. [ LinearDisplayP3ColorSpace ]: LinearDisplayP3ColorSpaceImpl,
  18. [ LinearRec2020ColorSpace ]: LinearRec2020ColorSpaceImpl
  19. } );
  20. QUnit.test( 'DisplayP3ColorSpace', ( assert ) => {
  21. const c = new Color().setRGB( 0.3, 0.5, 0.7 );
  22. ColorManagement.convert( c, LinearSRGBColorSpace, DisplayP3ColorSpace );
  23. assert.equal( c.r.toFixed( 3 ), 0.614, 'Red: ' + c.r + ' (display-p3, in gamut)' );
  24. assert.equal( c.g.toFixed( 3 ), 0.731, 'Green: ' + c.g + ' (display-p3, in gamut)' );
  25. assert.equal( c.b.toFixed( 3 ), 0.843, 'Blue: ' + c.b + ' (display-p3, in gamut)' );
  26. c.setRGB( 1.0, 0.5, 0.01, DisplayP3ColorSpace );
  27. assert.equal( c.r.toFixed( 3 ), 1.177, 'Red: ' + c.r + ' (srgb-linear, out of gamut)' );
  28. assert.equal( c.g.toFixed( 3 ), 0.181, 'Green: ' + c.g + ' (srgb-linear, out of gamut)' );
  29. assert.equal( c.b.toFixed( 3 ), - 0.036, 'Blue: ' + c.b + ' (srgb-linear, out of gamut)' );
  30. assert.equal( c.getStyle( DisplayP3ColorSpace ), 'color(display-p3 1.000 0.500 0.010)', 'style: display-p3' );
  31. } );
  32. QUnit.test( 'LinearDisplayP3ColorSpace', ( assert ) => {
  33. const c = new Color().setRGB( 0.3, 0.5, 0.7 );
  34. ColorManagement.convert( c, LinearSRGBColorSpace, LinearDisplayP3ColorSpace );
  35. assert.equal( c.r.toFixed( 3 ), 0.336, 'Red: ' + c.r + ' (display-p3-linear, in gamut)' );
  36. assert.equal( c.g.toFixed( 3 ), 0.493, 'Green: ' + c.g + ' (display-p3-linear, in gamut)' );
  37. assert.equal( c.b.toFixed( 3 ), 0.679, 'Blue: ' + c.b + ' (display-p3-linear, in gamut)' );
  38. c.setRGB( 1.0, 0.5, 0.01, LinearDisplayP3ColorSpace );
  39. assert.equal( c.r.toFixed( 3 ), 1.112, 'Red: ' + c.r + ' (srgb-linear, out of gamut)' );
  40. assert.equal( c.g.toFixed( 3 ), 0.479, 'Green: ' + c.g + ' (srgb-linear, out of gamut)' );
  41. assert.equal( c.b.toFixed( 3 ), - 0.048, 'Blue: ' + c.b + ' (srgb-linear, out of gamut)' );
  42. assert.equal( c.getStyle( LinearDisplayP3ColorSpace ), 'color(display-p3-linear 1.000 0.500 0.010)', 'style: display-p3-linear' );
  43. } );
  44. QUnit.test( 'LinearRec2020ColorSpace', ( assert ) => {
  45. const c = new Color().setRGB( 0.3, 0.5, 0.7 );
  46. ColorManagement.convert( c, LinearSRGBColorSpace, LinearRec2020ColorSpace );
  47. assert.equal( c.r.toFixed( 3 ), 0.383, 'Red: ' + c.r + ' (rec2020-linear, in gamut)' );
  48. assert.equal( c.g.toFixed( 3 ), 0.488, 'Green: ' + c.g + ' (rec2020-linear, in gamut)' );
  49. assert.equal( c.b.toFixed( 3 ), 0.676, 'Blue: ' + c.b + ' (rec2020-linear, in gamut)' );
  50. c.setRGB( 1.0, 0.5, 0.01, LinearRec2020ColorSpace );
  51. assert.equal( c.r.toFixed( 3 ), 1.366, 'Red: ' + c.r + ' (srgb-linear, out of gamut)' );
  52. assert.equal( c.g.toFixed( 3 ), 0.442, 'Green: ' + c.g + ' (srgb-linear, out of gamut)' );
  53. assert.equal( c.b.toFixed( 3 ), - 0.057, 'Blue: ' + c.b + ' (srgb-linear, out of gamut)' );
  54. assert.equal( c.getStyle( LinearRec2020ColorSpace ), 'color(rec2020-linear 1.000 0.500 0.010)', 'style: rec2020-linear' );
  55. } );
  56. } );
  57. } );
粤ICP备19079148号