All files / packages/utils/src color.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  268x         9648x 9648x 28944x   28944x   28944x       9648x    
// see https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
export const hslToRgb = (
	hueAngle: number,
	saturation: number,
	lightness: number,
): string => {
	const lightSaturation = saturation * Math.min(lightness, 1 - lightness);
	const getColor = (rotationShift: number) => {
		const coef = (rotationShift + hueAngle / 30) % 12;
		const colorComponent =
			lightness -
			lightSaturation * Math.max(-1, Math.min(coef - 3, Math.min(9 - coef, 1)));
		return Math.round(255 * colorComponent)
			.toString(16)
			.padStart(2, "0");
	};
	return `${getColor(0)}${getColor(8)}${getColor(4)}`;
};