ColorUtils.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.ColorUtils = {
  5. adjustHSV : function ( color, h, s, v ) {
  6. var hsv = THREE.ColorUtils.__hsv;
  7. THREE.ColorUtils.rgbToHsv( color, hsv );
  8. hsv.h = THREE.ColorUtils.clamp( hsv.h + h, 0, 1 );
  9. hsv.s = THREE.ColorUtils.clamp( hsv.s + s, 0, 1 );
  10. hsv.v = THREE.ColorUtils.clamp( hsv.v + v, 0, 1 );
  11. color.setHSV( hsv.h, hsv.s, hsv.v );
  12. },
  13. // based on MochiKit implementation by Bob Ippolito
  14. rgbToHsv : function ( color, hsv ) {
  15. var r = color.r;
  16. var g = color.g;
  17. var b = color.b;
  18. var max = Math.max( Math.max( r, g ), b );
  19. var min = Math.min( Math.min( r, g ), b );
  20. var hue;
  21. var saturation;
  22. var value = max;
  23. if ( min == max ) {
  24. hue = 0;
  25. saturation = 0;
  26. } else {
  27. var delta = ( max - min );
  28. saturation = delta / max;
  29. if ( r == max ) {
  30. hue = ( g - b ) / delta;
  31. } else if ( g == max ) {
  32. hue = 2 + ( ( b - r ) / delta );
  33. } else {
  34. hue = 4 + ( ( r - g ) / delta );
  35. }
  36. hue /= 6;
  37. if ( hue < 0 ) {
  38. hue += 1;
  39. }
  40. if ( hue > 1 ) {
  41. hue -= 1;
  42. }
  43. }
  44. if ( hsv === undefined ) {
  45. hsv = { h: 0, s: 0, v: 0 };
  46. }
  47. hsv.h = hue;
  48. hsv.s = saturation;
  49. hsv.v = value;
  50. return hsv;
  51. },
  52. clamp: function ( x, a, b ) {
  53. return x < a ? a : ( x > b ? b : x );
  54. }
  55. };
  56. THREE.ColorUtils.__hsv = { h: 0, s: 0, v: 0 };
粤ICP备19079148号