From a2ea7392f917e3d03854f63cdf88daceb552287f Mon Sep 17 00:00:00 2001 From: Christos Lytras Date: Fri, 26 Nov 2021 00:08:33 +0200 Subject: [PATCH] add alpha percentage for space-separated RGB --- README.md | 1 + index.js | 16 ++++++++++++---- test/basic.js | 4 +++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b9e5ad0..e58670c 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ colorString.get.rgb('#FFF') // [255, 255, 255, 1] colorString.get.rgb('blue') // [0, 0, 255, 1] colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3] colorString.get.rgb('rgba(200 60 60 / 0.3)') // [200, 60, 60, 0.3] +colorString.get.rgb('rgba(200 60 60 / 30%)') // [200, 60, 60, 0.3] colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1] colorString.get.rgb('rgb(200 200 200)') // [200, 200, 200, 1] diff --git a/index.js b/index.js index e20dde5..9ccd955 100644 --- a/index.js +++ b/index.js @@ -49,8 +49,8 @@ cs.get.rgb = function (string) { var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; - var rgba = /^rgba?\(\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)\s*)?\)$/; - var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)\s*)?\)$/; + var rgba = /^rgba?\(\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; + var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var keyword = /(\D+)/; var rgb = [0, 0, 0, 1]; @@ -88,7 +88,11 @@ cs.get.rgb = function (string) { } if (match[4]) { - rgb[3] = parseFloat(match[4]); + if (match[5]) { + rgb[3] = parseInt(match[4], 0) * 0.01; + } else { + rgb[3] = parseFloat(match[4]); + } } } else if (match = string.match(per)) { for (i = 0; i < 3; i++) { @@ -96,7 +100,11 @@ cs.get.rgb = function (string) { } if (match[4]) { - rgb[3] = parseFloat(match[4]); + if (match[5]) { + rgb[3] = parseInt(match[4], 0) * 0.01; + } else { + rgb[3] = parseFloat(match[4]); + } } } else if (match = string.match(keyword)) { if (match[1] === 'transparent') { diff --git a/test/basic.js b/test/basic.js index d33ec63..443da89 100644 --- a/test/basic.js +++ b/test/basic.js @@ -46,7 +46,6 @@ assert.deepEqual(string.get('hwb(240deg, 100%, 50.5%)'), {model: 'hwb', value: [ assert.deepEqual(string.get('hsla(250, 100%, 50%, 50%)'), null); assert.deepEqual(string.get('hsl(250 100% 50% / 50%)'), null); assert.deepEqual(string.get('rgba(250, 100%, 50%, 50%)'), null); -assert.deepEqual(string.get('rgba(250 100% 50% / 50%)'), null); assert.deepEqual(string.get('333333'), null); assert.strictEqual(string.get('#1'), null); assert.strictEqual(string.get('#f'), null); @@ -87,10 +86,13 @@ assert.deepEqual(string.get.rgb('#c814e900'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('#c814e9ff'), [200, 20, 233, 1]); assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0.2)'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0.2)'), [200, 20, 233, 0.2]); +assert.deepEqual(string.get.rgb('rgba(200 20 233 / 20%)'), [200, 20, 233, 0.2]); assert.deepEqual(string.get.rgb('rgba(200, 20, 233, 0)'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0)'), [200, 20, 233, 0]); +assert.deepEqual(string.get.rgb('rgba(200 20 233 / 0%)'), [200, 20, 233, 0]); assert.deepEqual(string.get.rgb('rgba(100%, 30%, 90%, 0.2)'), [255, 77, 229, 0.2]); assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 0.2)'), [255, 77, 229, 0.2]); +assert.deepEqual(string.get.rgb('rgba(100% 30% 90% / 20%)'), [255, 77, 229, 0.2]); assert.deepEqual(string.get.hsl('hsla(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]); assert.deepEqual(string.get.hsl('hsl(200 20% 33% / 0.2)'), [200, 20, 33, 0.2]); assert.deepEqual(string.get.hwb('hwb(200, 20%, 33%, 0.2)'), [200, 20, 33, 0.2]);