Skip to content

Commit ccbea79

Browse files
clytrasQix-
authored andcommitted
Add alpha percentage for space-separated RGB
1 parent 19a5456 commit ccbea79

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ colorString.get.rgb('#FFF') // [255, 255, 255, 1]
2828
colorString.get.rgb('blue') // [0, 0, 255, 1]
2929
colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3]
3030
colorString.get.rgb('rgba(200 60 60 / 0.3)') // [200, 60, 60, 0.3]
31+
colorString.get.rgb('rgba(200 60 60 / 30%)') // [200, 60, 60, 0.3]
3132
colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1]
3233
colorString.get.rgb('rgb(200 200 200)') // [200, 200, 200, 1]
3334

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ cs.get.rgb = function (string) {
4949

5050
var abbr = /^#([a-f0-9]{3,4})$/i;
5151
var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i;
52-
var rgba = /^rgba?\(\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)\s*)?\)$/;
53-
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)\s*)?\)$/;
52+
var rgba = /^rgba?\(\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
53+
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
5454
var keyword = /(\D+)/;
5555

5656
var rgb = [0, 0, 0, 1];
@@ -88,15 +88,23 @@ cs.get.rgb = function (string) {
8888
}
8989

9090
if (match[4]) {
91-
rgb[3] = parseFloat(match[4]);
91+
if (match[5]) {
92+
rgb[3] = parseInt(match[4], 0) * 0.01;
93+
} else {
94+
rgb[3] = parseFloat(match[4]);
95+
}
9296
}
9397
} else if (match = string.match(per)) {
9498
for (i = 0; i < 3; i++) {
9599
rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55);
96100
}
97101

98102
if (match[4]) {
99-
rgb[3] = parseFloat(match[4]);
103+
if (match[5]) {
104+
rgb[3] = parseInt(match[4], 0) * 0.01;
105+
} else {
106+
rgb[3] = parseFloat(match[4]);
107+
}
100108
}
101109
} else if (match = string.match(keyword)) {
102110
if (match[1] === 'transparent') {

test/basic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ assert.deepEqual(string.get('hwb(240deg, 100%, 50.5%)'), {model: 'hwb', value: [
4646
assert.deepEqual(string.get('hsla(250, 100%, 50%, 50%)'), null);
4747
assert.deepEqual(string.get('hsl(250 100% 50% / 50%)'), null);
4848
assert.deepEqual(string.get('rgba(250, 100%, 50%, 50%)'), null);
49-
assert.deepEqual(string.get('rgba(250 100% 50% / 50%)'), null);
5049
assert.deepEqual(string.get('333333'), null);
5150
assert.strictEqual(string.get('#1'), null);
5251
assert.strictEqual(string.get('#f'), null);
@@ -87,10 +86,13 @@ assert.deepEqual(string.get.rgb('#c814e900'), [200, 20, 233, 0]);
8786
assert.deepEqual(string.get.rgb('#c814e9ff'), [200, 20, 233, 1]);
8887
assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0.2)'), [200, 20, 233, 0.2]);
8988
assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0.2)'), [200, 20, 233, 0.2]);
89+
assert.deepEqual(string.get.rgb('rgba(200 20 233 / 20%)'), [200, 20, 233, 0.2]);
9090
assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0)'), [200, 20, 233, 0]);
9191
assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0)'), [200, 20, 233, 0]);
92+
assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0%)'), [200, 20, 233, 0]);
9293
assert.deepEqual(string.get.rgb('rgba(100%, 30%, 90%, 0.2)'), [255, 77, 229, 0.2]);
9394
assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 0.2)'), [255, 77, 229, 0.2]);
95+
assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 20%)'), [255, 77, 229, 0.2]);
9496
assert.deepEqual(string.get.hsl('hsla(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]);
9597
assert.deepEqual(string.get.hsl('hsl(200 20% 33% / 0.2)'), [200, 20, 33, 0.2]);
9698
assert.deepEqual(string.get.hwb('hwb(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]);

0 commit comments

Comments
 (0)