This blog is outdated. Check out my website at http://cem.re or my work blog at http://cem.re/tumblr.

can't use it

a blog by Cemre Güngör

Automatically convert all CSS rgb colors to hex colors

If you copy pasted CSS from firebug’s inspector, the colors turn out as rgb(a,b,c). To fix your file and revert all colors to #aabbcc hex format, use this PHP script:


$string = <<
(put your CSS here)
EOD;
$pattern = '/rgb\((\d+),\s*(\d+),\s*(\d+)\)/e';
$replacement = '"#" . dechex(\\1) . dechex(\\2) . dechex(\\3)';
echo preg_replace($pattern, $replacement, $string);

There usually aren’t any ‘$’ characters in CSS but if you have them, escape them while putting the CSS in heredoc :)