UNPKG

kewler

Version:

Simple functional and immutable color manipulation library

181 lines (143 loc) 5.67 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var flow = _interopDefault(require('lodash.flow')); var isFunction = _interopDefault(require('lodash.isfunction')); var isArray = _interopDefault(require('lodash.isarray')); var _hslToHex = _interopDefault(require('hsl-to-hex')); var hexToHsl = _interopDefault(require('hex-to-hsl')); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; // Change an hsl object { hue: int, sat: int, lit: int } to an array [int, int, int] var hslObjToArray = function hslObjToArray(_ref) { var hue = _ref.hue; var sat = _ref.sat; var lit = _ref.lit; return [hue, sat, lit]; }; // Returns a boolean telling if this is a valid hsl array [int, int, int] var isHSLArray = function isHSLArray(c) { return isArray(c) && c.length === 3 && typeof c[0] === 'number' && c[0] >= 0 && c[0] <= 360 && typeof c[1] === 'number' && c[1] >= 0 && c[1] <= 100 && typeof c[2] === 'number' && c[2] >= 0 && c[2] <= 100; }; // Returns a boolean telling if this is a valid hsl object { hue: int, sat: int, lit: int } var isHSLObject = function isHSLObject(c) { return (typeof c === 'undefined' ? 'undefined' : _typeof(c)) === 'object' && isHSLArray(hslObjToArray(c)); }; // Returns a boolean telling if this is a valid hex color '#000000' var isHexColor = function isHexColor(c) { return typeof c === 'string' && c[0] === '#' && (c.length === 7 || c.length === 4); }; function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } // Return an HSL array whatever the color type is passed var colorToHSL = function colorToHSL(c) { if (isHSLArray(c)) { return c; } if (isHSLObject(c)) { return hslObjToArray(c); } if (isHexColor(c)) { return hexToHsl(c); } return c; }; // Returns an hex string value, whatever color type is passed in argument var colorToHex = function colorToHex(c) { if (isArray(c)) { return _hslToHex.apply(undefined, _toConsumableArray(c)); } if (isHSLObject(c)) { return _hslToHex(c.hue, c.sat, c.lit); } if (isHexColor(c)) { return c; } return c; }; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); // Makes sure the function gets an HSL object var withHSL = function withHSL(func) { return function (c) { return func(colorToHSL(c)); }; }; var isValidColor = function isValidColor(col) { return col && (isHSLArray(col) || isHSLObject(col) || isHexColor(col)); }; // Return hex from a color var color = function color(col) { for (var _len = arguments.length, colAlts = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { colAlts[_key - 1] = arguments[_key]; } // If the color passed is a function, execute it if (isFunction(col)) { col = col(); } // If nothing is passed, return a black color by default if (!col) { return '#000000'; } // Validate if (!isValidColor(col)) { throw new Error('Color passed is not a valid color'); } // When alterations are passed, process the color and return an hex value if (colAlts.length) { var alteration = flow(colAlts); return colorToHex(alteration(col)); } // If just a color is passed, return a function which takes alterations return function () { for (var _len2 = arguments.length, alts = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { alts[_key2] = arguments[_key2]; } var firstArg = alts[0]; if (firstArg && isValidColor(firstArg)) { return color(firstArg); } // Return the altered color wrapper if alterations are passed if (alts.length) { var _alteration = flow(alts); return color(_alteration(col)); } // If no argument is passed, return the hex color return colorToHex(col); }; }; // British proxy var colour = color; // Alter lightness of an hsl color var lightness = function lightness(percent) { return withHSL(function (_ref) { var _ref2 = _slicedToArray(_ref, 3); var hue = _ref2[0]; var sat = _ref2[1]; var lit = _ref2[2]; return [hue, sat, lit + percent]; }); }; // Alter saturation of an hsl color var saturation = function saturation(percent) { return withHSL(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 3); var hue = _ref4[0]; var sat = _ref4[1]; var lit = _ref4[2]; return [hue, sat + percent, lit]; }); }; // Alter hue of an hsl color var hue = function hue(percent) { return withHSL(function (_ref5) { var _ref6 = _slicedToArray(_ref5, 3); var hue = _ref6[0]; var sat = _ref6[1]; var lit = _ref6[2]; return [hue + percent, sat, lit]; }); }; exports.color = color; exports.colour = colour; exports.lightness = lightness; exports.saturation = saturation; exports.hue = hue; //# sourceMappingURL=index.js.map