From eb857c2e5f421373a00057d08afb34f45b1095e1 Mon Sep 17 00:00:00 2001 From: Ben Briggs Date: Thu, 9 Mar 2017 19:13:23 +0000 Subject: [PATCH] Fix shorthand alpha channel parsing. (#34) --- index.js | 6 +++--- test/basic.js | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 74e2d43..4f0b37a 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ cs.get.rgb = function (string) { return null; } - var abbr = /^#([a-f0-9]{3,4})$/i; + var abbr = /^#([a-f0-9]{3})([a-f0-9]{1})?$/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*)?\)$/; @@ -58,15 +58,15 @@ cs.get.rgb = function (string) { var hexAlpha; if (match = string.match(abbr)) { + hexAlpha = match[2]; match = match[1]; - hexAlpha = match[3]; for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i] + match[i], 16); } if (hexAlpha) { - rgb[3] = Math.round(parseInt(hexAlpha + hexAlpha, 16) / 255); + rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100; } } else if (match = string.match(hex)) { hexAlpha = match[2]; diff --git a/test/basic.js b/test/basic.js index 94a6b0e..086d22c 100644 --- a/test/basic.js +++ b/test/basic.js @@ -55,6 +55,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(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]);