Skip to content

Commit

Permalink
The alpha should always be a float percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 13, 2018
1 parent cd19d4d commit 068420a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -13,7 +13,7 @@ module.exports = function (hex, options = {}) {
}

hex = hex.replace(/^#/, '');
let alpha = 255;
let alpha = 1;

if (hex.length === 8) {
alpha = parseInt(hex.slice(6, 8), 16) / 255;
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -16,13 +16,13 @@ $ npm install hex-rgb
const hexRgb = require('hex-rgb');

hexRgb('4183c4');
//=> {red: 65, green: 131, blue: 196, alpha: 255}
//=> {red: 65, green: 131, blue: 196, alpha: 1}

hexRgb('#4183c4');
//=> {red: 65, green: 131, blue: 196, alpha: 255}
//=> {red: 65, green: 131, blue: 196, alpha: 1}

hexRgb('#fff');
//=> {red: 255, green: 255, blue: 255, alpha: 255}
//=> {red: 255, green: 255, blue: 255, alpha: 1}

hexRgb('#22222299');
//=> {red: 34, green: 34, blue: 34, alpha: 0.6}
Expand Down
12 changes: 6 additions & 6 deletions test.js
Expand Up @@ -19,22 +19,22 @@ test('rejects', t => {

test('hex; output object', t => {
const options = {format: 'object'};
t.deepEqual(hexRgb('#4183c4', options), {red: 65, green: 131, blue: 196, alpha: 255});
t.deepEqual(hexRgb('#000', options), {red: 0, green: 0, blue: 0, alpha: 255});
t.deepEqual(hexRgb('#4183c4', options), {red: 65, green: 131, blue: 196, alpha: 1});
t.deepEqual(hexRgb('#000', options), {red: 0, green: 0, blue: 0, alpha: 1});
t.deepEqual(hexRgb('#0006', options), {red: 0, green: 0, blue: 0, alpha: 0.4});
t.deepEqual(hexRgb('4183c488', options), {red: 65, green: 131, blue: 196, alpha: 0.5333333333333333});
t.deepEqual(hexRgb('#4183c488'), {red: 65, green: 131, blue: 196, alpha: 0.5333333333333333});
t.deepEqual(hexRgb('4183c4'), {red: 65, green: 131, blue: 196, alpha: 255});
t.deepEqual(hexRgb('4183c4'), {red: 65, green: 131, blue: 196, alpha: 1});
t.deepEqual(hexRgb('#000f'), {red: 0, green: 0, blue: 0, alpha: 1});
t.deepEqual(hexRgb('#22222299'), {red: 34, green: 34, blue: 34, alpha: 0.6});
t.deepEqual(hexRgb('#cd2222cc'), {red: 205, green: 34, blue: 34, alpha: 0.8});
});

test('hex; output array', t => {
const options = {format: 'array'};
t.deepEqual(hexRgb('4183c4', options), [65, 131, 196, 255]);
t.deepEqual(hexRgb('#4183c4', options), [65, 131, 196, 255]);
t.deepEqual(hexRgb('#000', options), [0, 0, 0, 255]);
t.deepEqual(hexRgb('4183c4', options), [65, 131, 196, 1]);
t.deepEqual(hexRgb('#4183c4', options), [65, 131, 196, 1]);
t.deepEqual(hexRgb('#000', options), [0, 0, 0, 1]);
t.deepEqual(hexRgb('4183c488', options), [65, 131, 196, 0.5333333333333333]);
t.deepEqual(hexRgb('#4183c488', options), [65, 131, 196, 0.5333333333333333]);
t.deepEqual(hexRgb('#0008', options), [0, 0, 0, 0.5333333333333333]);
Expand Down

0 comments on commit 068420a

Please sign in to comment.