ColorConverter.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { MathUtils } from 'three';
  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 = MathUtils.euclideanModulo( h, 1 );
  7. s = MathUtils.clamp( s, 0, 1 );
  8. v = 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. color.getHSL( _hsl );
  13. // based on https://gist.github.com/xpansive/1337890#file-index-js
  14. _hsl.s *= ( _hsl.l < 0.5 ) ? _hsl.l : ( 1 - _hsl.l );
  15. target.h = _hsl.h;
  16. target.s = 2 * _hsl.s / ( _hsl.l + _hsl.s );
  17. target.v = _hsl.l + _hsl.s;
  18. return target;
  19. }
  20. // where c, m, y, k is between 0 and 1
  21. static setCMYK( color, c, m, y, k ) {
  22. const r = ( 1 - c ) * ( 1 - k );
  23. const g = ( 1 - m ) * ( 1 - k );
  24. const b = ( 1 - y ) * ( 1 - k );
  25. return color.setRGB( r, g, b );
  26. }
  27. static getCMYK( color, target ) {
  28. const r = color.r;
  29. const g = color.g;
  30. const b = color.b;
  31. const k = 1 - Math.max( r, g, b );
  32. const c = ( 1 - r - k ) / ( 1 - k );
  33. const m = ( 1 - g - k ) / ( 1 - k );
  34. const y = ( 1 - b - k ) / ( 1 - k );
  35. target.c = c;
  36. target.m = m;
  37. target.y = y;
  38. target.k = k;
  39. return target;
  40. }
  41. }
  42. export { ColorConverter };