Fixes out of bounds values in rgb / rgba declarations. See #216.
authorGoalSmashers <jakub@goalsmashers.com>
Thu, 30 Jan 2014 08:05:26 +0000 (08:05 +0000)
committerGoalSmashers <jakub@goalsmashers.com>
Thu, 30 Jan 2014 08:05:26 +0000 (08:05 +0000)
lib/colors/rgb-to-hex.js
test/unit-test.js

index 479e620..acfbf01 100644 (file)
@@ -1,7 +1,11 @@
 module.exports = function RGBToHex(data) {
   return {
     process: function() {
-      return data.replace(/rgb\((\d+),(\d+),(\d+)\)/g, function(match, red, green, blue) {
+      return data.replace(/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/g, function(match, red, green, blue) {
+        red = Math.max(0, Math.min(~~red, 255));
+        green = Math.max(0, Math.min(~~green, 255));
+        blue = Math.max(0, Math.min(~~blue, 255));
+
         // Credit: Asen  http://jsbin.com/UPUmaGOc/2/edit?js,console
         return '#' + ('00000' + (red << 16 | green << 8 | blue).toString(16)).slice(-6);
       });
index 2b663c6..1a56bf8 100644 (file)
@@ -686,6 +686,14 @@ vows.describe('clean-units').addBatch({
     'hsl out of bounds #7': [
       'a{color:hsl(0,0%,-10%)}',
       'a{color:#000}'
+    ],
+    'rgb out of a lower bound': [
+      'a{color:rgb(-1,-1,-1)}',
+      'a{color:#000}'
+    ],
+    'rgb out of an upper bound': [
+      'a{color:rgb(256,256,256)}',
+      'a{color:#fff}'
     ]
   }),
   'shortening colors': colorShorteningContext(),