color-tf
Version:
RGB, HSL, HSV, HWB and more color models convertors
17 lines (12 loc) • 380 B
JavaScript
;
var rgb2hsv = (r, g, b) => {
const max = Math.max(r, g, b),
min = Math.min(r, g, b);
const v = max,
d = max - min,
s = max === 0 ? 0 : d / max;
if (d <= 0) return [0, s, v]; // achromatic
const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
return [h / 6, s, v];
};
module.exports = rgb2hsv;