UNPKG

color-fns

Version:

Modern JavaScript color utility library.

43 lines 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var parseRgb_1 = require("./parseRgb"); function rgbToHsv(rgb) { var value = typeof rgb === 'string' ? parseRgb_1.parseRgb(rgb) : rgb; if (!value) { return null; } // Convert the RGB values to the range 0-1 var _a = [value.red / 255, value.green / 255, value.blue / 255, value.alpha], red = _a[0], green = _a[1], blue = _a[2], alpha = _a[3]; var _b = [0, 0, 0], hue = _b[0], sat = _b[1], val = _b[2]; // Find the minimum and maximum values of R, G and B. var min = Math.min(red, green, blue); var max = Math.max(red, green, blue); // Calculate the saturation. if (max !== 0) { sat = (max - min) / max; } // calculate the value val = max; // calculate the hue if (red >= max && min !== max) { hue = 60 * ((green - blue) / (max - min)); } if (green >= max && min !== max) { hue = 60 * (2.0 + (blue - red) / (max - min)); } if (blue >= max && min !== max) { hue = 60 * (4.0 + (red - green) / (max - min)); } // normalize values hue = hue < 0 ? Math.floor(hue + 360) : Math.floor(hue); sat = Math.floor(sat * 100); val = Math.floor(val * 100); return { alpha: alpha || 1, hue: hue, sat: sat, val: val }; } exports.rgbToHsv = rgbToHsv; //# sourceMappingURL=rgbToHsv.js.map