Comparing version 2.0.0 to 2.1.0
24
index.js
'use strict'; | ||
/* eslint-disable no-mixed-operators */ | ||
module.exports = (red, green, blue) => { | ||
module.exports = (red, green, blue, alpha) => { | ||
const isPercent = (red + (alpha || '')).toString().includes('%'); | ||
if (typeof red === 'string') { | ||
const res = red.match(/\b\d{1,3}\b/g).map(Number); | ||
const res = red.match(/(0?\.?\d{1,3})%?\b/g).map(Number); | ||
// TODO: use destructuring when targeting Node.js 6 | ||
@@ -10,2 +12,5 @@ red = res[0]; | ||
blue = res[2]; | ||
alpha = res[3]; | ||
} else if (alpha !== undefined) { | ||
alpha = parseFloat(alpha); | ||
} | ||
@@ -22,3 +27,16 @@ | ||
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1); | ||
if (typeof alpha === 'number') { | ||
if (!isPercent && alpha >= 0 && alpha <= 1) { | ||
alpha = Math.round(255 * alpha); | ||
} else if (isPercent && alpha >= 0 && alpha <= 100) { | ||
alpha = Math.round(255 * alpha / 100); | ||
} else { | ||
throw new TypeError(`Expected alpha value (${alpha}) as a fraction or percentage`); | ||
} | ||
alpha = (alpha | 1 << 8).toString(16).slice(1); | ||
} else { | ||
alpha = ''; | ||
} | ||
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha; | ||
}; |
{ | ||
"name": "rgb-hex", | ||
"version": "2.0.0", | ||
"description": "Convert RGB color to HEX", | ||
"version": "2.1.0", | ||
"description": "Convert RGB(A) color to HEX", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "sindresorhus/rgb-hex", |
# rgb-hex [![Build Status](https://travis-ci.org/sindresorhus/rgb-hex.svg?branch=master)](https://travis-ci.org/sindresorhus/rgb-hex) | ||
> Convert RGB color to HEX | ||
> Convert RGB(A) color to HEX | ||
@@ -23,2 +23,11 @@ | ||
//=> '282a36' | ||
rgbHex(65, 131, 196, 0.2); | ||
//=> '4183c433' | ||
rgbHex(40, 42, 54, '75%'); | ||
//=> '282a36c0' | ||
rgbHex('rgba(40, 42, 54, 75%)'); | ||
//=> '282a36c0' | ||
``` | ||
@@ -25,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3585
36
44