update
This commit is contained in:
+6
-2
@@ -1,7 +1,5 @@
|
||||
# color-string
|
||||
|
||||
[](https://travis-ci.org/Qix-/color-string)
|
||||
|
||||
> library for parsing and generating CSS color strings.
|
||||
|
||||
## Install
|
||||
@@ -21,15 +19,21 @@ colorString.get('#FFF') // {model: 'rgb', value: [255,
|
||||
colorString.get('#FFFA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
|
||||
colorString.get('#FFFFFFAA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
|
||||
colorString.get('hsl(360, 100%, 50%)') // {model: 'hsl', value: [0, 100, 50, 1]}
|
||||
colorString.get('hsl(360 100% 50%)') // {model: 'hsl', value: [0, 100, 50, 1]}
|
||||
colorString.get('hwb(60, 3%, 60%)') // {model: 'hwb', value: [60, 3, 60, 1]}
|
||||
|
||||
colorString.get.rgb('#FFF') // [255, 255, 255, 1]
|
||||
colorString.get.rgb('blue') // [0, 0, 255, 1]
|
||||
colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3]
|
||||
colorString.get.rgb('rgba(200 60 60 / 0.3)') // [200, 60, 60, 0.3]
|
||||
colorString.get.rgb('rgba(200 60 60 / 30%)') // [200, 60, 60, 0.3]
|
||||
colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1]
|
||||
colorString.get.rgb('rgb(200 200 200)') // [200, 200, 200, 1]
|
||||
|
||||
colorString.get.hsl('hsl(360, 100%, 50%)') // [0, 100, 50, 1]
|
||||
colorString.get.hsl('hsl(360 100% 50%)') // [0, 100, 50, 1]
|
||||
colorString.get.hsl('hsla(360, 60%, 50%, 0.4)') // [0, 60, 50, 0.4]
|
||||
colorString.get.hsl('hsl(360 60% 50% / 0.4)') // [0, 60, 50, 0.4]
|
||||
|
||||
colorString.get.hwb('hwb(60, 3%, 60%)') // [60, 3, 60, 1]
|
||||
colorString.get.hwb('hwb(60, 3%, 60%, 0.6)') // [60, 3, 60, 0.6]
|
||||
|
||||
+24
-16
@@ -1,12 +1,13 @@
|
||||
/* MIT license */
|
||||
var colorNames = require('color-name');
|
||||
var swizzle = require('simple-swizzle');
|
||||
var hasOwnProperty = Object.hasOwnProperty;
|
||||
|
||||
var reverseNames = {};
|
||||
var reverseNames = Object.create(null);
|
||||
|
||||
// create a list of reverse color names
|
||||
for (var name in colorNames) {
|
||||
if (colorNames.hasOwnProperty(name)) {
|
||||
if (hasOwnProperty.call(colorNames, name)) {
|
||||
reverseNames[colorNames[name]] = name;
|
||||
}
|
||||
}
|
||||
@@ -49,9 +50,9 @@ cs.get.rgb = function (string) {
|
||||
|
||||
var abbr = /^#([a-f0-9]{3,4})$/i;
|
||||
var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i;
|
||||
var rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
||||
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
||||
var keyword = /(\D+)/;
|
||||
var rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
|
||||
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
|
||||
var keyword = /^(\w+)$/;
|
||||
|
||||
var rgb = [0, 0, 0, 1];
|
||||
var match;
|
||||
@@ -69,7 +70,7 @@ cs.get.rgb = function (string) {
|
||||
}
|
||||
|
||||
if (hexAlpha) {
|
||||
rgb[3] = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
|
||||
rgb[3] = parseInt(hexAlpha, 16) / 255;
|
||||
}
|
||||
} else if (match = string.match(abbr)) {
|
||||
match = match[1];
|
||||
@@ -80,7 +81,7 @@ cs.get.rgb = function (string) {
|
||||
}
|
||||
|
||||
if (hexAlpha) {
|
||||
rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
|
||||
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
|
||||
}
|
||||
} else if (match = string.match(rgba)) {
|
||||
for (i = 0; i < 3; i++) {
|
||||
@@ -88,7 +89,11 @@ cs.get.rgb = function (string) {
|
||||
}
|
||||
|
||||
if (match[4]) {
|
||||
rgb[3] = parseFloat(match[4]);
|
||||
if (match[5]) {
|
||||
rgb[3] = parseFloat(match[4]) * 0.01;
|
||||
} else {
|
||||
rgb[3] = parseFloat(match[4]);
|
||||
}
|
||||
}
|
||||
} else if (match = string.match(per)) {
|
||||
for (i = 0; i < 3; i++) {
|
||||
@@ -96,19 +101,22 @@ cs.get.rgb = function (string) {
|
||||
}
|
||||
|
||||
if (match[4]) {
|
||||
rgb[3] = parseFloat(match[4]);
|
||||
if (match[5]) {
|
||||
rgb[3] = parseFloat(match[4]) * 0.01;
|
||||
} else {
|
||||
rgb[3] = parseFloat(match[4]);
|
||||
}
|
||||
}
|
||||
} else if (match = string.match(keyword)) {
|
||||
if (match[1] === 'transparent') {
|
||||
return [0, 0, 0, 0];
|
||||
}
|
||||
|
||||
rgb = colorNames[match[1]];
|
||||
|
||||
if (!rgb) {
|
||||
if (!hasOwnProperty.call(colorNames, match[1])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
rgb = colorNames[match[1]];
|
||||
rgb[3] = 1;
|
||||
|
||||
return rgb;
|
||||
@@ -129,12 +137,12 @@ cs.get.hsl = function (string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var hsl = /^hsla?\(\s*([+-]?(?:\d*\.)?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
||||
var hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d\.]+)%\s*,?\s*([+-]?[\d\.]+)%\s*(?:[,|\/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/;
|
||||
var match = string.match(hsl);
|
||||
|
||||
if (match) {
|
||||
var alpha = parseFloat(match[4]);
|
||||
var h = (parseFloat(match[1]) + 360) % 360;
|
||||
var h = ((parseFloat(match[1]) % 360) + 360) % 360;
|
||||
var s = clamp(parseFloat(match[2]), 0, 100);
|
||||
var l = clamp(parseFloat(match[3]), 0, 100);
|
||||
var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1);
|
||||
@@ -150,7 +158,7 @@ cs.get.hwb = function (string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var hwb = /^hwb\(\s*([+-]?\d*[\.]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
||||
var hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/;
|
||||
var match = string.match(hwb);
|
||||
|
||||
if (match) {
|
||||
@@ -229,6 +237,6 @@ function clamp(num, min, max) {
|
||||
}
|
||||
|
||||
function hexDouble(num) {
|
||||
var str = num.toString(16).toUpperCase();
|
||||
var str = Math.round(num).toString(16).toUpperCase();
|
||||
return (str.length < 2) ? '0' + str : str;
|
||||
}
|
||||
|
||||
+12
-12
@@ -1,32 +1,32 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"color-string@1.5.3",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"color-string@1.9.1",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "color-string@1.5.3",
|
||||
"_id": "color-string@1.5.3",
|
||||
"_from": "color-string@1.9.1",
|
||||
"_id": "color-string@1.9.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-dC2C5qeWoYkxki5UAXapdjqO672AM4vZuPGRQfO8b5HKuKGBbKWpITyDYN7TOFKvRW7kOgAn3746clDBMDJyQw==",
|
||||
"_integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==",
|
||||
"_location": "/color-string",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "color-string@1.5.3",
|
||||
"raw": "color-string@1.9.1",
|
||||
"name": "color-string",
|
||||
"escapedName": "color-string",
|
||||
"rawSpec": "1.5.3",
|
||||
"rawSpec": "1.9.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.5.3"
|
||||
"fetchSpec": "1.9.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/color"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.3.tgz",
|
||||
"_spec": "1.5.3",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/color-string/-/color-string-1.9.1.tgz",
|
||||
"_spec": "1.9.1",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Heather Arthur",
|
||||
"email": "fayearthur@gmail.com"
|
||||
@@ -74,7 +74,7 @@
|
||||
"pretest": "xo",
|
||||
"test": "node test/basic.js"
|
||||
},
|
||||
"version": "1.5.3",
|
||||
"version": "1.9.1",
|
||||
"xo": {
|
||||
"rules": {
|
||||
"no-cond-assign": 0,
|
||||
|
||||
Reference in New Issue
Block a user