hue.glsl 964 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * Adjusts the hue of a color.
  3. *
  4. * @name czm_hue
  5. * @glslFunction
  6. *
  7. * @param {vec3} rgb The color.
  8. * @param {float} adjustment The amount to adjust the hue of the color in radians.
  9. *
  10. * @returns {float} The color with the hue adjusted.
  11. *
  12. * @example
  13. * vec3 adjustHue = czm_hue(color, czm_pi); // The same as czm_hue(color, -czm_pi)
  14. */
  15. vec3 czm_hue(vec3 rgb, float adjustment)
  16. {
  17. const mat3 toYIQ = mat3(0.299, 0.587, 0.114,
  18. 0.595716, -0.274453, -0.321263,
  19. 0.211456, -0.522591, 0.311135);
  20. const mat3 toRGB = mat3(1.0, 0.9563, 0.6210,
  21. 1.0, -0.2721, -0.6474,
  22. 1.0, -1.107, 1.7046);
  23. vec3 yiq = toYIQ * rgb;
  24. float hue = atan(yiq.z, yiq.y) + adjustment;
  25. float chroma = sqrt(yiq.z * yiq.z + yiq.y * yiq.y);
  26. vec3 color = vec3(yiq.x, chroma * cos(hue), chroma * sin(hue));
  27. return toRGB * color;
  28. }