ColorConverter.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { MathUtils } from 'three';
  2. const _hsl = {};
  3. /**
  4. * A utility class with helper functions for color conversion.
  5. *
  6. * @hideconstructor
  7. * @three_import import { ColorConverter } from 'three/addons/math/ColorConverter.js';
  8. */
  9. class ColorConverter {
  10. /**
  11. * Sets the given HSV color definition to the given color object.
  12. *
  13. * @param {Color} color - The color to set.
  14. * @param {number} h - The hue.
  15. * @param {number} s - The saturation.
  16. * @param {number} v - The value.
  17. * @return {Color} The update color.
  18. */
  19. static setHSV( color, h, s, v ) {
  20. // https://gist.github.com/xpansive/1337890#file-index-js
  21. h = MathUtils.euclideanModulo( h, 1 );
  22. s = MathUtils.clamp( s, 0, 1 );
  23. v = MathUtils.clamp( v, 0, 1 );
  24. return color.setHSL( h, ( s * v ) / ( ( h = ( 2 - s ) * v ) < 1 ? h : ( 2 - h ) ), h * 0.5 );
  25. }
  26. /**
  27. * Returns a HSV color representation of the given color object.
  28. *
  29. * @param {Color} color - The color to get HSV values from.
  30. * @param {{h:number,s:number,v:number}} target - The target object that is used to store the method's result.
  31. * @return {{h:number,s:number,v:number}} The HSV color.
  32. */
  33. static getHSV( color, target ) {
  34. color.getHSL( _hsl );
  35. // based on https://gist.github.com/xpansive/1337890#file-index-js
  36. _hsl.s *= ( _hsl.l < 0.5 ) ? _hsl.l : ( 1 - _hsl.l );
  37. target.h = _hsl.h;
  38. target.s = 2 * _hsl.s / ( _hsl.l + _hsl.s );
  39. target.v = _hsl.l + _hsl.s;
  40. return target;
  41. }
  42. }
  43. export { ColorConverter };
粤ICP备19079148号