Skip to content

Commit 8407fb2

Browse files
committed
Merge pull request #16 from ben-eb/fix-transparency
Fix HSL alpha channel bug.
2 parents 5bcb961 + b6a1979 commit 8407fb2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

color-string.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ function getHsla(string) {
8787
var hsl = /^hsla?\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
8888
var match = string.match(hsl);
8989
if (match) {
90+
var alpha = parseFloat(match[4]);
9091
var h = scale(parseInt(match[1]), 0, 360),
9192
s = scale(parseFloat(match[2]), 0, 100),
9293
l = scale(parseFloat(match[3]), 0, 100),
93-
a = scale(parseFloat(match[4]) || 1, 0, 1);
94+
a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
9495
return [h, s, l, a];
9596
}
9697
}
@@ -102,10 +103,11 @@ function getHwb(string) {
102103
var hwb = /^hwb\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
103104
var match = string.match(hwb);
104105
if (match) {
106+
var alpha = parseFloat(match[4]);
105107
var h = scale(parseInt(match[1]), 0, 360),
106108
w = scale(parseFloat(match[2]), 0, 100),
107109
b = scale(parseFloat(match[3]), 0, 100),
108-
a = scale(parseFloat(match[4]) || 1, 0, 1);
110+
a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
109111
return [h, w, b, a];
110112
}
111113
}

test/basic.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ assert.deepEqual(string.getHwb("hwb(200, 20%, 33%, 0.2)"), [200, 20, 33, 0.2]);
4343
assert.deepEqual(string.getRgb("#fef"), [255, 238, 255]);
4444
assert.deepEqual(string.getRgb("rgba(200, 20, 233, 0.2)"), [200, 20, 233]);
4545
assert.deepEqual(string.getHsl("hsl(240, 100%, 50.5%)"), [240, 100, 50.5]);
46+
assert.deepEqual(string.getRgba('rgba(0,0,0,0)'), [0, 0, 0, 0]);
47+
assert.deepEqual(string.getHsla('hsla(0,0%,0%,0)'), [0, 0, 0, 0]);
48+
assert.deepEqual(string.getHwb("hwb(400, 10%, 200%, 0)"), [360, 10, 100, 0]);
4649

4750
// range
4851
assert.deepEqual(string.getRgba("rgba(300, 600, 100, 3)"), [255, 255, 100, 1]);

0 commit comments

Comments
 (0)