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

stylecow-plugin-color

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stylecow-plugin-color - npm Package Compare versions

Comparing version 4.0.1 to 4.1.0

211

index.js
//http://dev.w3.org/csswg/css-color/
var color = require('stylecow-color');
var Color = require('color');
var ColorFunction = require('css-color-function');

@@ -13,11 +14,16 @@ module.exports = function (stylecow) {

fn: function (hex) {
if ((hex.name.length === 4 || hex.name.length === 8)) {
var rgba = color.HEX_RGBA(hex.name);
var color = hexaColor(hex);
if (rgba[3] === 1) {
hex.setName(color.RGBA_HEX(rgba));
} else {
hex.replaceWith(stylecow.parse('rgba(' + rgba.join(',') + ')', 'Function'));
}
if (!color) {
return;
}
//if its opaque
if (color.alpha() == 1) {
hex.setName(color.hexString().substr(1));
return;
}
//replace by rgba
hex.replaceWith(stylecow.parse(color.rgbString(), 'Function'));
}

@@ -33,9 +39,12 @@ });

fn: function (fn) {
var rgba = color.toRGBA(fn);
var color = grayColor(fn.toArray());
if (rgba[3] === 1) {
fn.replaceWith((new stylecow.Hex()).setName(color.RGBA_HEX(rgba)));
} else {
fn.replaceWith(stylecow.parse('rgba(' + rgba.join(',') + ')', 'Function'));
//if its opaque
if (color.alpha() == 1) {
fn.replaceWith(stylecow.parse(color.hexString(), 'Hex'));
return;
}
//replace by rgba
fn.replaceWith(stylecow.parse(color.rgbString(), 'Function'));
}

@@ -51,113 +60,5 @@ });

fn: function (fn) {
var rgba;
var color = ColorFunction.convert(fn.toString());
fn[0].forEach(function (child, key) {
if (key === 0) {
rgba = color.toRGBA(child);
return;
}
var args = child.toArray();
switch (child.name) {
case 'alpha':
case 'a':
rgba[3] = modify(rgba[3], args[0], 1);
break;
case 'red':
rgba[0] = modify(rgba[0], args[0], 255);
break;
case 'green':
rgba[1] = modify(rgba[1], args[0], 255);
break;
case 'blue':
rgba[2] = modify(rgba[2], args[0], 255);
break;
case 'rgb':
rgba[0] = modify(rgba[0], args[0], 255);
rgba[1] = modify(rgba[1], args[1], 255);
rgba[2] = modify(rgba[2], args[2], 255);
break;
case 'saturation':
case 's':
var hsla = color.RGBA_HSLA(rgba);
hsla[1] = modify(hsla[1], args[0], 100);
rgba = color.HSLA_RGBA(hsla);
break;
case 'lightness':
case 'l':
var hsla = color.RGBA_HSLA(rgba);
hsla[2] = modify(hsla[2], args[0], 100);
rgba = color.HSLA_RGBA(hsla);
break;
case 'whiteness':
case 'w':
var hwba = color.RGBA_HWBA(rgba);
hwba[1] = modify(hwba[1], args[0], 100);
rgba = color.HWBA_RGBA(hwba);
break;
case 'blackness':
case 'b':
var hwba = color.RGBA_HWBA(rgba);
hwba[2] = modify(hwba[2], args[0], 100);
rgba = color.HWBA_RGBA(hwba);
break;
case 'blend':
var c = color.toRGBA(child[0][0]);
var p = child[0][1].toString();
rgba[0] = blend(rgba[0], c[0], p, 255);
rgba[1] = blend(rgba[1], c[1], p, 255);
rgba[2] = blend(rgba[2], c[2], p, 255);
break;
case 'blenda':
var c = color.toRGBA(child[0][0]);
var p = child[0][1].toString();
rgba[0] = blend(rgba[0], c[0], p, 255);
rgba[1] = blend(rgba[1], c[1], p, 255);
rgba[2] = blend(rgba[2], c[2], p, 255);
rgba[3] = blend(rgba[3], c[3], p, 1);
break;
case 'tint':
rgba[0] = blend(rgba[0], 255, args[0], 255);
rgba[1] = blend(rgba[1], 255, args[0], 255);
rgba[2] = blend(rgba[2], 255, args[0], 255);
break;
case 'shade':
rgba[0] = blend(rgba[0], 0, args[0], 255);
rgba[1] = blend(rgba[1], 0, args[0], 255);
rgba[2] = blend(rgba[2], 0, args[0], 255);
break;
case 'contrast':
var hsla = color.RGBA_HSLA(rgba);
var hwba = color.RGBA_HWBA(rgba);
if (hsla[2] < 50) { //is dark +50%
hwba[1] = modify(hwba[1], args[0], 100);
} else {
hwba[2] = modify(hwba[2], args[0], 100);
}
rgba = color.HWBA_RGBA(hwba);
}
});
if (rgba[3] === 1) {
fn.replaceWith((new stylecow.Hex()).setName(color.RGBA_HEX(rgba)));
} else {
fn.replaceWith(stylecow.parse('rgba(' + rgba.join(',') + ')', 'Function'));
}
fn.replaceWith(stylecow.parse(color, 'Function'));
}

@@ -168,64 +69,18 @@ });

function modify (base, value, max) {
var mode;
if (value[0] === '+' || value[0] === '-') {
mode = value[0];
value = value.substr(1);
function hexaColor (hex) {
if (hex.name.length === 4) {
return (Color('#' + hex.name.substr(0, 3))).alpha(parseFloat((parseInt(hex.name.substr(3) + hex.name.substr(3), 16) / 255).toFixed(2)));
}
if (value.indexOf('%') !== -1) {
value = ((max / 100) * parseFloat(value, 10));
} else {
value = parseFloat(value, 10);
if (hex.name.length === 8) {
return (Color('#' + hex.name.substr(0, 6))).alpha(parseFloat((parseInt(hex.name.substr(6), 16) / 255).toFixed(2)));
}
if (max === 1) {
value = parseFloat(value.toFixed(2));
} else {
value = Math.round(value);
}
if (mode === '+') {
base += value;
} else if (mode === '-') {
base -= value;
} else {
base = value;
}
if (base > max) {
return max;
}
if (base < 0) {
return 0;
}
return base;
}
function blend (base, value, percentage, max) {
percentage = parseFloat(percentage);
base = (base / 100) * percentage;
value = (value / 100) * (100 - percentage);
base += value;
if (max === 1) {
base = parseFloat(base.toFixed(2));
} else {
base = Math.round(base);
function grayColor (val) {
if (val[1] === undefined) {
val[1] = 1;
}
if (base > max) {
return max;
}
if (base < 0) {
return 0;
}
return base;
return Color('rgba(' + val[0] + ',' + val[0] + ',' + val[0] + ',' + val[1] + ')');
}

43

package.json
{
"name": "stylecow-plugin-color",
"description": "Stylecow plugin to add support for CSS Color Module Level 4",
"version": "4.0.1",
"author": "Oscar Otero <oom@oscarotero.com>",
"homepage": "https://github.com/stylecow/stylecow-plugin-color",
"bugs": "https://github.com/stylecow/stylecow-plugin-color/issues",
"repository": {
"type": "git",
"url": "https://github.com/stylecow/stylecow-plugin-color.git"
},
"main": "index.js",
"dependencies": {
"stylecow-color": "2.*"
},
"devDependencies": {
"stylecow": "6.*"
},
"scripts": {
"test": "mocha tests/cases.js"
},
"license": "MIT"
"name": "stylecow-plugin-color",
"description": "Stylecow plugin to add support for CSS Color Module Level 4",
"version": "4.1.0",
"author": "Oscar Otero <oom@oscarotero.com>",
"homepage": "https://github.com/stylecow/stylecow-plugin-color",
"bugs": "https://github.com/stylecow/stylecow-plugin-color/issues",
"repository": {
"type": "git",
"url": "https://github.com/stylecow/stylecow-plugin-color.git"
},
"main": "index.js",
"dependencies": {
"color": "^0.8.0",
"css-color-function": "^1.2.1"
},
"devDependencies": {
"stylecow": "6.*"
},
"scripts": {
"test": "mocha tests/cases.js"
},
"license": "MIT"
}

@@ -33,4 +33,34 @@ {

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -74,4 +104,34 @@ ]

{
"class": "Hex",
"name": "7F7FFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -91,4 +151,34 @@ ]

{
"class": "Hex",
"name": "BFBFFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 64
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 64
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -141,3 +231,3 @@ ]

"class": "Number",
"name": 127
"name": 128
}

@@ -151,3 +241,3 @@ ]

"class": "Number",
"name": 127
"name": 128
}

@@ -198,3 +288,3 @@ ]

"class": "Number",
"name": 191
"name": 64
}

@@ -208,3 +298,3 @@ ]

"class": "Number",
"name": 191
"name": 64
}

@@ -270,4 +360,34 @@ ]

{
"class": "Hex",
"name": "800000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -287,4 +407,34 @@ ]

{
"class": "Hex",
"name": "000080"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
}
]
}

@@ -304,4 +454,34 @@ ]

{
"class": "Hex",
"name": "008000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -321,4 +501,34 @@ ]

{
"class": "Hex",
"name": "808080"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
}
]
}

@@ -362,4 +572,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -379,4 +619,34 @@ ]

{
"class": "Hex",
"name": "808080"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
}
]
}

@@ -396,4 +666,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -413,4 +713,34 @@ ]

{
"class": "Hex",
"name": "808080"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
}
]
}

@@ -454,4 +784,34 @@ ]

{
"class": "Hex",
"name": "FFFFFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -471,4 +831,34 @@ ]

{
"class": "Hex",
"name": "000000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -488,4 +878,34 @@ ]

{
"class": "Hex",
"name": "FFFFFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -505,4 +925,34 @@ ]

{
"class": "Hex",
"name": "000000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -546,4 +996,34 @@ ]

{
"class": "Hex",
"name": "FFFFFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -563,4 +1043,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -580,4 +1090,34 @@ ]

{
"class": "Hex",
"name": "FFFFFF"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -597,4 +1137,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -638,4 +1208,34 @@ ]

{
"class": "Hex",
"name": "000000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -655,4 +1255,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -672,4 +1302,34 @@ ]

{
"class": "Hex",
"name": "000000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -689,4 +1349,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -730,4 +1420,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -747,4 +1467,34 @@ ]

{
"class": "Hex",
"name": "C00019"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -764,4 +1514,34 @@ ]

{
"class": "Hex",
"name": "800032"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -805,4 +1585,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 128
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -822,4 +1632,34 @@ ]

{
"class": "Hex",
"name": "C00019"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 204
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -839,4 +1679,34 @@ ]

{
"class": "Hex",
"name": "800032"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 0
}
]
}
]
}

@@ -880,4 +1750,34 @@ ]

{
"class": "Hex",
"name": "7F0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -897,4 +1797,34 @@ ]

{
"class": "Hex",
"name": "330000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -914,4 +1844,34 @@ ]

{
"class": "Hex",
"name": "FF0000"
"class": "Function",
"name": "rgb",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
},
{
"class": "Value",
"children": [
{
"class": "Number",
"name": 255
}
]
}
]
}

@@ -925,76 +1885,4 @@ ]

]
},
{
"class": "Rule",
"children": [
{
"class": "Selectors",
"children": [
{
"class": "Selector",
"children": [
{
"class": "TypeSelector",
"name": "div"
}
]
}
]
},
{
"class": "Block",
"children": [
{
"class": "Declaration",
"name": "color",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Hex",
"name": "BF0000"
}
]
}
]
},
{
"class": "Declaration",
"name": "color",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Hex",
"name": "800000"
}
]
}
]
},
{
"class": "Declaration",
"name": "color",
"vendor": null,
"children": [
{
"class": "Value",
"children": [
{
"class": "Hex",
"name": "000000"
}
]
}
]
}
]
}
]
}
]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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