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

@color2k/parse-to-rgba

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@color2k/parse-to-rgba - npm Package Compare versions

Comparing version 0.0.0-17a9eb32d to 0.0.0-218d7ef15

84

index.esm.js

@@ -9,15 +9,19 @@ class ColorError extends Error {

const cache = {};
let ctx = null;
let div = null;
function getContext() {
const canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
return canvas.getContext('2d');
function createDiv() {
const div = document.createElement('div');
div.classList.add('color2k-parser');
div.style.opacity = '0';
div.style.pointerEvents = 'none';
div.style.position = 'fixed'; // div must be mounted for `getComputedStyle` to work
document.body.appendChild(div);
return div;
}
/**
* Parses a color using canvas
* Parses a color using `getComputedStyle`
*
* Idea from `@Alnitak` this stackoverflow answer:
* https://stackoverflow.com/a/19366389/5776910
* Idea from `Niet the Dark Absol`'s stackoverflow answer:
* https://stackoverflow.com/a/11068286/5776910
*/

@@ -27,42 +31,44 @@

function parseToRgba(color) {
// for node-environments, we'll use @color2k/node
if (typeof document === 'undefined') {
// to enable this to work in any environment, you can provide `color2kCompat`
// globally as an escape hatch.
// @ts-ignore
if (typeof color2kCompat !== 'undefined') {
// @ts-ignore
return color2kCompat(color);
} // for non-browser-environments, we'll use @color2k/compat
return require('@color2k/node')(color);
} // normalize the color
if (typeof navigator !== 'undefined' && navigator.userAgent.includes('jsdom') || typeof document === 'undefined' && typeof require !== 'undefined') {
// Need to trick bundlers into _not_ bundling @color2k/compat. The
// expression in the require statement makes it so that bundlers can't
// statically resolve the dependency. It should be pulled on demand when
// ran in a node environment.
const someRandomExpression = '@color2k';
const someRandomValue = (() => '/compat')(); // @ts-ignore
// eslint-disable-next-line
const normalizedColor = color.toLowerCase().trim();
if (cache[normalizedColor]) {
return cache[normalizedColor];
}
return require(someRandomExpression + someRandomValue)(color);
} // normalize the color
ctx = ctx || getContext();
if (!ctx) {
throw new Error('Failed to get 2D context');
}
const normalizedColor = color.toLowerCase().trim();
if (cache[normalizedColor]) return cache[normalizedColor];
div = div || createDiv();
div.style.color = '#000';
div.style.color = normalizedColor;
const control = getComputedStyle(div).color;
div.style.color = '#fff';
div.style.color = normalizedColor;
const computedColor = getComputedStyle(div).color;
if (computedColor !== control) throw new ColorError(color);
const result = computedColor.replace(/[^\d.,]/g, '').split(',').map(parseFloat);
ctx.clearRect(0, 0, 1, 1); // In order to detect invalid values,
// we can't rely on col being in the same format as what fillStyle is computed as,
// but we can ask it to implicitly compute a normalized value twice and compare.
// https://stackoverflow.com/a/19366389/5776910
ctx.fillStyle = '#000';
ctx.fillStyle = normalizedColor;
const computed = ctx.fillStyle;
ctx.fillStyle = '#fff';
ctx.fillStyle = normalizedColor;
if (computed !== ctx.fillStyle) {
throw new ColorError(color);
if (result.length < 4) {
result.push(1);
}
ctx.fillRect(0, 0, 1, 1);
const result = Array.from(ctx.getImageData(0, 0, 1, 1).data);
const withNormalizedAlpha = [...result.slice(0, 3), result[3] / 255];
cache[normalizedColor] = withNormalizedAlpha;
return withNormalizedAlpha;
cache[normalizedColor] = result;
return result;
}

@@ -69,0 +75,0 @@

@@ -131,3 +131,3 @@ (function (global, factory) {

return function () {
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),

@@ -148,35 +148,2 @@ result;

function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var ColorError = /*#__PURE__*/function (_Error) {

@@ -197,15 +164,19 @@ _inherits(ColorError, _Error);

var cache = {};
var ctx = null;
var div = null;
function getContext() {
var canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
return canvas.getContext('2d');
function createDiv() {
var div = document.createElement('div');
div.classList.add('color2k-parser');
div.style.opacity = '0';
div.style.pointerEvents = 'none';
div.style.position = 'fixed'; // div must be mounted for `getComputedStyle` to work
document.body.appendChild(div);
return div;
}
/**
* Parses a color using canvas
* Parses a color using `getComputedStyle`
*
* Idea from `@Alnitak` this stackoverflow answer:
* https://stackoverflow.com/a/19366389/5776910
* Idea from `Niet the Dark Absol`'s stackoverflow answer:
* https://stackoverflow.com/a/11068286/5776910
*/

@@ -215,42 +186,46 @@

function parseToRgba(color) {
// for node-environments, we'll use @color2k/node
if (typeof document === 'undefined') {
// to enable this to work in any environment, you can provide `color2kCompat`
// globally as an escape hatch.
// @ts-ignore
if (typeof color2kCompat !== 'undefined') {
// @ts-ignore
return color2kCompat(color);
} // for non-browser-environments, we'll use @color2k/compat
return require('@color2k/node')(color);
} // normalize the color
if (typeof navigator !== 'undefined' && navigator.userAgent.includes('jsdom') || typeof document === 'undefined' && typeof require !== 'undefined') {
// Need to trick bundlers into _not_ bundling @color2k/compat. The
// expression in the require statement makes it so that bundlers can't
// statically resolve the dependency. It should be pulled on demand when
// ran in a node environment.
var someRandomExpression = '@color2k';
var someRandomValue = function () {
return '/compat';
}(); // @ts-ignore
// eslint-disable-next-line
var normalizedColor = color.toLowerCase().trim();
if (cache[normalizedColor]) {
return cache[normalizedColor];
}
return require(someRandomExpression + someRandomValue)(color);
} // normalize the color
ctx = ctx || getContext();
if (!ctx) {
throw new Error('Failed to get 2D context');
}
var normalizedColor = color.toLowerCase().trim();
if (cache[normalizedColor]) return cache[normalizedColor];
div = div || createDiv();
div.style.color = '#000';
div.style.color = normalizedColor;
var control = getComputedStyle(div).color;
div.style.color = '#fff';
div.style.color = normalizedColor;
var computedColor = getComputedStyle(div).color;
if (computedColor !== control) throw new ColorError(color);
var result = computedColor.replace(/[^\d.,]/g, '').split(',').map(parseFloat);
ctx.clearRect(0, 0, 1, 1); // In order to detect invalid values,
// we can't rely on col being in the same format as what fillStyle is computed as,
// but we can ask it to implicitly compute a normalized value twice and compare.
// https://stackoverflow.com/a/19366389/5776910
ctx.fillStyle = '#000';
ctx.fillStyle = normalizedColor;
var computed = ctx.fillStyle;
ctx.fillStyle = '#fff';
ctx.fillStyle = normalizedColor;
if (computed !== ctx.fillStyle) {
throw new ColorError(color);
if (result.length < 4) {
result.push(1);
}
ctx.fillRect(0, 0, 1, 1);
var result = Array.from(ctx.getImageData(0, 0, 1, 1).data);
var withNormalizedAlpha = [].concat(_toConsumableArray(result.slice(0, 3)), [result[3] / 255]);
cache[normalizedColor] = withNormalizedAlpha;
return withNormalizedAlpha;
cache[normalizedColor] = result;
return result;
}

@@ -257,0 +232,0 @@

{
"name": "@color2k/parse-to-rgba",
"version": "0.0.0-17a9eb32d",
"description": "a low-level color parser that uses canvas to parse a colors",
"main": "./index.js",
"author": {
"email": "ricokahler@me.com",
"name": "Rico Kahler"
},
"version": "0.0.0-218d7ef15",
"description": "a low-level color parser that uses getComputedStyle to parse a colors",
"repository": {

@@ -15,2 +10,7 @@ "type": "git",

"license": "MIT",
"author": {
"name": "Rico Kahler",
"email": "ricokahler@me.com"
},
"main": "./index.js",
"types": "./src",

@@ -20,5 +20,5 @@ "module": "./index.esm.js",

"peerDependencies": {
"@color2k/node": "0.0.0-17a9eb32d"
"@color2k/compat": "0.0.0-218d7ef15"
},
"sideEffects": false
}
/**
* Parses a color using canvas
* Parses a color using `getComputedStyle`
*
* Idea from `@Alnitak` this stackoverflow answer:
* https://stackoverflow.com/a/19366389/5776910
* Idea from `Niet the Dark Absol`'s stackoverflow answer:
* https://stackoverflow.com/a/11068286/5776910
*/

@@ -7,0 +7,0 @@ declare function parseToRgba(color: string): [number, number, number, number];

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