ColorConverter.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @author bhouston / http://exocortex.com/
  3. * @author zz85 / http://github.com/zz85
  4. */
  5. THREE.ColorConverter = {
  6. setHSV: function ( color, h, s, v ) {
  7. // https://gist.github.com/xpansive/1337890#file-index-js
  8. h = THREE.Math.euclideanModulo( h, 1 );
  9. s = THREE.Math.clamp( s, 0, 1 );
  10. v = THREE.Math.clamp( v, 0, 1 );
  11. return color.setHSL( h, ( s * v ) / ( ( h = ( 2 - s ) * v ) < 1 ? h : ( 2 - h ) ), h * 0.5 );
  12. },
  13. getHSV: function( color ) {
  14. var hsl = color.getHSL();
  15. // based on https://gist.github.com/xpansive/1337890#file-index-js
  16. hsl.s *= ( hsl.l < 0.5 ) ? hsl.l : ( 1 - hsl.l );
  17. return {
  18. h: hsl.h,
  19. s: 2 * hsl.s / ( hsl.l + hsl.s ),
  20. v: hsl.l + hsl.s
  21. };
  22. },
  23. // where c, m, y, k is between 0 and 1
  24. setCMYK: function ( color, c, m, y, k ) {
  25. var r = ( 1 - c ) * ( 1 - k );
  26. var g = ( 1 - m ) * ( 1 - k );
  27. var b = ( 1 - y ) * ( 1 - k );
  28. return color.setRGB( r, g, b );
  29. },
  30. getCMYK: function ( color ) {
  31. var r = color.r;
  32. var g = color.g;
  33. var b = color.b;
  34. var k = 1 - Math.max( r, g, b );
  35. var c = ( 1 - r - k ) / ( 1 - k );
  36. var m = ( 1 - g - k ) / ( 1 - k );
  37. var y = ( 1 - b - k ) / ( 1 - k );
  38. return {
  39. c: c, m: m, y: y, k: k
  40. };
  41. }
  42. };