tags:

views:

178

answers:

4

I have a large css file and i want to convert all 6 digit hex code into shorthand 3 digit. Is there any online or offline tool to do so.

my purpose is to reduce css file size without making something wrong.

any other tips would be be appreciated to reduce css file size without affecting css output.

Edit:

As me_and suggested css drive compressor does the work alt text

+1  A: 

If have access to a server side scripting language or your editor handles regular expressions, here's a regex replace to do the job:

PHP

preg_replace('/#([\dA-Fa-f])[\dA-Fa-f]([\dA-Fa-f])[\dA-Fa-f]([\dA-Fa-f])[\dA-Fa-f]/m', '#$1$2$3', $str);

JavaScript

var str = '#fd02eb';
str = str.replace(/#([\dA-Fa-f])[\dA-Fa-f]([\dA-Fa-f])[\dA-Fa-f]([\dA-Fa-f])[\dA-Fa-f]/g, '#$1$2$3');
Tatu Ulmanen
+1  A: 

By my understanding, you'll most likely lose colour information by converting it from 6-digit to 3-digit, so you'll need to specify what conversion you want. Examples would be good.

Nonetheless, CSS Drive will do code compression for you. Or just try Google for "CSS compression" :)

me_and
Indeed, compressing bring the available color space from 16M colors to just 4096.
Agos
Unless the second digit is equal to the first in every case :) This would be the only case in which the actual color displayed would not change.
Carl Smotricz
Thanks it works http://www.cssdrive.com/compressor/help/colors.htm
metal-gear-solid
I use Clean CSS (http://www.cleancss.com/) it has more options and validates your CSS as well.
fudgey
+1  A: 

If I really wanted to do this, I'd fire up vim and edit the file in that.

A command which will do roughly this would be:

:%s/\v#(\x)\x(\x)\x(\x)\x;/#\1\2\3;/g

Which

  • puts vim into line command mode;
  • starts a substitution;
  • puts vim into "very magic" mode with regard to special characters;
  • finds instances of 6 hex digits enclosed by # and ;
  • removes every second digit;
  • throughout every line.
Carl Smotricz