From 6f06dd24afb52e4f7bb08d871804dfc67afa1d67 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Wed, 20 Mar 2013 08:30:18 +0100 Subject: [PATCH] Reworked RGB to hex conversions. --- lib/clean.js | 17 +++++++++-------- test/unit-test.js | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/clean.js b/lib/clean.js index 120f5c17..3288b4df 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -170,14 +170,15 @@ var CleanCSS = { }); // rgb to hex colors - replace(/rgb\s*\(([^\)]+)\)/g, function(match, color) { - var parts = color.split(','); - var encoded = '#'; - for (var i = 0; i < 3; i++) { - var asHex = parseInt(parts[i], 10).toString(16); - encoded += asHex.length == 1 ? '0' + asHex : asHex; - } - return encoded; + 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); }); // long hex to short hex colors diff --git a/test/unit-test.js b/test/unit-test.js index fc5fc3b7..51fe9c3a 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -419,7 +419,7 @@ vows.describe('clean-units').addBatch({ }), 'colors': cssContext({ 'shorten rgb to standard hexadecimal format': [ - 'a{ color:rgb (5, 10, 15) }', + 'a{ color:rgb(5, 10, 15) }', 'a{color:#050a0f}' ], 'skip rgba shortening': [ -- 2.34.1