colors-formater
Advanced tools
Comparing version 0.0.9-alpha to 0.0.9
"use strict";function _wrapRegExp(){function a(a,b,d){var f=c.call(this,a,b);return e.set(f,d||e.get(a)),f}function b(a,b){var c=e.get(b);return Object.keys(c).reduce(function(b,d){return b[d]=a[c[d]],b},Object.create(null))}_wrapRegExp=function(b,c){return new a(b,void 0,c)};var c=_wrapNativeSuper(RegExp),d=RegExp.prototype,e=new WeakMap;return _inherits(a,c),a.prototype.exec=function(a){var c=d.exec.call(this,a);return c&&(c.groups=b(c,this)),c},a.prototype[Symbol.replace]=function(a,c){if("string"==typeof c){var f=e.get(this);return d[Symbol.replace].call(this,a,c.replace(/\$<([^>]+)>/g,function(a,b){return"$"+f[b]}))}if("function"==typeof c){var g=this;return d[Symbol.replace].call(this,a,function(){var a=[];return a.push.apply(a,arguments),"object"!==_typeof(a[a.length-1])&&a.push(b(a,g)),c.apply(this,a)})}return d[Symbol.replace].call(this,a,c)},_wrapRegExp.apply(this,arguments)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _wrapNativeSuper(a){var b="function"==typeof Map?new Map:void 0;return _wrapNativeSuper=function(a){function c(){return _construct(a,arguments,_getPrototypeOf(this).constructor)}if(null===a||!_isNativeFunction(a))return a;if("function"!=typeof a)throw new TypeError("Super expression must either be null or a function");if("undefined"!=typeof b){if(b.has(a))return b.get(a);b.set(a,c)}return c.prototype=Object.create(a.prototype,{constructor:{value:c,enumerable:!1,writable:!0,configurable:!0}}),_setPrototypeOf(c,a)},_wrapNativeSuper(a)}function _construct(){return _construct=_isNativeReflectConstruct()?Reflect.construct:function(b,c,d){var e=[null];e.push.apply(e,c);var a=Function.bind.apply(b,e),f=new a;return d&&_setPrototypeOf(f,d.prototype),f},_construct.apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(a){return!1}}function _isNativeFunction(a){return-1!==Function.toString.call(a).indexOf("[native code]")}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}/** | ||
* Pega qualquer tipo de cor e converte para o seu equivalente em | ||
* Pega qualquer tipo de cor e converte para o seu equivalente em | ||
* objeto RGB, no formato { r:12, g:23, b:45 } | ||
* | ||
* @tests | ||
* | ||
* // Cores hexadecimais válidas | ||
* | ||
* // Cores hexadecimais válidas | ||
* √ parse(123) returns { r: 17, g: 34, b: 51 } | ||
@@ -16,6 +16,6 @@ * √ parse(#123) returns { r: 17, g: 34, b: 51 } | ||
* √ parse("#ABC") returns { r: 170, g: 187, b: 204 } | ||
* | ||
* | ||
* // Não é uma cor hexadecimal válida | ||
* √ parse(#CLA) returns null | ||
* | ||
* | ||
* // Cores já em formato RGB retornam o mesmo valor | ||
@@ -28,3 +28,3 @@ * √ parse({"r":12,"g":45,"b":67}) returns same value | ||
* √ parse("rgb(120,125,167)") returns {"r":120,"g":125,"b":167} | ||
* | ||
* | ||
* // Cores no formato HSL object | ||
@@ -38,3 +38,3 @@ * | ||
// Using the HSP value, determine whether the color is light or dark | ||
return 127.5>b}return!1},isLight=function(a){return!isDark(a)},isValid=function(a){return!!parse(a)},invert=function(a){var b=parse(a);if(b){var c=function(a){return a-=255,parseInt(0>a?-1*a:a)};return{r:c(b.r),g:c(b.g),b:c(b.b)}}return null};// ////////////////////////////////////////////////////////////////////////// | ||
return 127.5>b}return!1},isLight=function(a){return!isDark(a)},isValid=function(a){return!!parse(a)},invert=function(a){var b=parse(a);if(b){var c=function(a){return a-=255,parseInt(0>a?-1*a:a)};return{r:c(b.r),g:c(b.g),b:c(b.b)}}return null},alphaToHex=function(a){return 1<=a?"FF":0>=a?"00":parseInt(255*a).toString(16).toUpperCase().padStart(2,"0")};// ////////////////////////////////////////////////////////////////////////// | ||
// | ||
@@ -59,5 +59,5 @@ // VALIDATIONS | ||
* √ {"r":244,"g":23,"b":260} is not a valid rgb object | ||
* | ||
* | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
@@ -67,2 +67,2 @@ * @return {Boolean} | ||
// rgbToHsl, | ||
hexToRgb:hexToRgb,rgbToHex:rgbToHex,normalizeHex:normalizeHex,darken:darken,lighten:lighten,invert:invert,isLight:isLight,isDark:isDark,isValid:isValid}; | ||
hexToRgb:hexToRgb,rgbToHex:rgbToHex,normalizeHex:normalizeHex,alphaToHex:alphaToHex,darken:darken,lighten:lighten,invert:invert,isLight:isLight,isDark:isDark,isValid:isValid}; |
@@ -50,2 +50,5 @@ "use strict"; | ||
}, | ||
toHexa: function toHEXA(alpha) { | ||
return HandleColors.rgbToHex(RGB) + HandleColors.alphaToHex(alpha); | ||
}, | ||
toRGB: function toRGB() { | ||
@@ -55,6 +58,6 @@ return RGB; | ||
toRGBA: function toRGBA(alpha) { | ||
return { ...RGB, alpha }; | ||
return { ...RGB, a: alpha }; | ||
}, | ||
toRGBAString: function toRGBAString(alpha) { | ||
return `rgba(${RGB.red}, ${RGB.green}, ${RGB.blue}, ${alpha} )`; | ||
return `rgba(${RGB.r}, ${RGB.g}, ${RGB.b}, ${alpha} )`; | ||
}, | ||
@@ -61,0 +64,0 @@ toRGBString: function toRGBString() { |
{ | ||
"name": "colors-formater", | ||
"description": "A project to handle colors in javascript", | ||
"version": "0.0.9-alpha", | ||
"version": "0.0.9", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
140
README.md
@@ -29,3 +29,2 @@ # colors-formater | ||
## Examples | ||
@@ -36,96 +35,113 @@ | ||
```js | ||
// CommonJS | ||
const Colors = require('colors-formater') | ||
// CommonJS | ||
const Colors = require("colors-formater"); | ||
// ES6 | ||
import Colors from 'colors-formater' | ||
// ES6 | ||
import Colors from "colors-formater"; | ||
``` | ||
### Initialization | ||
```js | ||
// Initialization with a Hexa Color | ||
const color = Colors('123') | ||
const color = Colors('#123') | ||
const color = Colors('#ABCDEF') | ||
// Initialization with a RGB Object | ||
const color = Colors({ r: 255, g: 23, b: 87 }) | ||
// Initialization with a RGB String | ||
const color = Colors('rgb(23, 45, 67)') | ||
const color = Colors('rgba(23, 45, 67, 0.4)') | ||
// Initialization with a HSL Object | ||
const color = Colors({ h: 255, s: '23%', l: '50%' }) | ||
// Initialization with a Hexa Color | ||
const color = Colors("123"); | ||
const color = Colors("#123"); | ||
const color = Colors("#ABCDEF"); | ||
// Initialization with a HSL Sring | ||
const color = Colors('hsl(255, 23%, 50%)') | ||
``` | ||
// Initialization with a RGB Object | ||
const color = Colors({ r: 255, g: 23, b: 87 }); | ||
### Convertions | ||
// Initialization with a RGB String | ||
const color = Colors("rgb(23, 45, 67)"); | ||
const color = Colors("rgba(23, 45, 67, 0.4)"); | ||
// Initialization with a HSL Object | ||
const color = Colors({ h: 255, s: "23%", l: "50%" }); | ||
// Initialization with a HSL Sring | ||
const color = Colors("hsl(255, 23%, 50%)"); | ||
``` | ||
### Convertions | ||
```js | ||
// Init | ||
const color = Colors('#abc') | ||
// Init | ||
const color = Colors("#abc"); | ||
// Convert do normalized Hexadecimal | ||
color.toHex() // #AABBCC | ||
// Convert do normalized Hexadecimal | ||
color.toHex(); // #AABBCC | ||
// Convert do RGB Object | ||
color.toRGB() // { r: 12, g: 45, b: 56 } | ||
// Convert do RGB Object | ||
color.toRGB(); // { r: 12, g: 45, b: 56 } | ||
// Convert to RGB string | ||
color.toRGBString() // rgb( 12, 45, 56 ) | ||
// Convert to RGB string | ||
color.toRGBString(); // rgb( 12, 45, 56 ) | ||
// Convert to HSL Object | ||
color.toHSL() // { h: 30, s: 50%, l: 50% } | ||
// Convert to HSL Object | ||
color.toHSL(); // { h: 30, s: 50%, l: 50% } | ||
// Convert to HSL String | ||
color.toHSLString() // hsl( 30, 50%, 50% ) | ||
// Convert to HSL String | ||
color.toHSLString(); // hsl( 30, 50%, 50% ) | ||
``` | ||
### Verifications | ||
### Verifications | ||
```js | ||
// Init | ||
const color = Colors('#abc') | ||
// Init | ||
const color = Colors("#abc"); | ||
// Is a valid color in any format supported by parse()? | ||
color.isValid() // true | ||
// Is a valid color in any format supported by parse()? | ||
color.isValid(); // true | ||
// Is a hexadecimal color? | ||
color.isHex() // true | ||
// Is a hexadecimal color? | ||
color.isHex(); // true | ||
// Is a HSL object color? | ||
color.isHSL() // false | ||
// Is a HSL object color? | ||
color.isHSL(); // false | ||
// Is a RGB Object color? | ||
color.isRGB() //false | ||
// Is a RGB Object color? | ||
color.isRGB(); //false | ||
// Is a Light color? | ||
color.isLight() //false | ||
// Is a Light color? | ||
color.isLight(); //false | ||
// Is a Dark color? | ||
color.isDark() //true | ||
// Is a Dark color? | ||
color.isDark(); //true | ||
``` | ||
### Calculations | ||
### Calculations | ||
These functions changes internal initial color. Always | ||
prefer using it with the constructor Colors([color]) | ||
These functions changes internal initial color. Always | ||
prefer using it with the constructor Colors([color]) | ||
```js | ||
// Init | ||
const color = Colors('#abc') | ||
// Init | ||
const color = Colors("#abc"); | ||
// Darken a color and returns the result as hexadecimal | ||
Colors('#A34').darken(.2).toHex() | ||
// Darken a color and returns the result as hexadecimal | ||
Colors("#A34").darken(0.2).toHex(); | ||
// Lighten a color and returns the result as RGB object | ||
Colors('#A34').lighten(.35).toRGB() | ||
// Lighten a color and returns the result as RGB object | ||
Colors("#A34").lighten(0.35).toRGB(); | ||
// Inverts a color and returns the result as HSL object | ||
Colors('#A34').invert().toHSL() | ||
// Inverts a color and returns the result as HSL object | ||
Colors("#A34").invert().toHSL(); | ||
``` | ||
### Alpha | ||
These functions adds alpha to color | ||
```js | ||
// Init | ||
const color = Colors("#abc"); | ||
// Darken a color and returns the result as hexadecimal with alpha | ||
Colors("#A34").toHexa(0.2); | ||
// Lighten a color and returns the result as RGBA object | ||
Colors("#A34").toRGBA(0.2); | ||
// Lighten a color and returns the result as RGBA string | ||
Colors("#A34").toRGBAString(0.2); | ||
``` |
@@ -1,9 +0,8 @@ | ||
/** | ||
* Pega qualquer tipo de cor e converte para o seu equivalente em | ||
* Pega qualquer tipo de cor e converte para o seu equivalente em | ||
* objeto RGB, no formato { r:12, g:23, b:45 } | ||
* | ||
* @tests | ||
* | ||
* // Cores hexadecimais válidas | ||
* | ||
* // Cores hexadecimais válidas | ||
* √ parse(123) returns { r: 17, g: 34, b: 51 } | ||
@@ -17,6 +16,6 @@ * √ parse(#123) returns { r: 17, g: 34, b: 51 } | ||
* √ parse("#ABC") returns { r: 170, g: 187, b: 204 } | ||
* | ||
* | ||
* // Não é uma cor hexadecimal válida | ||
* √ parse(#CLA) returns null | ||
* | ||
* | ||
* // Cores já em formato RGB retornam o mesmo valor | ||
@@ -29,3 +28,3 @@ * √ parse({"r":12,"g":45,"b":67}) returns same value | ||
* √ parse("rgb(120,125,167)") returns {"r":120,"g":125,"b":167} | ||
* | ||
* | ||
* // Cores no formato HSL object | ||
@@ -39,3 +38,3 @@ * | ||
if (isRGBObject(color)) { | ||
return color | ||
return color; | ||
} | ||
@@ -52,13 +51,11 @@ | ||
} | ||
// | ||
// | ||
else if (isHSLObject(color)) { | ||
return hslToRgb(color); | ||
} else if (isHexadecimal(color)) { | ||
return hexToRgb(color); | ||
} | ||
else if (isHexadecimal(color)) { | ||
return hexToRgb(color) | ||
} | ||
return null; | ||
} | ||
}; | ||
@@ -71,3 +68,2 @@ // ////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -88,5 +84,5 @@ * Verify if a specified param is a RGB color object | ||
* √ {"r":244,"g":23,"b":260} is not a valid rgb object | ||
* | ||
* | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
@@ -97,8 +93,13 @@ * @return {Boolean} | ||
if (!color) return false; | ||
return typeof color === 'object' | ||
&& Object.keys(color).every(item => ['r', 'g', 'b'].includes(item)) | ||
&& color.r >= 0 && color.r <= 255 | ||
&& color.g >= 0 && color.g <= 255 | ||
&& color.b >= 0 && color.b <= 255 | ||
} | ||
return ( | ||
typeof color === "object" && | ||
Object.keys(color).every((item) => ["r", "g", "b"].includes(item)) && | ||
color.r >= 0 && | ||
color.r <= 255 && | ||
color.g >= 0 && | ||
color.g <= 255 && | ||
color.b >= 0 && | ||
color.b <= 255 | ||
); | ||
}; | ||
@@ -108,3 +109,3 @@ /** | ||
* rgb(255, 255, 255) | ||
* | ||
* | ||
* @example | ||
@@ -120,3 +121,3 @@ * isRGBString('rgb(12, 34, 57)') | ||
* √ isRGBString(rgb(12, 260, 57)) returns false. "G" component cannot be bigger than 255 | ||
* √ isRGBString(rgb(12, 34, 260)) returns false. "B" component cannot be bigger than 255 | ||
* √ isRGBString(rgb(12, 34, 260)) returns false. "B" component cannot be bigger than 255 | ||
* | ||
@@ -130,12 +131,11 @@ * | ||
const rgb = convertRGBStringToObject(color); | ||
return !!rgb && isRGBObject(rgb) | ||
} | ||
return !!rgb && isRGBObject(rgb); | ||
}; | ||
/** | ||
* Validates a HSL object specified in param | ||
* | ||
* | ||
* @example | ||
* isHSLObject( { h:12, s:'0%', l:'20%' } ) | ||
* | ||
* | ||
* @tests | ||
@@ -157,20 +157,25 @@ * | ||
const isHSLObject = function (color) { | ||
const validate = component => { | ||
const rgx = /(?<component>[0-9]+)\%/ | ||
const validate = (component) => { | ||
const rgx = /(?<component>[0-9]+)\%/; | ||
const match = rgx.exec(component); | ||
if (match && match.groups) { | ||
return parseInt(match.groups.component) >= 0 && parseInt(match.groups.component) <= 100 | ||
return ( | ||
parseInt(match.groups.component) >= 0 && | ||
parseInt(match.groups.component) <= 100 | ||
); | ||
} | ||
return false; | ||
} | ||
}; | ||
return typeof color === 'object' | ||
&& Object.keys(color).every(item => ['h', 's', 'l'].includes(item)) | ||
&& color.h >= 0 && color.h <= 360 | ||
&& validate(color.s) | ||
&& validate(color.l) | ||
} | ||
return ( | ||
typeof color === "object" && | ||
Object.keys(color).every((item) => ["h", "s", "l"].includes(item)) && | ||
color.h >= 0 && | ||
color.h <= 360 && | ||
validate(color.s) && | ||
validate(color.l) | ||
); | ||
}; | ||
/** | ||
@@ -181,3 +186,3 @@ * Validates if a specified param is a HSL string color | ||
* isHSLString('hsl(12, 90%, 50%)') | ||
* | ||
* | ||
* @tests | ||
@@ -197,5 +202,5 @@ * | ||
* duration: 16ms | ||
* | ||
* | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
@@ -206,6 +211,5 @@ * @return {Boolean} | ||
const hsl = convertHSLStringToObject(color); | ||
return !!hsl && isHSLObject(hsl) | ||
} | ||
return !!hsl && isHSLObject(hsl); | ||
}; | ||
/** | ||
@@ -216,7 +220,7 @@ * Validates if the specified param is a hexadecimal color | ||
* isHexadecimal('#FFF') | ||
* | ||
* | ||
* @tests | ||
* | ||
* Is a VALID Hexadecimal Color | ||
* | ||
* | ||
* √ isHexadecimal(123) returns true | ||
@@ -228,10 +232,10 @@ * √ isHexadecimal(#123) returns true | ||
* √ isHexadecimal(abc) returns true | ||
* | ||
* | ||
* Is a INVALID Hexadecimal Color | ||
* | ||
* | ||
* √ "#1232" is not a valid hexadecimal color. Returns false | ||
* √ "#cla" is not a valid hexadecimal color. Returns false | ||
* √ "cla" is not a valid hexadecimal color. Returns false | ||
* | ||
* | ||
* | ||
* | ||
* total: 9 | ||
@@ -245,4 +249,4 @@ * passing: 9 | ||
const isHexadecimal = function (color) { | ||
return /^([#])?(([0-9a-fA-F]{3})|([0-9a-fA-F]{6}))$/.test(color) | ||
} | ||
return /^([#])?(([0-9a-fA-F]{3})|([0-9a-fA-F]{6}))$/.test(color); | ||
}; | ||
@@ -257,6 +261,6 @@ // ////////////////////////////////////////////////////////////////////////// | ||
* Convert a string in format rgb(12, 45, 67) to an object in format: { r: 12, g: 45, b: 67 } | ||
* | ||
* | ||
* @example | ||
* convertRGBStringToObject('rgb(12, 45, 67)') | ||
* | ||
* | ||
* @tests | ||
@@ -275,32 +279,30 @@ * | ||
* duration: 17ms | ||
* | ||
* | ||
* @param {*} color | ||
* | ||
* | ||
* @param {*} color | ||
*/ | ||
const convertRGBStringToObject = function (color) { | ||
const rgx = | ||
/^rgba?\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+)(?:,\s*(?<a>\d*(?:\.\d+)?))?\)$/; | ||
const match = rgx.exec(color); | ||
const rgx = /^rgba?\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+)(?:,\s*(?<a>\d*(?:\.\d+)?))?\)$/ | ||
const match = rgx.exec(color) | ||
if (match && match.groups) { | ||
const { r, g, b } = match.groups | ||
const { r, g, b } = match.groups; | ||
const rgb = { r: parseInt(r), g: parseInt(g), b: parseInt(b) } | ||
const rgb = { r: parseInt(r), g: parseInt(g), b: parseInt(b) }; | ||
if (isRGBObject(rgb)) { | ||
return rgb | ||
} | ||
else return null | ||
return rgb; | ||
} else return null; | ||
} | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Converts a RGB Object format to a String Format | ||
* | ||
* | ||
* @example | ||
* convertRGBObjectToString( { h:23, s: '50%', l: '50%' } ) | ||
* | ||
* | ||
* @tests | ||
@@ -319,5 +321,5 @@ * | ||
* duration: 20ms | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* @param {Object} color | ||
@@ -328,12 +330,11 @@ * @returns {String} in format rgb(23, 45, 67) or null if invalid rgb color object | ||
if (isRGBObject(color)) { | ||
return `rgb(${color.r}, ${color.g}, ${color.b})` | ||
return `rgb(${color.r}, ${color.g}, ${color.b})`; | ||
} | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Convert a string in format hsl(24, 50%, 50%) in format {h:24, s: '50%', l:'50%'} | ||
* | ||
* | ||
* @validation | ||
@@ -343,6 +344,6 @@ * hue = 0 - 360 | ||
* lightness = 0% - 100% (0% = black/ 100% = white) | ||
* | ||
* | ||
* @example | ||
* convertHSLStringToObject('hsl(24, 50%, 50%)') | ||
* | ||
* | ||
* @tests | ||
@@ -372,22 +373,24 @@ * | ||
const convertHSLStringToObject = function (color) { | ||
const rgx = /^hsl\((?<h>\d+),\s*(?<s>(?<sInt>\d+)\%),\s*(?<l>(?<lInt>\d+)\%)\)$/ | ||
const match = rgx.exec(color) | ||
const rgx = | ||
/^hsl\((?<h>\d+),\s*(?<s>(?<sInt>\d+)\%),\s*(?<l>(?<lInt>\d+)\%)\)$/; | ||
const match = rgx.exec(color); | ||
if (match && match.groups) { | ||
const { h, s, l, sInt, lInt } = match.groups | ||
const { h, s, l, sInt, lInt } = match.groups; | ||
if (h < 0 || h > 360 || sInt < 0 || sInt > 100 || lInt < 0 || lInt > 100) return null | ||
if (h < 0 || h > 360 || sInt < 0 || sInt > 100 || lInt < 0 || lInt > 100) | ||
return null; | ||
return { h: parseInt(h), s, l } | ||
return { h: parseInt(h), s, l }; | ||
} | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Converts a HSL Object format to a String Format | ||
* | ||
* | ||
* @example | ||
* convertHSLObjectToString( { h:23, s: '50%', l: '50%' } ) | ||
* | ||
* | ||
* @tests | ||
@@ -407,4 +410,4 @@ * | ||
* passing: 6 | ||
* duration: 20ms | ||
* | ||
* duration: 20ms | ||
* | ||
* @param {Object} color | ||
@@ -415,10 +418,8 @@ * @returns {String} in format hsl(23, 50%, 50%) or null if invalid hsl color object | ||
if (isHSLObject(color)) { | ||
return `hsl(${color.h}, ${color.s}, ${color.l})` | ||
return `hsl(${color.h}, ${color.s}, ${color.l})`; | ||
} | ||
return null; | ||
} | ||
}; | ||
/** | ||
@@ -448,3 +449,3 @@ * Converts an HSL color value to RGB. Conversion formula | ||
return p; | ||
} | ||
}; | ||
@@ -458,4 +459,8 @@ var q = l < 0.5 ? l * (1 + s) : l + s - l * s; | ||
return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255) }; | ||
} | ||
return { | ||
r: Math.round(r * 255), | ||
g: Math.round(g * 255), | ||
b: Math.round(b * 255), | ||
}; | ||
}; | ||
@@ -474,5 +479,8 @@ /** | ||
const rgbToHsl = function ({ r, g, b }) { | ||
r /= 255, g /= 255, b /= 255; | ||
var max = Math.max(r, g, b), min = Math.min(r, g, b); | ||
var h, s, l = (max + min) / 2; | ||
(r /= 255), (g /= 255), (b /= 255); | ||
var max = Math.max(r, g, b), | ||
min = Math.min(r, g, b); | ||
var h, | ||
s, | ||
l = (max + min) / 2; | ||
@@ -485,5 +493,11 @@ if (max == min) { | ||
switch (max) { | ||
case r: h = (g - b) / d + (g < b ? 6 : 0); break; | ||
case g: h = (b - r) / d + 2; break; | ||
case b: h = (r - g) / d + 4; break; | ||
case r: | ||
h = (g - b) / d + (g < b ? 6 : 0); | ||
break; | ||
case g: | ||
h = (b - r) / d + 2; | ||
break; | ||
case b: | ||
h = (r - g) / d + 4; | ||
break; | ||
} | ||
@@ -494,3 +508,3 @@ h /= 6; | ||
return { h, s, l }; | ||
} | ||
}; | ||
@@ -501,4 +515,4 @@ /** | ||
* @example | ||
* | ||
* | ||
* | ||
* | ||
* @tests | ||
@@ -519,5 +533,5 @@ * | ||
* duration: 18ms | ||
* | ||
* | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
@@ -528,3 +542,2 @@ * @return | ||
if (isHexadecimal(color)) { | ||
color = normalizeHex(color).substr(1, 6); | ||
@@ -536,15 +549,15 @@ // parseInt(hex, 16) | ||
b: parseInt(color.substr(4, 2), 16), | ||
} | ||
}; | ||
} | ||
return null | ||
} | ||
return null; | ||
}; | ||
/** | ||
* | ||
* | ||
* | ||
* | ||
* @example | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
* | ||
* | ||
* @param {String} color: Cor em formato | ||
* @return | ||
@@ -555,19 +568,19 @@ */ | ||
let { r, g, b } = color; | ||
r = (r).toString(16) | ||
g = (g).toString(16) | ||
b = (b).toString(16) | ||
r = r.toString(16); | ||
g = g.toString(16); | ||
b = b.toString(16); | ||
return normalizeHex(`${r.length === 1 ? '0' + r : r}${g.length === 1 ? '0' + g : g}${b.length === 1 ? '0' + b : b}`) | ||
return normalizeHex( | ||
`${r.length === 1 ? "0" + r : r}${g.length === 1 ? "0" + g : g}${ | ||
b.length === 1 ? "0" + b : b | ||
}` | ||
); | ||
} | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Normalizes a hexadecimal color | ||
* | ||
* | ||
* @tests | ||
@@ -597,3 +610,3 @@ * | ||
* normalizeHex('cla') // null | ||
* | ||
* | ||
* @param {String} strHexColor: Hexadecimal color | ||
@@ -603,16 +616,20 @@ */ | ||
strHexColor = String(strHexColor).toUpperCase(); | ||
const match = /^(?<hash>[#])?(?:(?<six>[0-9A-F]{6})|(?<three>[0-9A-F]{3}))?$/.exec(strHexColor); | ||
const match = | ||
/^(?<hash>[#])?(?:(?<six>[0-9A-F]{6})|(?<three>[0-9A-F]{3}))?$/.exec( | ||
strHexColor | ||
); | ||
if (match && match.groups) { | ||
const { three, six } = match.groups | ||
const { three, six } = match.groups; | ||
if (three) return `#${three[0]}${three[0]}${three[1]}${three[1]}${three[2]}${three[2]}` | ||
if (three) | ||
return `#${three[0]}${three[0]}${three[1]}${three[1]}${three[2]}${three[2]}`; | ||
return `#${six}`; | ||
} | ||
return null | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Darken a specified color | ||
* | ||
* | ||
* @example | ||
@@ -622,3 +639,3 @@ * darken('#DCF', 0.2) | ||
* darken('rgb(12, 65, 87)', 0.4) | ||
* | ||
* | ||
* @tests | ||
@@ -638,5 +655,5 @@ * | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -650,24 +667,22 @@ * @param {Float}: Number between 0 and 1 to darken color | ||
if (rgb) { | ||
const darkenComponent = (component, step) => { | ||
component -= component * step; | ||
if (component < 0) return 0 | ||
else if (component > 255) return 255 | ||
else return parseInt(component) | ||
} | ||
if (component < 0) return 0; | ||
else if (component > 255) return 255; | ||
else return parseInt(component); | ||
}; | ||
return { | ||
r: darkenComponent(rgb.r, step), | ||
g: darkenComponent(rgb.g, step), | ||
b: darkenComponent(rgb.b, step) | ||
} | ||
b: darkenComponent(rgb.b, step), | ||
}; | ||
} | ||
return null | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Lighten a specified color | ||
* | ||
* | ||
* @example | ||
@@ -677,3 +692,3 @@ * lighten('#DCF', 0.2) | ||
* lighten('rgb(12, 65, 87)', 0.4) | ||
* | ||
* | ||
* @tests | ||
@@ -694,5 +709,5 @@ * | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -706,20 +721,18 @@ * @param {Float}: Number between 0 and 1 to darken color | ||
if (rgb) { | ||
const lightenComponent = (component, step) => { | ||
component += component * step; | ||
if (component < 0) return 0 | ||
else if (component > 255) return 255 | ||
else return parseInt(component) | ||
} | ||
if (component < 0) return 0; | ||
else if (component > 255) return 255; | ||
else return parseInt(component); | ||
}; | ||
return { | ||
r: lightenComponent(rgb.r, step), | ||
g: lightenComponent(rgb.g, step), | ||
b: lightenComponent(rgb.b, step) | ||
} | ||
b: lightenComponent(rgb.b, step), | ||
}; | ||
} | ||
return null | ||
} | ||
return null; | ||
}; | ||
@@ -733,3 +746,3 @@ /** | ||
* isDark('rgb(12, 65, 87)') | ||
* | ||
* | ||
* @tests | ||
@@ -746,4 +759,4 @@ * | ||
* duration: 28ms | ||
* | ||
* | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -753,3 +766,2 @@ * @returns {Boolean} | ||
const isDark = function (color) { | ||
const rgb = parse(color); | ||
@@ -760,7 +772,3 @@ if (rgb) { | ||
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html | ||
const hsp = Math.sqrt( | ||
0.299 * (r * r) + | ||
0.587 * (g * g) + | ||
0.114 * (b * b) | ||
); | ||
const hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); | ||
@@ -772,7 +780,7 @@ // Using the HSP value, determine whether the color is light or dark | ||
return false; | ||
} | ||
}; | ||
/** | ||
* Verify if a specified param is a light color | ||
* | ||
* | ||
* @example | ||
@@ -794,4 +802,4 @@ * isLight('#DCF') | ||
* duration: 19ms | ||
* | ||
* | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -801,9 +809,8 @@ * @returns {Object} RGB Color object | ||
const isLight = function (color) { | ||
return !isDark(color) | ||
} | ||
return !isDark(color); | ||
}; | ||
/** | ||
* Verify if a specified param is a valid color in anu format supperted by parse() | ||
* | ||
* | ||
* @example | ||
@@ -831,4 +838,4 @@ * isValid('#DCF') | ||
* duration: 28ms | ||
* | ||
* | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -839,10 +846,7 @@ * @returns {Object} RGB Color object | ||
return !!parse(color); | ||
} | ||
}; | ||
/** | ||
* Invert a specified color | ||
* | ||
* | ||
* @example | ||
@@ -852,3 +856,3 @@ * invert('#DCF') | ||
* invert('rgb(12, 65, 87)') | ||
* | ||
* | ||
* @tests | ||
@@ -862,3 +866,3 @@ * | ||
* √ #FF0A0A inverted | ||
* | ||
* | ||
* @param {String, Object} color: Any supported format by parse() | ||
@@ -871,19 +875,36 @@ * @returns {Object} RGB Color object | ||
if (rgb) { | ||
const invertComponent = (component) => { | ||
component -= 255 | ||
return parseInt(component < 0 ? component * (-1) : component) | ||
} | ||
component -= 255; | ||
return parseInt(component < 0 ? component * -1 : component); | ||
}; | ||
return { | ||
r: invertComponent(rgb.r), | ||
g: invertComponent(rgb.g), | ||
b: invertComponent(rgb.b) | ||
} | ||
b: invertComponent(rgb.b), | ||
}; | ||
} | ||
return null | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Convert an alpha between 0 and 1 to Hex | ||
* | ||
* @example | ||
* alphaToIndex(0.1) // -> 19 | ||
* alphaToIndex(0.67) // -> AA | ||
* alphaToIndex(0.84) // -> D6 | ||
*/ | ||
var alphaToHex = (alpha) => { | ||
return alpha >= 1 | ||
? "FF" | ||
: alpha <= 0 | ||
? "00" | ||
: parseInt(alpha * 255) | ||
.toString(16) | ||
.toUpperCase() | ||
.padStart(2, "0"); | ||
}; | ||
module.exports = { | ||
@@ -905,2 +926,3 @@ parse, | ||
normalizeHex, | ||
alphaToHex, | ||
@@ -913,3 +935,3 @@ darken, | ||
isDark, | ||
isValid | ||
} | ||
isValid, | ||
}; |
@@ -1,35 +0,33 @@ | ||
require('./index.test') | ||
require("./index.test"); | ||
require('./parse.test') // ok | ||
require("./parse.test"); // ok | ||
require('./is-rgb-object.test') // ok | ||
require('./is-rgb-string.test') // ok | ||
require("./is-rgb-object.test"); // ok | ||
require("./is-rgb-string.test"); // ok | ||
require('./is-hsl-object.test') // ok | ||
require('./is-hsl-string.test') // ok | ||
require("./is-hsl-object.test"); // ok | ||
require("./is-hsl-string.test"); // ok | ||
require('./is-hexadecimal.test') // ok | ||
require("./is-hexadecimal.test"); // ok | ||
require('./convert-rgb-string-to-object.test') // ok | ||
require('./convert-rgb-object-to-string.test') // OK | ||
require("./convert-rgb-string-to-object.test"); // ok | ||
require("./convert-rgb-object-to-string.test"); // OK | ||
require('./convert-hsl-string-to-object.test') // OK | ||
require('./convert-hsl-object-to-string.test') // OK | ||
require("./convert-hsl-string-to-object.test"); // OK | ||
require("./convert-hsl-object-to-string.test"); // OK | ||
require('./hex-to-rgb.test') // OK | ||
require("./hex-to-rgb.test"); // OK | ||
require('./normalize-hex.test') // OK | ||
require("./normalize-hex.test"); // OK | ||
require("./invert.test"); // OK | ||
require('./invert.test') // OK | ||
require("./darken.test"); // OK | ||
require("./lighten.test"); // OK | ||
require('./darken.test') // OK | ||
require('./lighten.test') // OK | ||
require("./is-light.test"); // OK | ||
require("./is-dark.test"); // OK | ||
require("./is-valid.test"); // OK | ||
require('./is-light.test') // OK | ||
require('./is-dark.test') // OK | ||
require('./is-valid.test') // OK | ||
//*/ | ||
require("./alpha-to-hex.test"); // ok | ||
//*/ |
58216
27
1400
146