ColorConverter.js 1.5 KB

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