UNPKG

colorlyjs

Version:

A library for parsing colors within the sRGB color space. it includes rgb(a), hsl(a), hsv(a), hex and cmyk. It mainly focuses on web standards, which is why it has build in css support. However, it may be used in any application that follows the css color

542 lines (372 loc) 22.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _GenericsUtil = _interopRequireDefault(require("../util/GenericsUtil.js")); var _ParserUtil = _interopRequireDefault(require("../util/ParserUtil.js")); var _CSSUtil = _interopRequireDefault(require("../util/CSSUtil.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var Color = /*#__PURE__*/_createClass(function Color() { var _this = this; var _r = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 255; var _g = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 255; var _b = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 255; var a = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1; _classCallCheck(this, Color); _defineProperty(this, "r", void 0); _defineProperty(this, "g", void 0); _defineProperty(this, "b", void 0); _defineProperty(this, "a", void 0); _defineProperty(this, "precision", { rgb: 0, cmyk: 2, hsl: 2, hsv: 2, opacity: 2 }); _defineProperty(this, "addRed", function (r) { _this.setRed(r + _this.getRed()); }); _defineProperty(this, "addGreen", function (g) { _this.setGreen(g + _this.getGreen()); }); _defineProperty(this, "addBlue", function (b) { _this.setBlue(b + _this.getBlue()); }); _defineProperty(this, "addCyan", function (c) { var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setCyan(cmyk[0] + c); }); _defineProperty(this, "addYellow", function (y) { var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setYellow(cmyk[2] + y); }); _defineProperty(this, "addMagenta", function (m) { var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setMagenta(cmyk[1] + m); }); _defineProperty(this, "addBlack", function (k) { var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setBlack(cmyk[3] + k); }); _defineProperty(this, "addHue", function (h) { var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setHue(hsl[0] + h); }); _defineProperty(this, "addSaturation", function (s) { var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setSaturation(hsl[1] + s); }); _defineProperty(this, "addLightness", function (l) { var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setLightness(hsl[2] + l); }); _defineProperty(this, "addValue", function (v) { var hsv = _ParserUtil["default"].parseRGBAToHSV(_this.getRed(), _this.getGreen(), _this.getBlue()); _this.setValue(hsv[2] + v); }); _defineProperty(this, "addDecimalOpacity", function (o) { _this.setDecimalOpacity(o + _this.getDecimalOpacity()); }); _defineProperty(this, "addIntOpacity", function (o) { o /= _GenericsUtil["default"].maxIntPercentage; _this.addDecimalOpacity(o); }); _defineProperty(this, "getRed", function () { return _this.r; }); _defineProperty(this, "getGreen", function () { return _this.g; }); _defineProperty(this, "getBlue", function () { return _this.b; }); _defineProperty(this, "getCyan", function () { var _ParserUtil$parseRGBA = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()), _ParserUtil$parseRGBA2 = _slicedToArray(_ParserUtil$parseRGBA, 4), c = _ParserUtil$parseRGBA2[0], m = _ParserUtil$parseRGBA2[1], y = _ParserUtil$parseRGBA2[2], k = _ParserUtil$parseRGBA2[3]; return c; }); _defineProperty(this, "getYellow", function () { var _ParserUtil$parseRGBA3 = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()), _ParserUtil$parseRGBA4 = _slicedToArray(_ParserUtil$parseRGBA3, 4), c = _ParserUtil$parseRGBA4[0], m = _ParserUtil$parseRGBA4[1], y = _ParserUtil$parseRGBA4[2], k = _ParserUtil$parseRGBA4[3]; return y; }); _defineProperty(this, "getMagenta", function () { var _ParserUtil$parseRGBA5 = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()), _ParserUtil$parseRGBA6 = _slicedToArray(_ParserUtil$parseRGBA5, 4), c = _ParserUtil$parseRGBA6[0], m = _ParserUtil$parseRGBA6[1], y = _ParserUtil$parseRGBA6[2], k = _ParserUtil$parseRGBA6[3]; return m; }); _defineProperty(this, "getBlack", function () { var _ParserUtil$parseRGBA7 = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()), _ParserUtil$parseRGBA8 = _slicedToArray(_ParserUtil$parseRGBA7, 4), c = _ParserUtil$parseRGBA8[0], m = _ParserUtil$parseRGBA8[1], y = _ParserUtil$parseRGBA8[2], k = _ParserUtil$parseRGBA8[3]; return k; }); _defineProperty(this, "getHue", function () { return _GenericsUtil["default"].getHueFromRGBA(_this.getRed(), _this.getGreen(), _this.getBlue()); }); _defineProperty(this, "getSaturation", function () { return _GenericsUtil["default"].getSaturationFromRGBA(_this.getRed(), _this.getGreen(), _this.getBlue()); }); _defineProperty(this, "getLightness", function () { return _GenericsUtil["default"].getLuminosityFromRGBA(_this.getRed(), _this.getGreen(), _this.getBlue()); }); _defineProperty(this, "getValue", function () { return _GenericsUtil["default"].getValueFromRGBA(_this.getRed(), _this.getGreen(), _this.getBlue()); }); _defineProperty(this, "getDecimalOpacity", function () { return _this.a; }); _defineProperty(this, "getIntOpacity", function () { return _this.getDecimalOpacity() * _GenericsUtil["default"].maxIntPercentage; }); _defineProperty(this, "setRed", function (r) { if (!r && r !== 0) return; _this.r = r && r < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : r && r > _GenericsUtil["default"].maxRGB ? _GenericsUtil["default"].maxRGB : _GenericsUtil["default"].roundNumberToInfinity(r, _this.precision.rgb); }); _defineProperty(this, "setGreen", function (g) { if (!g && g !== 0) return; _this.g = g && g < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : g && g > _GenericsUtil["default"].maxRGB ? _GenericsUtil["default"].maxRGB : _GenericsUtil["default"].roundNumberToInfinity(g, _this.precision.rgb); }); _defineProperty(this, "setBlue", function (b) { if (!b && b !== 0) return; _this.b = b && b < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : b && b > _GenericsUtil["default"].maxRGB ? _GenericsUtil["default"].maxRGB : _GenericsUtil["default"].roundNumberToInfinity(b, _this.precision.rgb); }); _defineProperty(this, "setCyan", function (c) { if (!c && c !== 0) return; var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); cmyk[0] = c && c < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : c && c > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : c; var rgb = _ParserUtil["default"].parseCMYKToRGBA.apply(_ParserUtil["default"], _toConsumableArray(cmyk)); if (!rgb) return; var _rgb = _slicedToArray(rgb, 3), r = _rgb[0], g = _rgb[1], b = _rgb[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setYellow", function (y) { if (!y && y !== 0) return; var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); cmyk[2] = y && y < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : y && y > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : y; var rgb = _ParserUtil["default"].parseCMYKToRGBA.apply(_ParserUtil["default"], _toConsumableArray(cmyk)); if (!rgb) return; var _rgb2 = _slicedToArray(rgb, 3), r = _rgb2[0], g = _rgb2[1], b = _rgb2[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setMagenta", function (m) { if (!m && m !== 0) return; var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); cmyk[1] = m && m < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : m && m > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : m; var rgb = _ParserUtil["default"].parseCMYKToRGBA.apply(_ParserUtil["default"], _toConsumableArray(cmyk)); if (!rgb) return; var _rgb3 = _slicedToArray(rgb, 3), r = _rgb3[0], g = _rgb3[1], b = _rgb3[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setBlack", function (k) { if (!k && k !== 0) return; var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()); cmyk[3] = k && k < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : k && k > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : k; var rgb = _ParserUtil["default"].parseCMYKToRGBA.apply(_ParserUtil["default"], _toConsumableArray(cmyk)); if (!rgb) return; var _rgb4 = _slicedToArray(rgb, 3), r = _rgb4[0], g = _rgb4[1], b = _rgb4[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setHue", function (h) { if (!h && h !== 0) return; var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); hsl[0] = h && h < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : h && h > _GenericsUtil["default"].maxHue ? _GenericsUtil["default"].maxHue : h; var rgb = _ParserUtil["default"].parseHSLToRGBA.apply(_ParserUtil["default"], _toConsumableArray(hsl)); if (!rgb) return; var _rgb5 = _slicedToArray(rgb, 3), r = _rgb5[0], g = _rgb5[1], b = _rgb5[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setSaturation", function (s) { if (!s && s !== 0) return; var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); hsl[1] = s && s < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : s && s > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : s; var rgb = _ParserUtil["default"].parseHSLToRGBA.apply(_ParserUtil["default"], _toConsumableArray(hsl)); if (!rgb) return; var _rgb6 = _slicedToArray(rgb, 3), r = _rgb6[0], g = _rgb6[1], b = _rgb6[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setLightness", function (l) { if (!l && l !== 0) return; var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()); hsl[2] = l && l < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : l && l > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : l; var rgb = _ParserUtil["default"].parseHSLToRGBA.apply(_ParserUtil["default"], _toConsumableArray(hsl)); if (!rgb) return; var _rgb7 = _slicedToArray(rgb, 3), r = _rgb7[0], g = _rgb7[1], b = _rgb7[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setValue", function (v) { if (!v && v !== 0) return; var hsv = _ParserUtil["default"].parseRGBAToHSV(_this.getRed(), _this.getGreen(), _this.getBlue()); hsv[2] = v && v < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : v && v > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : v; var rgb = _ParserUtil["default"].parseHSVToRGBA.apply(_ParserUtil["default"], _toConsumableArray(hsv)); if (!rgb) return; var _rgb8 = _slicedToArray(rgb, 3), r = _rgb8[0], g = _rgb8[1], b = _rgb8[2]; _this.setRed(r); _this.setGreen(g); _this.setBlue(b); }); _defineProperty(this, "setDecimalOpacity", function (o) { if (!o && o !== 0) return; _this.a = o && o < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : o && o > _GenericsUtil["default"].maxDecimalPercentage ? _GenericsUtil["default"].maxDecimalPercentage : _GenericsUtil["default"].roundNumberToInfinity(o, _this.precision.opacity); }); _defineProperty(this, "setIntOpacity", function (o) { if (!o && o !== 0) return; o = o && o < _GenericsUtil["default"].zero ? _GenericsUtil["default"].zero : o && o > _GenericsUtil["default"].maxIntPercentage ? _GenericsUtil["default"].maxIntPercentage : o; _this.setDecimalOpacity(o); }); _defineProperty(this, "toHexIntValue", function () { return _GenericsUtil["default"].convertHexStringToIntValue(_ParserUtil["default"].parseRGBAToHex(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity())); }); _defineProperty(this, "toHexArray", function () { return _GenericsUtil["default"].__convertHexToArray(_ParserUtil["default"].parseRGBAToHex(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity())); }); _defineProperty(this, "toHexCSSString", function () { var hex = _ParserUtil["default"].parseRGBAToHex(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()); return _CSSUtil["default"].toHexCSSString(hex); }); _defineProperty(this, "toRGBArray", function () { return [_this.getRed(), _this.getGreen(), _this.getBlue()]; }); _defineProperty(this, "toRGBCSSString", function () { return _CSSUtil["default"].toRGBCSSString(_this.getRed(), _this.getGreen(), _this.getBlue()); }); _defineProperty(this, "toRGBAArray", function () { return [_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()]; }); _defineProperty(this, "toRGBACSSString", function () { return _CSSUtil["default"].toRGBACSSString(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()); }); _defineProperty(this, "toCMYKArray", function () { return _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e) { return _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.cmyk); }); }); _defineProperty(this, "toCMYKCSSString", function () { var cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e) { return _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.cmyk); }); return _CSSUtil["default"].toCMYKCSSString.apply(_CSSUtil["default"], _toConsumableArray(cmyk)); }); _defineProperty(this, "toHSLArray", function () { return _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e, i) { return i === 0 ? _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsl - 2) : _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsl); }); }); _defineProperty(this, "toHSLCSSString", function () { var hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e, i) { return i === 0 ? _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsl - 2) : _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsl); }); return _CSSUtil["default"].toHSLCSSString.apply(_CSSUtil["default"], _toConsumableArray(hsl)); }); _defineProperty(this, "toHSVArray", function () { return _ParserUtil["default"].parseRGBAToHSV(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e, i) { return i === 0 ? _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsv - 2) : _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsv); }); }); _defineProperty(this, "toHSVCSSString", function () { var hsv = _ParserUtil["default"].parseRGBAToHSV(_this.getRed(), _this.getGreen(), _this.getBlue()).map(function (e, i) { return i === 0 ? _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsv - 2) : _GenericsUtil["default"].roundNumberToInfinity(e, _this.precision.hsv); }); return _CSSUtil["default"].toHSVCSSString.apply(_CSSUtil["default"], _toConsumableArray(hsv)); }); _defineProperty(this, "toOpacityCSSString", function () { return _this.getDecimalOpacity(); }); _defineProperty(this, "copy", function () { return new Color(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()); }); _defineProperty(this, "toString", function () { var hex = _ParserUtil["default"].parseRGBAToHex(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()), cmyk = _ParserUtil["default"].parseRGBAToCMYK(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()), hsl = _ParserUtil["default"].parseRGBAToHSL(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()), hsv = _ParserUtil["default"].parseRGBAToHSV(_this.getRed(), _this.getGreen(), _this.getBlue(), _this.getDecimalOpacity()); return "{hex: ".concat(hex, ", rgb: [").concat(_this.getRed(), ",\n ").concat(_this.getGreen(), ",\n ").concat(_this.getBlue(), "], rgba: [").concat(_this.getRed(), ",\n ").concat(_this.getGreen(), ",\n ").concat(_this.getBlue(), ", ").concat(_this.getDecimalOpacity(), "], cmyk: ").concat(cmyk, ", hsl: ").concat(hsl, ", hsv: ").concat(hsv, "}"); }); var rgba = [_r, _g, _b, a]; var errMsg = "The given values ".concat(rgba, " are not valid color values. Read the documentation do find out more."); if (!_GenericsUtil["default"].isRGBAArray(rgba)) throw new Error(errMsg); this.r = _GenericsUtil["default"].roundNumberToInfinity(_r, this.precision.rgb); this.g = _GenericsUtil["default"].roundNumberToInfinity(_g, this.precision.rgb); this.b = _GenericsUtil["default"].roundNumberToInfinity(_b, this.precision.rgb); this.a = _GenericsUtil["default"].roundNumberToInfinity(a, this.precision.rgb); } /** * Adders */ //TODO: Unsure if there's a need for those //addHexRed = (hex) => {}; //addHexGreen = (hex) => {}; //addHexBlue = (hex) => {}; //rgb ); var _default = Color; exports["default"] = _default;