ColorSpaces.tests.js 3.4 KB

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