Fixes #213 - faster rgb to hex transforms.
authortomByrer <tomByrer@gmail.com>
Thu, 30 Jan 2014 07:44:13 +0000 (07:44 +0000)
committerGoalSmashers <jakub@goalsmashers.com>
Thu, 30 Jan 2014 07:44:13 +0000 (07:44 +0000)
History.md
lib/colors/rgb-to-hex.js

index 229fab0..20c49f4 100644 (file)
@@ -12,6 +12,7 @@
 * Fixed issue [#165](https://github.com/GoalSmashers/clean-css/issues/165) - extra space after trailing parenthesis.
 * Fixed issue [#186](https://github.com/GoalSmashers/clean-css/issues/186) - strip unit from 0rem.
 * Fixed issue [#207](https://github.com/GoalSmashers/clean-css/issues/207) - bug in parsing protocol `@import`s.
+* Fixed issue [#213](https://github.com/GoalSmashers/clean-css/issues/213) - faster rgb to hex transforms.
 
 [2.0.7 / 2014-01-16](https://github.com/GoalSmashers/clean-css/compare/v2.0.6...v2.0.7)
 ==================
index d235289..479e620 100644 (file)
@@ -2,14 +2,8 @@ module.exports = function RGBToHex(data) {
   return {
     process: function() {
       return data.replace(/rgb\((\d+),(\d+),(\d+)\)/g, function(match, red, green, blue) {
-        var redAsHex = parseInt(red, 10).toString(16);
-        var greenAsHex = parseInt(green, 10).toString(16);
-        var blueAsHex = parseInt(blue, 10).toString(16);
-
-        return '#' +
-          ((redAsHex.length == 1 ? '0' : '') + redAsHex) +
-          ((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
-          ((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
+        // Credit: Asen  http://jsbin.com/UPUmaGOc/2/edit?js,console
+        return '#' + ('00000' + (red << 16 | green << 8 | blue).toString(16)).slice(-6);
       });
     }
   };