How to convert all color code #XXYYZZ to shorter 3 character version #XYZ of whole css file?
views:
256answers:
3If you are just interested in minimizing the download size for your CSS file, you might use one of the many CSS compressors available (such as this one). And be sure to do the same with your javascript files while you are at it.
You can only shorten CSS colour codes to a 3 character version if they take the form
#XXYYZZ
Then they can be abbreviated
#XYZ
There are only 216 different codes which meet this requirement.
You can convert to the shorter 3-character version only colors expressed this way: #RRGGBB where the first and the second characters are the same, the third and fourth characters are the same and the fifth and sixth characters are the same.
So: #CC00DD
can be shortened to #C0D
while #CC01DD
cannot.
A quick way to shorten all the possible colors in a CSS file is to open the file with an editor supporting regular expressions (for example kwrite or kate on linux) and replace (ignoring case) the following regular expression:
#([0-9A-F])\1([0-9A-F])\2([0-9A-F])\3
with this substitution text:
#\1\2\3
Tested with kate.
Otherwise you can use this tool where, if you only need to compress color codes, you can uncheck all the options except the "Compress color codes where possible".