Skip to content

Commit

Permalink
add support for alpha channel (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie authored and sindresorhus committed Nov 4, 2016
1 parent 19cb8cf commit c638bbd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
24 changes: 21 additions & 3 deletions index.js
@@ -1,12 +1,17 @@
'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
red = res[0];
green = res[1];
blue = res[2];
alpha = res[3];
} else if (alpha !== undefined) {
alpha = parseFloat(alpha);
}

if (typeof red !== 'number' ||
Expand All @@ -18,5 +23,18 @@ module.exports = (red, green, blue) => {
throw new TypeError('Expected three numbers below 256');
}

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;
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "rgb-hex",
"version": "2.0.0",
"description": "Convert RGB color to HEX",
"description": "Convert RGB(A) color to HEX",
"license": "MIT",
"repository": "sindresorhus/rgb-hex",
"author": {
Expand Down
11 changes: 10 additions & 1 deletion readme.md
@@ -1,6 +1,6 @@
# 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

## Install
Expand All @@ -20,6 +20,15 @@ rgbHex(65, 131, 196);

rgbHex('rgb(40, 42, 54)');
//=> '282a36'

rgbHex(65, 131, 196, 0.2);
//=> '4183c433'

rgbHex(40, 42, 54, '75%');
//=> '282a36c0'

rgbHex('rgba(40, 42, 54, 75%)');
//=> '282a36c0'
```


Expand Down
10 changes: 10 additions & 0 deletions test.js
Expand Up @@ -6,4 +6,14 @@ test(t => {
t.is(m(65, 131, 196), '4183c4');
t.is(m('255 154 253'), 'ff9afd');
t.is(m('rgb(40, 42, 54)'), '282a36');
t.is(m(0, 0, 0, 0), '00000000');
t.is(m(0, 0, 0, 0.5), '00000080');
t.is(m(0, 0, 0, '50%'), '00000080');
t.is(m(0, 0, 0, '100%'), '000000ff');
t.is(m(65, 131, 196, 0.2), '4183c433');
t.is(m('255 154 253, 0.8'), 'ff9afdcc');
t.is(m('160 82 45 .4'), 'a0522d66');
t.is(m(40, 42, 54, 0.75), '282a36bf');
t.is(m(40, 42, 54, '75%'), '282a36bf');
t.is(m('rgba(40, 42, 54, 75%)'), '282a36bf');
});

0 comments on commit c638bbd

Please sign in to comment.