Skip to content

Commit

Permalink
don't fix alpha to 2 decimal points (ref qix-/color#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Oct 9, 2020
1 parent ad1e511 commit 6f7d6f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -69,7 +69,7 @@ cs.get.rgb = function (string) {
}

if (hexAlpha) {
rgb[3] = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
rgb[3] = parseInt(hexAlpha, 16) / 255;
}
} else if (match = string.match(abbr)) {
match = match[1];
Expand All @@ -80,7 +80,7 @@ cs.get.rgb = function (string) {
}

if (hexAlpha) {
rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
}
} else if (match = string.match(rgba)) {
for (i = 0; i < 3; i++) {
Expand Down
13 changes: 11 additions & 2 deletions test/basic.js
@@ -1,6 +1,15 @@
var assert = require('assert');
var string = require('../');

function normalizeAlpha(res) {
if (res.model === 'rgb' && res.value.length >= 4) {
res.value[3] = res.value[3].toFixed(2);
} else if (res.length >= 4) {
res[3] = res[3].toFixed(2);
}
return res;
}

assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]);
assert.deepEqual(string.get.rgb('#fffFEF'), [255, 255, 239, 1]);
assert.deepEqual(string.get.rgb('rgb(244, 233, 100)'), [244, 233, 100, 1]);
Expand All @@ -16,7 +25,7 @@ assert.deepEqual(string.get('#fef'), {model: 'rgb', value: [255, 238, 255, 1]});
assert.deepEqual(string.get('#fffFEF'), {model: 'rgb', value: [255, 255, 239, 1]});
assert.deepEqual(string.get('#fffFEFff'), {model: 'rgb', value: [255, 255, 239, 1]});
assert.deepEqual(string.get('#fffFEF00'), {model: 'rgb', value: [255, 255, 239, 0]});
assert.deepEqual(string.get('#fffFEFa9'), {model: 'rgb', value: [255, 255, 239, 0.66]});
assert.deepEqual(normalizeAlpha(string.get('#fffFEFa9')), {model: 'rgb', value: [255, 255, 239, '0.66']});
assert.deepEqual(string.get('rgb(244, 233, 100)'), {model: 'rgb', value: [244, 233, 100, 1]});
assert.deepEqual(string.get('rgb(100%, 30%, 90%)'), {model: 'rgb', value: [255, 77, 229, 1]});
assert.deepEqual(string.get('transparent'), {model: 'rgb', value: [0, 0, 0, 0]});
Expand Down Expand Up @@ -55,7 +64,7 @@ assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]);
assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]);

// alpha
assert.deepEqual(string.get.rgb('#fffa'), [255, 255, 255, 0.67]);
assert.deepEqual(normalizeAlpha(string.get.rgb('#fffa')), [255, 255, 255, '0.67']);
assert.deepEqual(string.get.rgb('#c814e933'), [200, 20, 233, 0.2]);
assert.deepEqual(string.get.rgb('#c814e900'), [200, 20, 233, 0]);
assert.deepEqual(string.get.rgb('#c814e9ff'), [200, 20, 233, 1]);
Expand Down

0 comments on commit 6f7d6f1

Please sign in to comment.