UNPKG

@vimeo/iris

Version:
52 lines (47 loc) 1.81 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('../tslib.es6-3ec409b7.js'); var polished = require('polished'); var HSVtoHSL = function (_a) { var hue = _a.hue, saturation = _a.saturation, value = _a.value; return ({ hue: round(hue), saturation: round(saturation), lightness: round(((2 - saturation) * value) / 2), }); }; function HSLtoHSV(_a) { var hue = _a.hue, HSL_S = _a.saturation, lightness = _a.lightness; HSL_S *= lightness < 0.5 ? lightness : 1 - lightness; var saturation = Math.abs(round((2 * HSL_S) / (lightness + HSL_S))) || 0; var value = round(lightness + saturation); return { hue: hue, saturation: saturation, value: value }; } function colorSpaces(payload) { var HSV = roundValues(HSLtoHSV(payload)); var HEX = SixDigitHEX(polished.hsl(payload).toUpperCase()); var RGB = RGBObject(payload); return { HSV: HSV, HEX: HEX, RGB: RGB, HSL: payload }; } var round = function (num) { return Math.round(num * 1000) / 1000; }; var roundValues = function (obj) { return Object.keys(obj).map(function (key) { return (obj[key] = round(obj[key])); }); }; function RGBObject(HSL) { var RGBArray = roundValues(polished.parseToRgb(polished.toColorString(HSL))); var _a = tslib_es6.__read(RGBArray, 3), red = _a[0], green = _a[1], blue = _a[2]; return { red: red, green: green, blue: blue }; } function SixDigitHEX(HEX) { HEX = HEX.replace('#', ''); if (HEX.length === 3) HEX = HEX.split('') .map(function (hex) { return hex + hex; }) .join(''); return '#' + HEX; } exports.HSLtoHSV = HSLtoHSV; exports.HSVtoHSL = HSVtoHSL; exports.colorSpaces = colorSpaces; exports.round = round; exports.roundValues = roundValues;