ColorConverter.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ( function () {
  2. const _hsl = {};
  3. class ColorConverter {
  4. static setHSV( color, h, s, v ) {
  5. // https://gist.github.com/xpansive/1337890#file-index-js
  6. h = THREE.MathUtils.euclideanModulo( h, 1 );
  7. s = THREE.MathUtils.clamp( s, 0, 1 );
  8. v = THREE.MathUtils.clamp( v, 0, 1 );
  9. return color.setHSL( h, s * v / ( ( h = ( 2 - s ) * v ) < 1 ? h : 2 - h ), h * 0.5 );
  10. }
  11. static getHSV( color, target ) {
  12. if ( target === undefined ) {
  13. console.warn( 'THREE.ColorConverter: .getHSV() target is now required' );
  14. target = {
  15. h: 0,
  16. s: 0,
  17. l: 0
  18. };
  19. }
  20. color.getHSL( _hsl ); // based on https://gist.github.com/xpansive/1337890#file-index-js
  21. _hsl.s *= _hsl.l < 0.5 ? _hsl.l : 1 - _hsl.l;
  22. target.h = _hsl.h;
  23. target.s = 2 * _hsl.s / ( _hsl.l + _hsl.s );
  24. target.v = _hsl.l + _hsl.s;
  25. return target;
  26. } // where c, m, y, k is between 0 and 1
  27. static setCMYK( color, c, m, y, k ) {
  28. const r = ( 1 - c ) * ( 1 - k );
  29. const g = ( 1 - m ) * ( 1 - k );
  30. const b = ( 1 - y ) * ( 1 - k );
  31. return color.setRGB( r, g, b );
  32. }
  33. static getCMYK( color, target ) {
  34. if ( target === undefined ) {
  35. console.warn( 'THREE.ColorConverter: .getCMYK() target is now required' );
  36. target = {
  37. c: 0,
  38. m: 0,
  39. y: 0,
  40. k: 0
  41. };
  42. }
  43. const r = color.r;
  44. const g = color.g;
  45. const b = color.b;
  46. const k = 1 - Math.max( r, g, b );
  47. const c = ( 1 - r - k ) / ( 1 - k );
  48. const m = ( 1 - g - k ) / ( 1 - k );
  49. const y = ( 1 - b - k ) / ( 1 - k );
  50. target.c = c;
  51. target.m = m;
  52. target.y = y;
  53. target.k = k;
  54. return target;
  55. }
  56. }
  57. THREE.ColorConverter = ColorConverter;
  58. } )();