Skip to content
This repository
Browse code

Reworked RGB to hex conversions.

  • Loading branch information...
commit 6f06dd24afb52e4f7bb08d871804dfc67afa1d67 1 parent 1b9aca3
Goal Smashers Dev Team authored

Showing 2 changed files with 10 additions and 9 deletions. Show diff stats Hide diff stats

  1. +9 8 lib/clean.js
  2. +1 1  test/unit-test.js
17 lib/clean.js
@@ -170,14 +170,15 @@ var CleanCSS = {
170 170 });
171 171
172 172 // rgb to hex colors
173   - replace(/rgb\s*\(([^\)]+)\)/g, function(match, color) {
174   - var parts = color.split(',');
175   - var encoded = '#';
176   - for (var i = 0; i < 3; i++) {
177   - var asHex = parseInt(parts[i], 10).toString(16);
178   - encoded += asHex.length == 1 ? '0' + asHex : asHex;
179   - }
180   - return encoded;
  173 + replace(/rgb\((\d+),(\d+),(\d+)\)/g, function(match, red, green, blue) {
  174 + var redAsHex = parseInt(red, 10).toString(16);
  175 + var greenAsHex = parseInt(green, 10).toString(16);
  176 + var blueAsHex = parseInt(blue, 10).toString(16);
  177 +
  178 + return '#' +
  179 + ((redAsHex.length == 1 ? '0' : '') + redAsHex) +
  180 + ((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
  181 + ((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
181 182 });
182 183
183 184 // long hex to short hex colors
2  test/unit-test.js
@@ -419,7 +419,7 @@ vows.describe('clean-units').addBatch({
419 419 }),
420 420 'colors': cssContext({
421 421 'shorten rgb to standard hexadecimal format': [
422   - 'a{ color:rgb (5, 10, 15) }',
  422 + 'a{ color:rgb(5, 10, 15) }',
423 423 'a{color:#050a0f}'
424 424 ],
425 425 'skip rgba shortening': [

0 comments on commit 6f06dd2

Please sign in to comment.
Something went wrong with that request. Please try again.