ColorConverter.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @author bhouston / http://exocortex.com/
  3. */
  4. THREE.ColorConverter = {
  5. fromRGB: function ( color, r, g, b ) {
  6. return color.setRGB( r, g, b );
  7. },
  8. fromHex: function ( color, hex ) {
  9. return color.setHex( hex );
  10. },
  11. toHex: function ( color ) {
  12. return color.getHex();
  13. },
  14. toHexString: function ( color ) {
  15. return color.getHexString( color );
  16. },
  17. fromHSL: function ( color, h, s, l ) {
  18. return color.setHSL( h, s, l );
  19. },
  20. toHSL: function ( color ) {
  21. return color.getHSL( color );
  22. },
  23. fromHSV: function ( color, h, s, v ) {
  24. // https://gist.github.com/xpansive/1337890#file-index-js
  25. return color.setHSL( h, ( s * v ) / ( ( h = ( 2 - s ) * v ) < 1 ? h : ( 2 - h ) ), h * 0.5 );
  26. },
  27. toHSV: function () {
  28. var hsv = { h: 0, s: 0, v: 0 };
  29. return function( color ) {
  30. var hsl = color.getHSL();
  31. // based on https://gist.github.com/xpansive/1337890#file-index-js
  32. hsl.s *= ( hsl.l < 0.5 ) ? hsl.l : ( 1 - hsl.l );
  33. hsv.h = hsl.h;
  34. hsv.s = 2 * hsl.s / ( hsl.l + hsl.s );
  35. hsv.v = hsl.l + hsl.s;
  36. return hsv;
  37. }
  38. }(),
  39. fromStyle: function ( color, style ) {
  40. return color.setStyle( style );
  41. },
  42. toStyle: function ( color ) {
  43. return color.getStyle( color );
  44. }
  45. };