From a1bebe1af23a642ee23e61f4b602eaba32fd3765 Mon Sep 17 00:00:00 2001
From: christian-bromann <mail@christian-bromann.com>
Date: Wed, 18 Jul 2018 15:27:16 +0200
Subject: [PATCH] allow transparent values

---
 index.js             |  2 +-
 rgb2hex.js           |  2 +-
 rgb2hex.min.js       |  2 +-
 test/rgb2hex.test.js | 24 +++++++++++++++---------
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/index.js b/index.js
index 124b3c4..3d40eb1 100644
--- a/index.js
+++ b/index.js
@@ -28,7 +28,7 @@ var rgb2hex = module.exports = function rgb2hex(color) {
     /**
      * parse input
      */
-    var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
+    var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
 
     if(!digits) {
         // or throw error if input isn't a valid rgb(a) color
diff --git a/rgb2hex.js b/rgb2hex.js
index a6ccd48..2112549 100644
--- a/rgb2hex.js
+++ b/rgb2hex.js
@@ -21,7 +21,7 @@
         /**
          * parse input
          */
-        var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
+        var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
 
         if(!digits) {
             // or throw error if input isn't a valid rgb(a) color
diff --git a/rgb2hex.min.js b/rgb2hex.min.js
index 26f018c..18b7089 100644
--- a/rgb2hex.min.js
+++ b/rgb2hex.min.js
@@ -1 +1 @@
-!function(r){var e=function(r){if("string"!=typeof r)throw new Error("color has to be type of `string`");if("#"===r.substr(0,1))return{hex:r,alpha:1};var e=r.replace(/\s+/g,""),t=/(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(e);if(!t)throw new Error("given color ("+r+") isn't a valid rgb or rgba color");var n=parseInt(t[3],10),o=parseInt(t[4],10),a=parseInt(t[5],10),i=t[6]?/([0-9\.]+)/.exec(t[6])[0]:"1",s=(a|o<<8|n<<16|1<<24).toString(16).slice(1);return"."===i.substr(0,1)&&(i=parseFloat("0"+i)),1<i&&(i=1),i=parseFloat(Math.round(100*i))/100,{hex:"#"+s.toString(16),alpha:i}};"function"==typeof define&&define.amd?define("rgb2hex",function(){return e}):r.rgb2hex=e}(window);
\ No newline at end of file
+!function(r){var e=function(r){if("string"!=typeof r)throw new Error("color has to be type of `string`");if("#"===r.substr(0,1))return{hex:r,alpha:1};var e=r.replace(/\s+/g,""),t=/(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|0??\.([0-9]{0,3})))??\)/.exec(e);if(!t)throw new Error("given color ("+r+") isn't a valid rgb or rgba color");var n=parseInt(t[3],10),o=parseInt(t[4],10),a=parseInt(t[5],10),i=t[6]?/([0-9\.]+)/.exec(t[6])[0]:"1",s=(a|o<<8|n<<16|1<<24).toString(16).slice(1);return"."===i.substr(0,1)&&(i=parseFloat("0"+i)),1<i&&(i=1),i=parseFloat(Math.round(100*i))/100,{hex:"#"+s.toString(16),alpha:i}};"function"==typeof define&&define.amd?define("rgb2hex",function(){return e}):r.rgb2hex=e}(window);
\ No newline at end of file
diff --git a/test/rgb2hex.test.js b/test/rgb2hex.test.js
index 91caa92..7c67616 100644
--- a/test/rgb2hex.test.js
+++ b/test/rgb2hex.test.js
@@ -5,39 +5,39 @@ const invalidErrorMessage = (input) => 'given color (' + input + ') isn\'t a val
 describe('rgb2hex should', () => {
     describe('throw an error if input is not typeof string', () => {
         it('[Object] {color: \'something\'}', () => {
-            var input = {color: 'something'}
+            const input = {color: 'something'}
             expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
         })
 
         it('[Function] function(){}', () => {
-            var input = function(){}
+            const input = function(){}
             expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
         })
 
         it('[Number] 231', () => {
-            var input = 231
+            const input = 231
             expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
         })
     })
 
     describe('throw an error if input is invalid', () => {
         it('notacolor', () => {
-            var input = 'notacolor'
+            const input = 'notacolor'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
         })
 
         it('rgba(100, 100)', () => {
-            var input = 'rgb(100, 100)'
+            const input = 'rgb(100, 100)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
         })
 
         it('rgba(100, 10a0, 200, 300)', () => {
-            var input = 'rgba(100, 10a0, 200, 300)'
+            const input = 'rgba(100, 10a0, 200, 300)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
         })
 
         it('rgba(23, 54, 4, -.33)', () => {
-            var input = 'rgba(23, 54, 4, -.33)'
+            const input = 'rgba(23, 54, 4, -.33)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
         })
     })
@@ -80,12 +80,12 @@ describe('rgb2hex should', () => {
         })
 
         it('by limiting alpha value to 1', () => {
-            var input = 'rgba(236,68,44,1)'
+            const input = 'rgba(236,68,44,1)'
             expect(rgb2hex(input).alpha).not.toBeGreaterThan(1)
         })
 
         it('by not accepting to big values', () => {
-            var input = 'rgba(1123, 54, 4, 0.33)'
+            let input = 'rgba(1123, 54, 4, 0.33)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
             input = 'rgba(113, 1154, 4, 0.33)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
@@ -98,6 +98,12 @@ describe('rgb2hex should', () => {
             input = 'rgba(12,173,22,1.67)'
             expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
         })
+
+        it('transparent color', () => {
+            const input = 'rgba(0, 0, 0, 0)'
+            expect(rgb2hex(input).alpha).toBe(0)
+            expect(rgb2hex(input).hex).toBe('#000000')
+        })
     })
 
     describe('not care about', () => {