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

rgb-hex

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rgb-hex - npm Package Compare versions

Comparing version 4.0.1 to 4.1.0

48

index.js

@@ -0,6 +1,16 @@

// TODO: Remove this ignore comment.
// eslint-disable-next-line no-mixed-operators
const toHex = (red, green, blue, alpha) => ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha;
export default function rgbHex(red, green, blue, alpha) {
const isPercent = (red + (alpha || '')).toString().includes('%');
let isPercent = (red + (alpha || '')).toString().includes('%');
if (typeof red === 'string') {
[red, green, blue, alpha] = red.match(/(0?\.?\d+)%?\b/g).map(component => Number(component));
if (typeof red === 'string' && !green) { // Single string parameter.
const parsed = parseCssRgbString(red);
if (!parsed) {
throw new TypeError('Invalid or unsupported color format.');
}
isPercent = false;
[red, green, blue, alpha] = parsed;
} else if (alpha !== undefined) {

@@ -34,5 +44,31 @@ alpha = Number.parseFloat(alpha);

// TODO: Remove this ignore comment.
// eslint-disable-next-line no-mixed-operators
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha;
return toHex(red, green, blue, alpha);
}
const parseCssRgbString = input => {
const parts = input.replace(/rgba?\(([^)]+)\)/, '$1').split(/[,\s/]+/).filter(Boolean);
if (parts.length < 3) {
return;
}
const parseValue = (value, max) => {
value = value.trim();
if (value.endsWith('%')) {
return Math.min(Number.parseFloat(value) * max / 100, max);
}
return Math.min(Number.parseFloat(value), max);
};
const red = parseValue(parts[0], 255);
const green = parseValue(parts[1], 255);
const blue = parseValue(parts[2], 255);
let alpha;
if (parts.length === 4) {
alpha = parseValue(parts[3], 1);
}
return [red, green, blue, alpha];
};

2

package.json
{
"name": "rgb-hex",
"version": "4.0.1",
"version": "4.1.0",
"description": "Convert RGB(A) color to HEX",

@@ -5,0 +5,0 @@ "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