New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

color-string

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

color-string - npm Package Compare versions

Comparing version 1.9.1 to 2.0.0

index.d.ts

164

index.js

@@ -1,11 +0,8 @@

/* MIT license */
var colorNames = require('color-name');
var swizzle = require('simple-swizzle');
var hasOwnProperty = Object.hasOwnProperty;
import colorNames from 'color-name';
var reverseNames = Object.create(null);
const reverseNames = Object.create(null);
// create a list of reverse color names
for (var name in colorNames) {
if (hasOwnProperty.call(colorNames, name)) {
// Create a list of reverse color names
for (const name in colorNames) {
if (Object.hasOwn(colorNames, name)) {
reverseNames[colorNames[name]] = name;

@@ -15,31 +12,36 @@ }

var cs = module.exports = {
const cs = {
to: {},
get: {}
get: {},
};
cs.get = function (string) {
var prefix = string.substring(0, 3).toLowerCase();
var val;
var model;
const prefix = string.slice(0, 3).toLowerCase();
let value;
let model;
switch (prefix) {
case 'hsl':
val = cs.get.hsl(string);
case 'hsl': {
value = cs.get.hsl(string);
model = 'hsl';
break;
case 'hwb':
val = cs.get.hwb(string);
}
case 'hwb': {
value = cs.get.hwb(string);
model = 'hwb';
break;
default:
val = cs.get.rgb(string);
}
default: {
value = cs.get.rgb(string);
model = 'rgb';
break;
}
}
if (!val) {
if (!value) {
return null;
}
return {model: model, value: val};
return {model, value};
};

@@ -52,12 +54,12 @@

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*(?:,\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+)$/;
const abbr = /^#([a-f\d]{3,4})$/i;
const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i;
const rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/;
const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/;
const keyword = /^(\w+)$/;
var rgb = [0, 0, 0, 1];
var match;
var i;
var hexAlpha;
let rgb = [0, 0, 0, 1];
let match;
let i;
let hexAlpha;

@@ -70,8 +72,8 @@ if (match = string.match(hex)) {

// https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19
var i2 = i * 2;
rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
const i2 = i * 2;
rgb[i] = Number.parseInt(match.slice(i2, i2 + 2), 16);
}
if (hexAlpha) {
rgb[3] = parseInt(hexAlpha, 16) / 255;
rgb[3] = Number.parseInt(hexAlpha, 16) / 255;
}

@@ -83,31 +85,23 @@ } else if (match = string.match(abbr)) {

for (i = 0; i < 3; i++) {
rgb[i] = parseInt(match[i] + match[i], 16);
rgb[i] = Number.parseInt(match[i] + match[i], 16);
}
if (hexAlpha) {
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
rgb[3] = Number.parseInt(hexAlpha + hexAlpha, 16) / 255;
}
} else if (match = string.match(rgba)) {
for (i = 0; i < 3; i++) {
rgb[i] = parseInt(match[i + 1], 0);
rgb[i] = Number.parseInt(match[i + 1], 10);
}
if (match[4]) {
if (match[5]) {
rgb[3] = parseFloat(match[4]) * 0.01;
} else {
rgb[3] = parseFloat(match[4]);
}
rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]);
}
} else if (match = string.match(per)) {
for (i = 0; i < 3; i++) {
rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55);
rgb[i] = Math.round(Number.parseFloat(match[i + 1]) * 2.55);
}
if (match[4]) {
if (match[5]) {
rgb[3] = parseFloat(match[4]) * 0.01;
} else {
rgb[3] = parseFloat(match[4]);
}
rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]);
}

@@ -119,3 +113,3 @@ } else if (match = string.match(keyword)) {

if (!hasOwnProperty.call(colorNames, match[1])) {
if (!Object.hasOwn(colorNames, match[1])) {
return null;

@@ -135,2 +129,3 @@ }

}
rgb[3] = clamp(rgb[3], 0, 1);

@@ -146,11 +141,11 @@

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);
const 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*)?\)$/;
const match = string.match(hsl);
if (match) {
var alpha = parseFloat(match[4]);
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);
const alpha = Number.parseFloat(match[4]);
const h = ((Number.parseFloat(match[1]) % 360) + 360) % 360;
const s = clamp(Number.parseFloat(match[2]), 0, 100);
const l = clamp(Number.parseFloat(match[3]), 0, 100);
const a = clamp(Number.isNaN(alpha) ? 1 : alpha, 0, 1);

@@ -168,11 +163,11 @@ return [h, s, l, a];

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);
const 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*)?\)$/;
const match = string.match(hwb);
if (match) {
var alpha = parseFloat(match[4]);
var h = ((parseFloat(match[1]) % 360) + 360) % 360;
var w = clamp(parseFloat(match[2]), 0, 100);
var b = clamp(parseFloat(match[3]), 0, 100);
var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1);
const alpha = Number.parseFloat(match[4]);
const h = ((Number.parseFloat(match[1]) % 360) + 360) % 360;
const w = clamp(Number.parseFloat(match[2]), 0, 100);
const b = clamp(Number.parseFloat(match[3]), 0, 100);
const a = clamp(Number.isNaN(alpha) ? 1 : alpha, 0, 1);
return [h, w, b, a];

@@ -184,5 +179,3 @@ }

cs.to.hex = function () {
var rgba = swizzle(arguments);
cs.to.hex = function (...rgba) {
return (

@@ -199,5 +192,3 @@ '#' +

cs.to.rgb = function () {
var rgba = swizzle(arguments);
cs.to.rgb = function (...rgba) {
return rgba.length < 4 || rgba[3] === 1

@@ -208,9 +199,7 @@ ? 'rgb(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ')'

cs.to.rgb.percent = function () {
var rgba = swizzle(arguments);
cs.to.rgb.percent = function (...rgba) {
const r = Math.round(rgba[0] / 255 * 100);
const g = Math.round(rgba[1] / 255 * 100);
const b = Math.round(rgba[2] / 255 * 100);
var r = Math.round(rgba[0] / 255 * 100);
var g = Math.round(rgba[1] / 255 * 100);
var b = Math.round(rgba[2] / 255 * 100);
return rgba.length < 4 || rgba[3] === 1

@@ -221,4 +210,3 @@ ? 'rgb(' + r + '%, ' + g + '%, ' + b + '%)'

cs.to.hsl = function () {
var hsla = swizzle(arguments);
cs.to.hsl = function (...hsla) {
return hsla.length < 4 || hsla[3] === 1

@@ -229,8 +217,6 @@ ? 'hsl(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%)'

// hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax
// Hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax
// (hwb have alpha optional & 1 is default value)
cs.to.hwb = function () {
var hwba = swizzle(arguments);
var a = '';
cs.to.hwb = function (...hwba) {
let a = '';
if (hwba.length >= 4 && hwba[3] !== 1) {

@@ -243,14 +229,16 @@ a = ', ' + hwba[3];

cs.to.keyword = function (rgb) {
cs.to.keyword = function (...rgb) {
return reverseNames[rgb.slice(0, 3)];
};
// helpers
function clamp(num, min, max) {
return Math.min(Math.max(min, num), max);
// Helpers
function clamp(number_, min, max) {
return Math.min(Math.max(min, number_), max);
}
function hexDouble(num) {
var str = Math.round(num).toString(16).toUpperCase();
return (str.length < 2) ? '0' + str : str;
function hexDouble(number_) {
const string_ = Math.round(number_).toString(16).toUpperCase();
return (string_.length < 2) ? '0' + string_ : string_;
}
export default cs;
{
"name": "color-string",
"description": "Parser and generator for CSS color strings",
"version": "1.9.1",
"version": "2.0.0",
"author": "Heather Arthur <fayearthur@gmail.com>",

@@ -12,9 +12,15 @@ "contributors": [

"repository": "Qix-/color-string",
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"engines": {
"node": ">=18"
},
"scripts": {
"pretest": "xo",
"test": "node test/basic.js"
"test": "xo && tsd && node test.js"
},
"license": "MIT",
"files": [
"index.js"
"index.js",
"index.d.ts"
],

@@ -28,7 +34,7 @@ "xo": {

"dependencies": {
"color-name": "^1.0.0",
"simple-swizzle": "^0.2.2"
"color-name": "^2.0.0"
},
"devDependencies": {
"xo": "^0.12.1"
"tsd": "^0.31.2",
"xo": "^0.60.0"
},

@@ -35,0 +41,0 @@ "keywords": [

@@ -7,6 +7,4 @@ # color-string

With [npm](http://npmjs.org/):
```console
$ npm install color-string
```sh
npm install color-string
```

@@ -48,17 +46,16 @@

```js
colorString.to.hex([255, 255, 255]) // "#FFFFFF"
colorString.to.hex([0, 0, 255, 0.4]) // "#0000FF66"
colorString.to.hex([0, 0, 255], 0.4) // "#0000FF66"
colorString.to.rgb([255, 255, 255]) // "rgb(255, 255, 255)"
colorString.to.rgb([0, 0, 255, 0.4]) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb([0, 0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb.percent([0, 0, 255]) // "rgb(0%, 0%, 100%)"
colorString.to.keyword([255, 255, 0]) // "yellow"
colorString.to.hsl([360, 100, 100]) // "hsl(360, 100%, 100%)"
colorString.to.hwb([50, 3, 15]) // "hwb(50, 3%, 15%)"
colorString.to.hex(255, 255, 255) // "#FFFFFF"
colorString.to.hex(0, 0, 255, 0.4) // "#0000FF66"
colorString.to.hex(0, 0, 255, 0.4) // "#0000FF66"
colorString.to.rgb(255, 255, 255) // "rgb(255, 255, 255)"
colorString.to.rgb(0, 0, 255, 0.4) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb(0, 0, 255, 0.4) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb.percent(0, 0, 255) // "rgb(0%, 0%, 100%)"
colorString.to.keyword(255, 255, 0) // "yellow"
colorString.to.hsl(360, 100, 100) // "hsl(360, 100%, 100%)"
colorString.to.hwb(50, 3, 15) // "hwb(50, 3%, 15%)"
```
// all functions also support swizzling
colorString.to.rgb(0, [0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb([0, 0], [255], 0.4) // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb([0], 0, [255, 0.4]) // "rgba(0, 0, 255, 0.4)"
```
## License
MIT
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