allc
Version:
A small typescript package for color conversion.
49 lines (48 loc) • 1.93 kB
JavaScript
/**
* Helper function for converting HSV to RGB.
*
* @param h The hue component of HSV, in radians.
* @param s The saturation component of HSV, typically in the range [0, 1].
* @param v The value component of HSV, typically in the range [0, 1].
* @param n A component-specific number.
*
* @returns The RGB component, typically in the range [0, 1].
* @see https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
*/
const f = (h, s, v, n) => {
const k = (n + h * 3 / Math.PI) % 6;
return v - v * s * Math.max(0, Math.min(k, 4 - k, 1));
};
/**
* Calculates the red component of RGB from HSV.
*
* @param h The hue component of HSV, in radians.
* @param s The saturation component of HSV, typically in the range [0, 1].
* @param v The value component of HSV, typically in the range [0, 1].
*
* @returns The red component, typically in the range [0, 1].
* @see https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
*/
export const toRGBRFromHSV = (h, s, v) => f(h, s, v, 5);
/**
* Calculates the green component of RGB from HSV.
*
* @param h The hue component of HSV, in radians.
* @param s The saturation component of HSV, typically in the range [0, 1].
* @param v The value component of HSV, typically in the range [0, 1].
*
* @returns The green component, typically in the range [0, 1].
* @see https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
*/
export const toRGBGFromHSV = (h, s, v) => f(h, s, v, 3);
/**
* Calculates the blue component of RGB from HSV.
*
* @param h The hue component of HSV, in radians.
* @param s The saturation component of HSV, typically in the range [0, 1].
* @param v The value component of HSV, typically in the range [0, 1].
*
* @returns The blue component, typically in the range [0, 1].
* @see https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
*/
export const toRGBBFromHSV = (h, s, v) => f(h, s, v, 1);