Skip to content

Commit 6f7d6f1

Browse files
committed
don't fix alpha to 2 decimal points (ref qix-/color#174)
1 parent ad1e511 commit 6f7d6f1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ cs.get.rgb = function (string) {
6969
}
7070

7171
if (hexAlpha) {
72-
rgb[3] = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
72+
rgb[3] = parseInt(hexAlpha, 16) / 255;
7373
}
7474
} else if (match = string.match(abbr)) {
7575
match = match[1];
@@ -80,7 +80,7 @@ cs.get.rgb = function (string) {
8080
}
8181

8282
if (hexAlpha) {
83-
rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
83+
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
8484
}
8585
} else if (match = string.match(rgba)) {
8686
for (i = 0; i < 3; i++) {

test/basic.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
var assert = require('assert');
22
var string = require('../');
33

4+
function normalizeAlpha(res) {
5+
if (res.model === 'rgb' && res.value.length >= 4) {
6+
res.value[3] = res.value[3].toFixed(2);
7+
} else if (res.length >= 4) {
8+
res[3] = res[3].toFixed(2);
9+
}
10+
return res;
11+
}
12+
413
assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]);
514
assert.deepEqual(string.get.rgb('#fffFEF'), [255, 255, 239, 1]);
615
assert.deepEqual(string.get.rgb('rgb(244, 233, 100)'), [244, 233, 100, 1]);
@@ -16,7 +25,7 @@ assert.deepEqual(string.get('#fef'), {model: 'rgb', value: [255, 238, 255, 1]});
1625
assert.deepEqual(string.get('#fffFEF'), {model: 'rgb', value: [255, 255, 239, 1]});
1726
assert.deepEqual(string.get('#fffFEFff'), {model: 'rgb', value: [255, 255, 239, 1]});
1827
assert.deepEqual(string.get('#fffFEF00'), {model: 'rgb', value: [255, 255, 239, 0]});
19-
assert.deepEqual(string.get('#fffFEFa9'), {model: 'rgb', value: [255, 255, 239, 0.66]});
28+
assert.deepEqual(normalizeAlpha(string.get('#fffFEFa9')), {model: 'rgb', value: [255, 255, 239, '0.66']});
2029
assert.deepEqual(string.get('rgb(244, 233, 100)'), {model: 'rgb', value: [244, 233, 100, 1]});
2130
assert.deepEqual(string.get('rgb(100%, 30%, 90%)'), {model: 'rgb', value: [255, 77, 229, 1]});
2231
assert.deepEqual(string.get('transparent'), {model: 'rgb', value: [0, 0, 0, 0]});
@@ -55,7 +64,7 @@ assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]);
5564
assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]);
5665

5766
// alpha
58-
assert.deepEqual(string.get.rgb('#fffa'), [255, 255, 255, 0.67]);
67+
assert.deepEqual(normalizeAlpha(string.get.rgb('#fffa')), [255, 255, 255, '0.67']);
5968
assert.deepEqual(string.get.rgb('#c814e933'), [200, 20, 233, 0.2]);
6069
assert.deepEqual(string.get.rgb('#c814e900'), [200, 20, 233, 0]);
6170
assert.deepEqual(string.get.rgb('#c814e9ff'), [200, 20, 233, 1]);

0 commit comments

Comments
 (0)