Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

colorizr

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

colorizr - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

386

es/index.js

@@ -1,18 +0,23 @@

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"); } }; }();
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _iterableToArrayLimit(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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
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; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import { expr, hasProperty, isPlainObject, isRequired, pick, round, validateHex } from './utils';
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); return Constructor; }
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; }
import { expr, isPlainObject, isRequired, pick, round, validateHex } from './utils';
var RGB = ['r', 'g', 'b'];
var HSL = ['h', 's', 'l'];
export { validateHex };
/**

@@ -23,4 +28,5 @@ * Colorizr

var Colorizr = function () {
var Colorizr =
/*#__PURE__*/
function () {
/**

@@ -33,17 +39,18 @@ * @constructs Colorizr

this.hex = '';
this.hsl = {
_defineProperty(this, "hex", '');
_defineProperty(this, "hsl", {
h: 0,
s: 0,
l: 0
};
this.rgb = {
});
_defineProperty(this, "rgb", {
r: 0,
g: 0,
b: 0
};
});
this.setColor(color);
}
/**

@@ -57,3 +64,3 @@ * Set the instance color.

_createClass(Colorizr, [{
key: 'setColor',
key: "setColor",
value: function setColor(color) {

@@ -74,3 +81,2 @@ if (!color) return;

}
/**

@@ -84,8 +90,6 @@ * Set color from an array.

}, {
key: 'setColorFromArray',
key: "setColorFromArray",
value: function setColorFromArray() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
if (!Array.isArray(input)) throw new Error('input is not an array');
this.rgb = {

@@ -96,7 +100,5 @@ r: this.limit(input[0], 'r'),

};
this.hex = this.rgb2hex();
this.hsl = this.rgb2hsl();
}
/**

@@ -110,9 +112,8 @@ * Set color from an object.

}, {
key: 'setColorFromObject',
key: "setColorFromObject",
value: function setColorFromObject() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
if (!isPlainObject(input)) throw new Error('input is not an object');
if (hasProperty(input, 'h') && hasProperty(input, 's') && hasProperty(input, 'l')) {
if (this.isHSL(input, true)) {
this.hsl = {

@@ -124,3 +125,3 @@ h: this.limit(input.h, 'h'),

this.rgb = this.hsl2rgb();
} else if (hasProperty(input, 'r') && hasProperty(input, 'g') && hasProperty(input, 'b')) {
} else if (this.isRGB(input, true)) {
this.rgb = input;

@@ -134,5 +135,4 @@ this.hsl = this.rgb2hsl();

}
/**
* Make the color lighter.
* Increase lightness.
*

@@ -144,15 +144,12 @@ * @param {number} percentage

}, {
key: 'lighten',
key: "lighten",
value: function lighten() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
l: this.constrain(this.lightness, percentage, [0, 100], '+')
});
return this.hsl2hex(hsl);
}
/**
* Make the color darker.
* Decrease lightness.
*

@@ -164,13 +161,10 @@ * @param {number} percentage

}, {
key: 'darken',
key: "darken",
value: function darken() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
l: this.constrain(this.lightness, percentage, [0, 100], '-')
});
return this.hsl2hex(hsl);
}
/**

@@ -184,13 +178,10 @@ * Increase saturation.

}, {
key: 'saturate',
key: "saturate",
value: function saturate() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
s: this.constrain(this.saturation, percentage, [0, 100], '+')
});
return this.hsl2hex(hsl);
}
/**

@@ -204,13 +195,10 @@ * Decrease saturation.

}, {
key: 'desaturate',
key: "desaturate",
value: function desaturate() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
s: this.constrain(this.saturation, percentage, [0, 100], '-')
});
return this.hsl2hex(hsl);
}
/**

@@ -224,13 +212,10 @@ * Adjust the color hue.

}, {
key: 'adjustHue',
value: function adjustHue() {
key: "rotate",
value: function rotate() {
var degrees = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 15;
var hsl = this.shift({
h: this.constrainDegrees(this.hue, +degrees)
});
return this.hsl2hex(hsl);
}
/**

@@ -246,7 +231,6 @@ * Alter color values.

}, {
key: 'remix',
key: "remix",
value: function remix() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
var returnHex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var shift = this.shift(input);

@@ -260,3 +244,2 @@

}
/**

@@ -269,3 +252,3 @@ * Generate a random color.

}, {
key: 'random',
key: "random",
value: function random() {

@@ -277,3 +260,2 @@ var hsl = {

};
return {

@@ -285,3 +267,2 @@ hex: this.hsl2hex(hsl),

}
/**

@@ -295,17 +276,51 @@ * Get the contrasted color for a given hex.

}, {
key: 'textColor',
key: "textColor",
value: function textColor() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var _hex2rgb = this.hex2rgb(input),
r = _hex2rgb.r,
g = _hex2rgb.g,
b = _hex2rgb.b;
var _this$hex2rgb = this.hex2rgb(input),
r = _this$hex2rgb.r,
g = _this$hex2rgb.g,
b = _this$hex2rgb.b;
var yiq = (r * 299 + g * 587 + b * 114) / 1000;
return yiq >= 128 ? '#000' : '#fff';
}
/**
* Get the brightness difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: "getBrightnessDifference",
value: function getBrightnessDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
var rightY = (RGBRight.r * 299 + RGBRight.g * 587 + RGBRight.b * 114) / 1000;
var leftY = (RGBLeft.r * 299 + RGBLeft.g * 587 + RGBLeft.b * 114) / 1000;
return round(Math.abs(rightY - leftY), 4);
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: "getColorDifference",
value: function getColorDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
return Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r) + (Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g)) + (Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Test 2 colors for compliance.

@@ -319,7 +334,6 @@ *

}, {
key: 'checkContrast',
value: function checkContrast() {
key: "compare",
value: function compare() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var LuminanceLeft = this.getLuminance(left);

@@ -340,6 +354,4 @@ var LuminanceRight = this.getLuminance(right);

ratio = round(ratio, 4);
var isBright = brightnessDifference >= brightnessThreshold;
var hasEnoughDifference = colorDifference >= colorThreshold;
var compliant = 0;

@@ -357,3 +369,3 @@

compliant: compliant,
contrastRatio: ratio,
contrast: ratio,
w2a: ratio >= 3,

@@ -365,46 +377,19 @@ w2aaab: ratio >= 7,

}
/**
* Get the brightness difference between 2 colors.
* Get contrast ratio.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
* @returns {Object}
*/
}, {
key: 'getBrightnessDifference',
value: function getBrightnessDifference() {
key: "contrast",
value: function contrast() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
var rightY = (RGBRight.r * 299 + RGBRight.g * 587 + RGBRight.b * 114) / 1000;
var leftY = (RGBLeft.r * 299 + RGBLeft.g * 587 + RGBLeft.b * 114) / 1000;
return round(Math.abs(rightY - leftY), 4);
var compare = this.compare(left, right);
return compare.contrast;
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: 'getColorDifference',
value: function getColorDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
return Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r) + (Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g)) + (Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Get the luminance of a color.

@@ -417,8 +402,6 @@ *

}, {
key: 'getLuminance',
key: "getLuminance",
value: function getLuminance() {
var hex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var rgb = this.hex2rgb(hex);
rgb = [rgb.r / 255, rgb.g / 255, rgb.b / 255];

@@ -436,3 +419,2 @@

}
/**

@@ -447,6 +429,5 @@ * Parse HEX color.

}, {
key: 'parseHex',
key: "parseHex",
value: function parseHex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
var color = input.replace('#', '');

@@ -457,3 +438,2 @@ var hex = color;

hex = '';
color.split('').forEach(function (d) {

@@ -464,3 +444,3 @@ hex += d + d;

hex = '#' + hex;
hex = "#".concat(hex);

@@ -473,3 +453,2 @@ if (!validateHex(hex)) {

}
/**

@@ -483,8 +462,7 @@ * Parse CSS attribute.

}, {
key: 'parseCSS',
key: "parseCSS",
value: function parseCSS() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
var model = /^rgb/.test(input) ? 'rgba' : 'hsla';
var regex = new RegExp('^' + model + '?[\\s+]?\\([\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?', 'i');
var regex = new RegExp("^".concat(model, "?[\\s+]?\\([\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?"), 'i');
var matches = input.match(regex);

@@ -500,3 +478,2 @@

}
/**

@@ -510,8 +487,6 @@ * Convert a hex string to RGB object.

}, {
key: 'hex2rgb',
key: "hex2rgb",
value: function hex2rgb() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var hex = this.parseHex(input).substr(1);
return {

@@ -523,3 +498,2 @@ r: parseInt(String(hex.charAt(0)) + hex.charAt(1), 16),

}
/**

@@ -533,9 +507,7 @@ * Convert a hex string to HSL object.

}, {
key: 'hex2hsl',
key: "hex2hsl",
value: function hex2hsl() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
return this.rgb2hsl(this.hex2rgb(input));
}
/**

@@ -549,9 +521,8 @@ * Convert a RGB object to HSL.

}, {
key: 'rgb2hsl',
key: "rgb2hsl",
value: function rgb2hsl() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.rgb;
var rgb = typeof input === 'string' ? this.parseCSS(input) : input;
if (!hasProperty(rgb, 'r') || !hasProperty(rgb, 'g') || !hasProperty(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -563,11 +534,9 @@ }

var b = this.limit(rgb.b, 'b') / 255;
var min = Math.min(r, g, b);
var max = Math.max(r, g, b);
var delta = max - min;
var h = 0;
var s = void 0;
var l = void 0;
var rate = void 0;
var s;
var l;
var rate;

@@ -579,2 +548,3 @@ switch (max) {

break;
case g:

@@ -584,2 +554,3 @@ rate = (b - r) / delta;

break;
case b:

@@ -589,3 +560,5 @@ rate = (r - g) / delta;

break;
/* istanbul ignore next */
default:

@@ -613,3 +586,2 @@ break;

}
/**

@@ -624,9 +596,8 @@ * Convert a RGA object to hex.

}, {
key: 'rgb2hex',
key: "rgb2hex",
value: function rgb2hex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.rgb;
var rgb = typeof input === 'string' ? this.parseCSS(input) : input;
if (!hasProperty(rgb, 'r') || !hasProperty(rgb, 'g') || !hasProperty(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -636,6 +607,4 @@ }

var hex = (1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b;
return '#' + hex.toString(16).slice(1);
return "#".concat(hex.toString(16).slice(1));
}
/**

@@ -649,9 +618,8 @@ * Convert a HSL object to RGB.

}, {
key: 'hsl2rgb',
key: "hsl2rgb",
value: function hsl2rgb() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hsl;
var hsl = typeof input === 'string' ? this.parseCSS(input) : input;
if (!hasProperty(hsl, 'h') || !hasProperty(hsl, 's') || !hasProperty(hsl, 'l')) {
if (!this.isHSL(hsl, true)) {
throw new Error('Invalid object');

@@ -663,10 +631,8 @@ }

var l = round(hsl.l) / 100;
var r;
var g;
var b;
var point;
var chroma;
var r = void 0;
var g = void 0;
var b = void 0;
var point = void 0;
var chroma = void 0;
if (s === 0) {

@@ -679,3 +645,2 @@ r = l;

point = 2 * l - chroma;
r = this.hue2rgb(point, chroma, h + 1 / 3);

@@ -692,3 +657,2 @@ g = this.hue2rgb(point, chroma, h);

}
/**

@@ -702,11 +666,8 @@ * Convert a HSL object to HEX.

}, {
key: 'hsl2hex',
key: "hsl2hex",
value: function hsl2hex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hsl;
var hsl = typeof input === 'string' ? this.parseCSS(input) : input;
return this.rgb2hex(this.hsl2rgb(hsl));
}
/**

@@ -724,3 +685,3 @@ * Convert hue to RGB using chroma and median point

}, {
key: 'hue2rgb',
key: "hue2rgb",
value: function hue2rgb() {

@@ -730,3 +691,2 @@ var point = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('point');

var h = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : isRequired('hue');
var hue = h;

@@ -756,3 +716,2 @@

}
/**

@@ -767,3 +726,3 @@ * Get a shifted color object with the same model of the input.

}, {
key: 'shift',
key: "shift",
value: function shift() {

@@ -789,12 +748,9 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');

var hsl = pick(input, HSL);
return _objectSpread({}, this.hsl, hsl);
} // Not HSL so it must be RGB
return _extends({}, this.hsl, hsl);
}
// Not HSL so it must be RGB
var rgb = pick(input, RGB);
return _extends({}, this.rgb, rgb);
return _objectSpread({}, this.rgb, rgb);
}
/**

@@ -810,3 +766,3 @@ * Limit values per type.

}, {
key: 'limit',
key: "limit",
value: function limit() {

@@ -819,9 +775,14 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');

}
/* istanbul ignore else */
/* istanbul ignore else */
if (RGB.indexOf(type) !== -1) {
return Math.max(Math.min(input, 255), 0);
} else if (['s', 'l'].indexOf(type) !== -1) {
}
if (['s', 'l'].indexOf(type) !== -1) {
return Math.max(Math.min(input, 100), 0);
} else if (type === 'h') {
}
if (type === 'h') {
return Math.max(Math.min(input, 360), 0);

@@ -832,3 +793,2 @@ }

}
/**

@@ -846,3 +806,3 @@ * Constrain value into the range

}, {
key: 'constrain',
key: "constrain",
value: function constrain() {

@@ -868,3 +828,2 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');

}
/**

@@ -880,7 +839,6 @@ * Constrain an angle

}, {
key: 'constrainDegrees',
key: "constrainDegrees",
value: function constrainDegrees() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('input');
var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : isRequired('amount');
var value = input + amount;

@@ -898,3 +856,2 @@

}
/**

@@ -904,2 +861,4 @@ * Check if an object contains HSL values.

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}

@@ -909,9 +868,17 @@ */

}, {
key: 'isHSL',
key: "isHSL",
value: function isHSL(input) {
return Object.keys(input).some(function (d) {
var all = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var keys = Object.keys(input);
if (all) {
return keys.every(function (d) {
return HSL.indexOf(d) !== -1;
});
}
return keys.some(function (d) {
return HSL.indexOf(d) !== -1;
});
}
/**

@@ -921,2 +888,4 @@ * Check if an object contains RGB values.

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}

@@ -926,10 +895,20 @@ */

}, {
key: 'isRGB',
key: "isRGB",
value: function isRGB(input) {
return Object.keys(input).some(function (d) {
var all = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var keys = Object.keys(input);
if (all) {
return keys.every(function (d) {
return RGB.indexOf(d) !== -1;
});
}
return keys.some(function (d) {
return RGB.indexOf(d) !== -1;
});
}
/**
* Get red value.
*
* @type {number}

@@ -939,8 +918,9 @@ */

}, {
key: 'red',
key: "red",
get: function get() {
return Number(this.rgb.r);
}
/**
* Get green value.
*
* @type {number}

@@ -950,8 +930,9 @@ */

}, {
key: 'green',
key: "green",
get: function get() {
return Number(this.rgb.g);
}
/**
* Get blue value.
*
* @type {number}

@@ -961,8 +942,9 @@ */

}, {
key: 'blue',
key: "blue",
get: function get() {
return Number(this.rgb.b);
}
/**
* Get hue value.
*
* @type {number}

@@ -972,8 +954,9 @@ */

}, {
key: 'hue',
key: "hue",
get: function get() {
return Number(this.hsl.h);
}
/**
* Get saturation value,
*
* @type {number}

@@ -983,8 +966,9 @@ */

}, {
key: 'saturation',
key: "saturation",
get: function get() {
return Number(this.hsl.s);
}
/**
* Get lightness value.
*
* @type {number}

@@ -994,6 +978,28 @@ */

}, {
key: 'lightness',
key: "lightness",
get: function get() {
return Number(this.hsl.l);
}
/**
* Get luminance value.
*
* @type {number}
*/
}, {
key: "luminance",
get: function get() {
return this.getLuminance();
}
}, {
key: "chroma",
get: function get() {
var _this$rgb = this.rgb,
r = _this$rgb.r,
g = _this$rgb.g,
b = _this$rgb.b;
var max = Math.max(r, g, b);
var min = Math.min(r, g, b);
return round((max - min) / 255, 4);
}
}]);

@@ -1004,2 +1010,2 @@

export default Colorizr;
export { Colorizr as default };

@@ -0,3 +1,6 @@

/**
* Colorizr Utils
* @module Utils
*/
/**

@@ -15,10 +18,7 @@ * Parse math string expressions.

var op = [];
var parsed = void 0;
var parsed;
var index = 0;
var last = true;
n[index] = ''; // Parse the string
n[index] = '';
// Parse the string
for (var c = 0; c < chars.length; c++) {

@@ -34,6 +34,7 @@ if (isNaN(parseInt(chars[c], 10)) && chars[c] !== '.' && !last) {

}
}
} // Calculate the expression
// Calculate the expression
parsed = parseFloat(n[0]);
for (var o = 0; o < op.length; o++) {

@@ -46,11 +47,15 @@ var num = parseFloat(n[o + 1]);

break;
case '-':
parsed -= num;
break;
case '*':
parsed *= num;
break;
case '/':
parsed /= num;
break;
default:

@@ -63,36 +68,18 @@ break;

}
/**
* Check if an object has a property.
* Check if the input is an object
*
* @param {*} input
* @param {string} prop
* @returns {boolean}
*/
export function hasProperty(input, prop) {
if (!input || !prop) return false;
return Object.prototype.hasOwnProperty.call(input, prop);
}
/**
* Check if the input is an object
*
* @param {*} input
* @returns {boolean}
*/
export function isPlainObject(input) {
if (!input) return false;
var toString = Object.prototype.toString;
var prototype = Object.getPrototypeOf(input);
return toString.call(input) === '[object Object]' && (prototype === null || prototype === Object.getPrototypeOf({}));
}
export function isRequired(name) {
throw new Error((name || 'parameter') + ' is required');
throw new Error("".concat(name || 'parameter', " is required"));
}
/**

@@ -106,2 +93,3 @@ * Creates an object composed of the picked source properties.

*/
export function pick(input, options) {

@@ -119,3 +107,2 @@ if (!Array.isArray(options)) {

}
/**

@@ -130,12 +117,10 @@ * Round decimal numbers.

*/
export function round(input) {
var digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
var factor = Math.pow(10, digits);
return Math.round(input * factor) / factor;
}
export function validateHex(input) {
return (/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(input)
);
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(input);
}

@@ -1,23 +0,31 @@

'use strict';
"use strict";
exports.__esModule = true;
exports.validateHex = undefined;
exports.default = void 0;
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"); } }; }();
var _utils = require("./utils");
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.validateHex = _utils.validateHex;
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
var _utils = require('./utils');
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
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; }
function _iterableToArrayLimit(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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); return Constructor; }
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 RGB = ['r', 'g', 'b'];
var HSL = ['h', 's', 'l'];
exports.validateHex = _utils.validateHex;
/**

@@ -27,5 +35,5 @@ * Colorizr

*/
var Colorizr = function () {
var Colorizr =
/*#__PURE__*/
function () {
/**

@@ -38,17 +46,18 @@ * @constructs Colorizr

this.hex = '';
this.hsl = {
_defineProperty(this, "hex", '');
_defineProperty(this, "hsl", {
h: 0,
s: 0,
l: 0
};
this.rgb = {
});
_defineProperty(this, "rgb", {
r: 0,
g: 0,
b: 0
};
});
this.setColor(color);
}
/**

@@ -62,3 +71,3 @@ * Set the instance color.

_createClass(Colorizr, [{
key: 'setColor',
key: "setColor",
value: function setColor(color) {

@@ -79,3 +88,2 @@ if (!color) return;

}
/**

@@ -89,8 +97,6 @@ * Set color from an array.

}, {
key: 'setColorFromArray',
key: "setColorFromArray",
value: function setColorFromArray() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
if (!Array.isArray(input)) throw new Error('input is not an array');
this.rgb = {

@@ -101,7 +107,5 @@ r: this.limit(input[0], 'r'),

};
this.hex = this.rgb2hex();
this.hsl = this.rgb2hsl();
}
/**

@@ -115,9 +119,8 @@ * Set color from an object.

}, {
key: 'setColorFromObject',
key: "setColorFromObject",
value: function setColorFromObject() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
if (!(0, _utils.isPlainObject)(input)) throw new Error('input is not an object');
if ((0, _utils.hasProperty)(input, 'h') && (0, _utils.hasProperty)(input, 's') && (0, _utils.hasProperty)(input, 'l')) {
if (this.isHSL(input, true)) {
this.hsl = {

@@ -129,3 +132,3 @@ h: this.limit(input.h, 'h'),

this.rgb = this.hsl2rgb();
} else if ((0, _utils.hasProperty)(input, 'r') && (0, _utils.hasProperty)(input, 'g') && (0, _utils.hasProperty)(input, 'b')) {
} else if (this.isRGB(input, true)) {
this.rgb = input;

@@ -139,5 +142,4 @@ this.hsl = this.rgb2hsl();

}
/**
* Make the color lighter.
* Increase lightness.
*

@@ -149,15 +151,12 @@ * @param {number} percentage

}, {
key: 'lighten',
key: "lighten",
value: function lighten() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
l: this.constrain(this.lightness, percentage, [0, 100], '+')
});
return this.hsl2hex(hsl);
}
/**
* Make the color darker.
* Decrease lightness.
*

@@ -169,13 +168,10 @@ * @param {number} percentage

}, {
key: 'darken',
key: "darken",
value: function darken() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
l: this.constrain(this.lightness, percentage, [0, 100], '-')
});
return this.hsl2hex(hsl);
}
/**

@@ -189,13 +185,10 @@ * Increase saturation.

}, {
key: 'saturate',
key: "saturate",
value: function saturate() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
s: this.constrain(this.saturation, percentage, [0, 100], '+')
});
return this.hsl2hex(hsl);
}
/**

@@ -209,13 +202,10 @@ * Decrease saturation.

}, {
key: 'desaturate',
key: "desaturate",
value: function desaturate() {
var percentage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
var hsl = this.shift({
s: this.constrain(this.saturation, percentage, [0, 100], '-')
});
return this.hsl2hex(hsl);
}
/**

@@ -229,13 +219,10 @@ * Adjust the color hue.

}, {
key: 'adjustHue',
value: function adjustHue() {
key: "rotate",
value: function rotate() {
var degrees = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 15;
var hsl = this.shift({
h: this.constrainDegrees(this.hue, +degrees)
});
return this.hsl2hex(hsl);
}
/**

@@ -251,7 +238,6 @@ * Alter color values.

}, {
key: 'remix',
key: "remix",
value: function remix() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
var returnHex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var shift = this.shift(input);

@@ -265,3 +251,2 @@

}
/**

@@ -274,3 +259,3 @@ * Generate a random color.

}, {
key: 'random',
key: "random",
value: function random() {

@@ -282,3 +267,2 @@ var hsl = {

};
return {

@@ -290,3 +274,2 @@ hex: this.hsl2hex(hsl),

}
/**

@@ -300,17 +283,51 @@ * Get the contrasted color for a given hex.

}, {
key: 'textColor',
key: "textColor",
value: function textColor() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var _hex2rgb = this.hex2rgb(input),
r = _hex2rgb.r,
g = _hex2rgb.g,
b = _hex2rgb.b;
var _this$hex2rgb = this.hex2rgb(input),
r = _this$hex2rgb.r,
g = _this$hex2rgb.g,
b = _this$hex2rgb.b;
var yiq = (r * 299 + g * 587 + b * 114) / 1000;
return yiq >= 128 ? '#000' : '#fff';
}
/**
* Get the brightness difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: "getBrightnessDifference",
value: function getBrightnessDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
var rightY = (RGBRight.r * 299 + RGBRight.g * 587 + RGBRight.b * 114) / 1000;
var leftY = (RGBLeft.r * 299 + RGBLeft.g * 587 + RGBLeft.b * 114) / 1000;
return (0, _utils.round)(Math.abs(rightY - leftY), 4);
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: "getColorDifference",
value: function getColorDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
return Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r) + (Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g)) + (Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Test 2 colors for compliance.

@@ -324,7 +341,6 @@ *

}, {
key: 'checkContrast',
value: function checkContrast() {
key: "compare",
value: function compare() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var LuminanceLeft = this.getLuminance(left);

@@ -345,6 +361,4 @@ var LuminanceRight = this.getLuminance(right);

ratio = (0, _utils.round)(ratio, 4);
var isBright = brightnessDifference >= brightnessThreshold;
var hasEnoughDifference = colorDifference >= colorThreshold;
var compliant = 0;

@@ -362,3 +376,3 @@

compliant: compliant,
contrastRatio: ratio,
contrast: ratio,
w2a: ratio >= 3,

@@ -370,46 +384,19 @@ w2aaab: ratio >= 7,

}
/**
* Get the brightness difference between 2 colors.
* Get contrast ratio.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
* @returns {Object}
*/
}, {
key: 'getBrightnessDifference',
value: function getBrightnessDifference() {
key: "contrast",
value: function contrast() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
var rightY = (RGBRight.r * 299 + RGBRight.g * 587 + RGBRight.b * 114) / 1000;
var leftY = (RGBLeft.r * 299 + RGBLeft.g * 587 + RGBLeft.b * 114) / 1000;
return (0, _utils.round)(Math.abs(rightY - leftY), 4);
var compare = this.compare(left, right);
return compare.contrast;
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
}, {
key: 'getColorDifference',
value: function getColorDifference() {
var left = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('left');
var right = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.hex;
var RGBLeft = this.hex2rgb(left);
var RGBRight = this.hex2rgb(right);
return Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r) + (Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g)) + (Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Get the luminance of a color.

@@ -422,8 +409,6 @@ *

}, {
key: 'getLuminance',
key: "getLuminance",
value: function getLuminance() {
var hex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var rgb = this.hex2rgb(hex);
rgb = [rgb.r / 255, rgb.g / 255, rgb.b / 255];

@@ -441,3 +426,2 @@

}
/**

@@ -452,6 +436,5 @@ * Parse HEX color.

}, {
key: 'parseHex',
key: "parseHex",
value: function parseHex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
var color = input.replace('#', '');

@@ -462,3 +445,2 @@ var hex = color;

hex = '';
color.split('').forEach(function (d) {

@@ -469,3 +451,3 @@ hex += d + d;

hex = '#' + hex;
hex = "#".concat(hex);

@@ -478,3 +460,2 @@ if (!(0, _utils.validateHex)(hex)) {

}
/**

@@ -488,8 +469,7 @@ * Parse CSS attribute.

}, {
key: 'parseCSS',
key: "parseCSS",
value: function parseCSS() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
var model = /^rgb/.test(input) ? 'rgba' : 'hsla';
var regex = new RegExp('^' + model + '?[\\s+]?\\([\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?', 'i');
var regex = new RegExp("^".concat(model, "?[\\s+]?\\([\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?,[\\s+]?(\\d+)[\\s+]?"), 'i');
var matches = input.match(regex);

@@ -505,3 +485,2 @@

}
/**

@@ -515,8 +494,6 @@ * Convert a hex string to RGB object.

}, {
key: 'hex2rgb',
key: "hex2rgb",
value: function hex2rgb() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
var hex = this.parseHex(input).substr(1);
return {

@@ -528,3 +505,2 @@ r: parseInt(String(hex.charAt(0)) + hex.charAt(1), 16),

}
/**

@@ -538,9 +514,7 @@ * Convert a hex string to HSL object.

}, {
key: 'hex2hsl',
key: "hex2hsl",
value: function hex2hsl() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hex;
return this.rgb2hsl(this.hex2rgb(input));
}
/**

@@ -554,9 +528,8 @@ * Convert a RGB object to HSL.

}, {
key: 'rgb2hsl',
key: "rgb2hsl",
value: function rgb2hsl() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.rgb;
var rgb = typeof input === 'string' ? this.parseCSS(input) : input;
if (!(0, _utils.hasProperty)(rgb, 'r') || !(0, _utils.hasProperty)(rgb, 'g') || !(0, _utils.hasProperty)(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -568,11 +541,9 @@ }

var b = this.limit(rgb.b, 'b') / 255;
var min = Math.min(r, g, b);
var max = Math.max(r, g, b);
var delta = max - min;
var h = 0;
var s = void 0;
var l = void 0;
var rate = void 0;
var s;
var l;
var rate;

@@ -584,2 +555,3 @@ switch (max) {

break;
case g:

@@ -589,2 +561,3 @@ rate = (b - r) / delta;

break;
case b:

@@ -594,3 +567,5 @@ rate = (r - g) / delta;

break;
/* istanbul ignore next */
default:

@@ -618,3 +593,2 @@ break;

}
/**

@@ -629,9 +603,8 @@ * Convert a RGA object to hex.

}, {
key: 'rgb2hex',
key: "rgb2hex",
value: function rgb2hex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.rgb;
var rgb = typeof input === 'string' ? this.parseCSS(input) : input;
if (!(0, _utils.hasProperty)(rgb, 'r') || !(0, _utils.hasProperty)(rgb, 'g') || !(0, _utils.hasProperty)(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -641,6 +614,4 @@ }

var hex = (1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b;
return '#' + hex.toString(16).slice(1);
return "#".concat(hex.toString(16).slice(1));
}
/**

@@ -654,9 +625,8 @@ * Convert a HSL object to RGB.

}, {
key: 'hsl2rgb',
key: "hsl2rgb",
value: function hsl2rgb() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hsl;
var hsl = typeof input === 'string' ? this.parseCSS(input) : input;
if (!(0, _utils.hasProperty)(hsl, 'h') || !(0, _utils.hasProperty)(hsl, 's') || !(0, _utils.hasProperty)(hsl, 'l')) {
if (!this.isHSL(hsl, true)) {
throw new Error('Invalid object');

@@ -668,10 +638,8 @@ }

var l = (0, _utils.round)(hsl.l) / 100;
var r;
var g;
var b;
var point;
var chroma;
var r = void 0;
var g = void 0;
var b = void 0;
var point = void 0;
var chroma = void 0;
if (s === 0) {

@@ -684,3 +652,2 @@ r = l;

point = 2 * l - chroma;
r = this.hue2rgb(point, chroma, h + 1 / 3);

@@ -697,3 +664,2 @@ g = this.hue2rgb(point, chroma, h);

}
/**

@@ -707,11 +673,8 @@ * Convert a HSL object to HEX.

}, {
key: 'hsl2hex',
key: "hsl2hex",
value: function hsl2hex() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.hsl;
var hsl = typeof input === 'string' ? this.parseCSS(input) : input;
return this.rgb2hex(this.hsl2rgb(hsl));
}
/**

@@ -729,3 +692,3 @@ * Convert hue to RGB using chroma and median point

}, {
key: 'hue2rgb',
key: "hue2rgb",
value: function hue2rgb() {

@@ -735,3 +698,2 @@ var point = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('point');

var h = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : (0, _utils.isRequired)('hue');
var hue = h;

@@ -761,3 +723,2 @@

}
/**

@@ -772,3 +733,3 @@ * Get a shifted color object with the same model of the input.

}, {
key: 'shift',
key: "shift",
value: function shift() {

@@ -794,12 +755,9 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');

var hsl = (0, _utils.pick)(input, HSL);
return _objectSpread({}, this.hsl, hsl);
} // Not HSL so it must be RGB
return _extends({}, this.hsl, hsl);
}
// Not HSL so it must be RGB
var rgb = (0, _utils.pick)(input, RGB);
return _extends({}, this.rgb, rgb);
return _objectSpread({}, this.rgb, rgb);
}
/**

@@ -815,3 +773,3 @@ * Limit values per type.

}, {
key: 'limit',
key: "limit",
value: function limit() {

@@ -824,9 +782,14 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');

}
/* istanbul ignore else */
/* istanbul ignore else */
if (RGB.includes(type)) {
return Math.max(Math.min(input, 255), 0);
} else if (['s', 'l'].includes(type)) {
}
if (['s', 'l'].includes(type)) {
return Math.max(Math.min(input, 100), 0);
} else if (type === 'h') {
}
if (type === 'h') {
return Math.max(Math.min(input, 360), 0);

@@ -837,3 +800,2 @@ }

}
/**

@@ -851,3 +813,3 @@ * Constrain value into the range

}, {
key: 'constrain',
key: "constrain",
value: function constrain() {

@@ -873,3 +835,2 @@ var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');

}
/**

@@ -885,7 +846,6 @@ * Constrain an angle

}, {
key: 'constrainDegrees',
key: "constrainDegrees",
value: function constrainDegrees() {
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _utils.isRequired)('input');
var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _utils.isRequired)('amount');
var value = input + amount;

@@ -903,3 +863,2 @@

}
/**

@@ -909,2 +868,4 @@ * Check if an object contains HSL values.

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}

@@ -914,9 +875,17 @@ */

}, {
key: 'isHSL',
key: "isHSL",
value: function isHSL(input) {
return Object.keys(input).some(function (d) {
var all = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var keys = Object.keys(input);
if (all) {
return keys.every(function (d) {
return HSL.includes(d);
});
}
return keys.some(function (d) {
return HSL.includes(d);
});
}
/**

@@ -926,2 +895,4 @@ * Check if an object contains RGB values.

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}

@@ -931,10 +902,20 @@ */

}, {
key: 'isRGB',
key: "isRGB",
value: function isRGB(input) {
return Object.keys(input).some(function (d) {
var all = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var keys = Object.keys(input);
if (all) {
return keys.every(function (d) {
return RGB.includes(d);
});
}
return keys.some(function (d) {
return RGB.includes(d);
});
}
/**
* Get red value.
*
* @type {number}

@@ -944,8 +925,9 @@ */

}, {
key: 'red',
key: "red",
get: function get() {
return Number(this.rgb.r);
}
/**
* Get green value.
*
* @type {number}

@@ -955,8 +937,9 @@ */

}, {
key: 'green',
key: "green",
get: function get() {
return Number(this.rgb.g);
}
/**
* Get blue value.
*
* @type {number}

@@ -966,8 +949,9 @@ */

}, {
key: 'blue',
key: "blue",
get: function get() {
return Number(this.rgb.b);
}
/**
* Get hue value.
*
* @type {number}

@@ -977,8 +961,9 @@ */

}, {
key: 'hue',
key: "hue",
get: function get() {
return Number(this.hsl.h);
}
/**
* Get saturation value,
*
* @type {number}

@@ -988,8 +973,9 @@ */

}, {
key: 'saturation',
key: "saturation",
get: function get() {
return Number(this.hsl.s);
}
/**
* Get lightness value.
*
* @type {number}

@@ -999,6 +985,28 @@ */

}, {
key: 'lightness',
key: "lightness",
get: function get() {
return Number(this.hsl.l);
}
/**
* Get luminance value.
*
* @type {number}
*/
}, {
key: "luminance",
get: function get() {
return this.getLuminance();
}
}, {
key: "chroma",
get: function get() {
var _this$rgb = this.rgb,
r = _this$rgb.r,
g = _this$rgb.g,
b = _this$rgb.b;
var max = Math.max(r, g, b);
var min = Math.min(r, g, b);
return (0, _utils.round)((max - min) / 255, 4);
}
}]);

@@ -1005,0 +1013,0 @@

@@ -1,6 +0,5 @@

'use strict';
"use strict";
exports.__esModule = true;
exports.expr = expr;
exports.hasProperty = hasProperty;
exports.isPlainObject = isPlainObject;

@@ -12,2 +11,6 @@ exports.isRequired = isRequired;

/**
* Colorizr Utils
* @module Utils
*/

@@ -26,10 +29,7 @@ /**

var op = [];
var parsed = void 0;
var parsed;
var index = 0;
var last = true;
n[index] = ''; // Parse the string
n[index] = '';
// Parse the string
for (var c = 0; c < chars.length; c++) {

@@ -45,6 +45,7 @@ if (isNaN(parseInt(chars[c], 10)) && chars[c] !== '.' && !last) {

}
}
} // Calculate the expression
// Calculate the expression
parsed = parseFloat(n[0]);
for (var o = 0; o < op.length; o++) {

@@ -57,11 +58,15 @@ var num = parseFloat(n[o + 1]);

break;
case '-':
parsed -= num;
break;
case '*':
parsed *= num;
break;
case '/':
parsed /= num;
break;
default:

@@ -74,29 +79,14 @@ break;

}
/**
* Check if an object has a property.
* Check if the input is an object
*
* @param {*} input
* @param {string} prop
* @returns {boolean}
*/
function hasProperty(input, prop) {
if (!input || !prop) return false;
return Object.prototype.hasOwnProperty.call(input, prop);
}
/**
* Check if the input is an object
*
* @param {*} input
* @returns {boolean}
*/
function isPlainObject(input) {
if (!input) return false;
var toString = Object.prototype.toString;
var prototype = Object.getPrototypeOf(input);
return toString.call(input) === '[object Object]' && (prototype === null || prototype === Object.getPrototypeOf({}));

@@ -106,5 +96,4 @@ }

function isRequired(name) {
throw new Error((name || 'parameter') + ' is required');
throw new Error("".concat(name || 'parameter', " is required"));
}
/**

@@ -118,2 +107,4 @@ * Creates an object composed of the picked source properties.

*/
function pick(input, options) {

@@ -131,3 +122,2 @@ if (!Array.isArray(options)) {

}
/**

@@ -142,5 +132,6 @@ * Round decimal numbers.

*/
function round(input) {
var digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
var factor = Math.pow(10, digits);

@@ -151,4 +142,3 @@ return Math.round(input * factor) / factor;

function validateHex(input) {
return (/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(input)
);
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(input);
}
{
"name": "colorizr",
"version": "1.0.4",
"version": "1.1.0",
"description": "Manipulate colors like a boss",

@@ -27,20 +27,37 @@ "author": "Gil Barbara <gilbarbara@gmail.com>",

"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^22.1.0",
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/plugin-proposal-decorators": "^7.2.0",
"@babel/plugin-proposal-do-expressions": "^7.2.0",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@babel/plugin-proposal-export-namespace-from": "^7.2.0",
"@babel/plugin-proposal-function-sent": "^7.2.0",
"@babel/plugin-proposal-json-strings": "^7.2.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.2.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.2.0",
"@babel/plugin-proposal-numeric-separator": "^7.2.0",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/plugin-proposal-pipeline-operator": "^7.2.0",
"@babel/plugin-proposal-throw-expressions": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/plugin-transform-flow-strip-types": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-flow": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-plugin-array-includes": "^2.0.3",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-1": "^6.24.1",
"cross-env": "^5.1.3",
"eslint": "^4.16.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-flowtype": "^2.42.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"flow-bin": "^0.64.0",
"husky": "^0.14.3",
"jest": "^22.1.4",
"bundlesize": "^0.17.0",
"cross-env": "^5.2.0",
"eslint": "^5.10.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-flowtype": "^3.2.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"flow-bin": "^0.87.0",
"husky": "^1.2.0",
"jest": "^23.6.0",
"rimraf": "^2.6.2",

@@ -58,5 +75,25 @@ "watch-run": "^1.2.5"

"test:watch": "jest --watch",
"precommit": "npm run lint && npm test",
"bundlesize": "bundlesize",
"validate": "npm run lint && npm test && flow && npm run build && bundlesize",
"prepublishOnly": "npm run build"
},
"browserslist": [
"> 0.2%",
"not dead"
],
"bundlesize": [
{
"path": "./es/index.js",
"maxSize": "5 kB"
},
{
"path": "./lib/index.js",
"maxSize": "5 kB"
}
],
"husky": {
"hooks": {
"pre-commit": "npm run validate"
}
}
}

@@ -9,2 +9,4 @@ Colorizr

> JavaScript module to manipulate colors, get its properties and compare it with another color following the WCAG 2.0.
## Setup

@@ -18,3 +20,3 @@

And import:
And import it:

@@ -28,10 +30,10 @@ ```javascript

```javascript
this.colorizr = new Colorizr('#ff0044');
const colorizr = new Colorizr('#ff0044');
this.colorizr.hex // #ff0044
this.colorizr.hsl // { h: 344, s: 100, l: 50 };
this.colorizr.rgb // { r: 255, g: 0, b: 68 };
colorizr.hex // #ff0044
colorizr.hsl // { h: 344, s: 100, l: 50 };
colorizr.rgb // { r: 255, g: 0, b: 68 };
this.colorizr.saturate(20);
this.colorizr.lighten(10);
colorizr.saturate(20);
colorizr.lighten(10);
```

@@ -41,7 +43,40 @@

Coming soon...
*Set the instance color.*
**colorizr.setColor(color: string | Array<number> | Object)**
**References**
https://24ways.org/2010/calculating-color-contrast/
https://snook.ca/technical/colour_contrast/colour.html
https://webaim.org/resources/contrastchecker/
#### Getters
```js
colorizr.hue // 0-360
colorizr.saturation // 0-100
colorizr.lightness // 0-100
colorizr.red // 0-255
colorizr.green // 0-255
colorizr.blue // 0-255
colorizr.luminance // 0-1
colorizr.chroma // 0-1
```
#### Manipulation
*Increase lightness.*
**colorizr.lighten(percentage: number = 10)**
*Decrease lightness.*
**colorizr.darken(percentage: number = 10)**
*Increase saturation.*
**colorizr.saturate(percentage: number = 10)**
*Decrease saturation.*
**colorizr.saturate(percentage: number = 10)**
### References
[calculating-color-contrast](https://24ways.org/2010/calculating-color-contrast/)
[Colour Contrast Check](https://snook.ca/technical/colour_contrast/colour.html)
[Contrast Checker](https://webaim.org/resources/contrastchecker/)
// @flow
import { expr, hasProperty, isPlainObject, isRequired, pick, round, validateHex } from './utils';
import { expr, isPlainObject, isRequired, pick, round, validateHex } from './utils';

@@ -15,2 +15,3 @@ const RGB = ['r', 'g', 'b'];

hex: string = '';
hsl: Object = {

@@ -21,2 +22,3 @@ h: 0,

};
rgb: Object = {

@@ -88,3 +90,3 @@ r: 0,

if (hasProperty(input, 'h') && hasProperty(input, 's') && hasProperty(input, 'l')) {
if (this.isHSL(input, true)) {
this.hsl = {

@@ -97,3 +99,3 @@ h: this.limit(input.h, 'h'),

}
else if (hasProperty(input, 'r') && hasProperty(input, 'g') && hasProperty(input, 'b')) {
else if (this.isRGB(input, true)) {
this.rgb = input;

@@ -110,3 +112,3 @@ this.hsl = this.rgb2hsl();

/**
* Make the color lighter.
* Increase lightness.
*

@@ -125,3 +127,3 @@ * @param {number} percentage

/**
* Make the color darker.
* Decrease lightness.
*

@@ -173,3 +175,3 @@ * @param {number} percentage

*/
adjustHue(degrees: number = 15): string {
rotate(degrees: number = 15): string {
const hsl = this.shift({

@@ -233,2 +235,34 @@ h: this.constrainDegrees(this.hue, +degrees),

/**
* Get the brightness difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
getBrightnessDifference(left: string = isRequired('left'), right: string = this.hex): number {
const RGBLeft = this.hex2rgb(left);
const RGBRight = this.hex2rgb(right);
const rightY = ((RGBRight.r * 299) + (RGBRight.g * 587) + (RGBRight.b * 114)) / 1000;
const leftY = ((RGBLeft.r * 299) + (RGBLeft.g * 587) + (RGBLeft.b * 114)) / 1000;
return round(Math.abs(rightY - leftY), 4);
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
getColorDifference(left: string = isRequired('left'), right: string = this.hex): number {
const RGBLeft = this.hex2rgb(left);
const RGBRight = this.hex2rgb(right);
return (Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r))
+ (Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g))
+ (Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Test 2 colors for compliance.

@@ -240,3 +274,3 @@ *

*/
checkContrast(left: string = isRequired('left'), right: string = this.hex): Object {
compare(left: string = isRequired('left'), right: string = this.hex): Object {
const LuminanceLeft = this.getLuminance(left);

@@ -275,3 +309,3 @@ const LuminanceRight = this.getLuminance(right);

compliant,
contrastRatio: ratio,
contrast: ratio,
w2a: ratio >= 3,

@@ -285,34 +319,15 @@ w2aaab: ratio >= 7,

/**
* Get the brightness difference between 2 colors.
* Get contrast ratio.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
* @returns {Object}
*/
getBrightnessDifference(left: string = isRequired('left'), right: string = this.hex): number {
const RGBLeft = this.hex2rgb(left);
const RGBRight = this.hex2rgb(right);
contrast(left: string = isRequired('left'), right: string = this.hex): Object {
const compare = this.compare(left, right);
const rightY = ((RGBRight.r * 299) + (RGBRight.g * 587) + (RGBRight.b * 114)) / 1000;
const leftY = ((RGBLeft.r * 299) + (RGBLeft.g * 587) + (RGBLeft.b * 114)) / 1000;
return round(Math.abs(rightY - leftY), 4);
return compare.contrast;
}
/**
* Get the color difference between 2 colors.
*
* @param {Object} left
* @param {Object} [right]
* @returns {number}
*/
getColorDifference(left: string = isRequired('left'), right: string = this.hex): number {
const RGBLeft = this.hex2rgb(left);
const RGBRight = this.hex2rgb(right);
return (Math.max(RGBLeft.r, RGBRight.r) - Math.min(RGBLeft.r, RGBRight.r)) +
(Math.max(RGBLeft.g, RGBRight.g) - Math.min(RGBLeft.g, RGBRight.g)) +
(Math.max(RGBLeft.b, RGBRight.b) - Math.min(RGBLeft.b, RGBRight.b));
}
/**
* Get the luminance of a color.

@@ -425,3 +440,3 @@ *

if (!hasProperty(rgb, 'r') || !hasProperty(rgb, 'g') || !hasProperty(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -491,3 +506,3 @@ }

if (!hasProperty(rgb, 'r') || !hasProperty(rgb, 'g') || !hasProperty(rgb, 'b')) {
if (!this.isRGB(rgb, true)) {
throw new Error('Invalid object');

@@ -510,3 +525,3 @@ }

if (!hasProperty(hsl, 'h') || !hasProperty(hsl, 's') || !hasProperty(hsl, 'l')) {
if (!this.isHSL(hsl, true)) {
throw new Error('Invalid object');

@@ -647,6 +662,8 @@ }

}
else if (['s', 'l'].includes(type)) {
if (['s', 'l'].includes(type)) {
return Math.max(Math.min(input, 100), 0);
}
else if (type === 'h') {
if (type === 'h') {
return Math.max(Math.min(input, 360), 0);

@@ -708,6 +725,14 @@ }

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}
*/
isHSL(input: Object): boolean {
return Object.keys(input).some(d => HSL.includes(d));
isHSL(input: Object, all: boolean = false): boolean {
const keys = Object.keys(input);
if (all) {
return keys.every(d => HSL.includes(d));
}
return keys.some(d => HSL.includes(d));
}

@@ -719,9 +744,19 @@

* @param {Object} input
* @param {boolean} all
*
* @returns {boolean}
*/
isRGB(input: Object): boolean {
return Object.keys(input).some(d => RGB.includes(d));
isRGB(input: Object, all: boolean = false): boolean {
const keys = Object.keys(input);
if (all) {
return keys.every(d => RGB.includes(d));
}
return keys.some(d => RGB.includes(d));
}
/**
* Get red value.
*
* @type {number}

@@ -734,2 +769,4 @@ */

/**
* Get green value.
*
* @type {number}

@@ -742,2 +779,4 @@ */

/**
* Get blue value.
*
* @type {number}

@@ -750,2 +789,4 @@ */

/**
* Get hue value.
*
* @type {number}

@@ -758,2 +799,4 @@ */

/**
* Get saturation value,
*
* @type {number}

@@ -766,2 +809,4 @@ */

/**
* Get lightness value.
*
* @type {number}

@@ -772,2 +817,19 @@ */

}
/**
* Get luminance value.
*
* @type {number}
*/
get luminance(): number {
return this.getLuminance();
}
get chroma(): number {
const { r, g, b } = this.rgb;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
return round((max - min) / 255, 4);
}
}
// @flow
/**
* Colorizr Utils
* @module Utils
*/
/**
* Parse math string expressions.

@@ -63,15 +66,2 @@ *

/**
* Check if an object has a property.
*
* @param {*} input
* @param {string} prop
* @returns {boolean}
*/
export function hasProperty(input: any, prop: string): boolean {
if (!input || !prop) return false;
return Object.prototype.hasOwnProperty.call(input, prop);
}
/**
* Check if the input is an object

@@ -91,3 +81,3 @@ *

export function isRequired(name) {
export function isRequired(name: string) {
throw new Error(`${name || 'parameter'} is required`);

@@ -94,0 +84,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc