开发者

How to convert 6 digit color code to 3 digit quickly in css file?

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开发者_StackOverflow社区_and suggested css drive compressor does the work alt text http://easycaptures.com/fs/uploaded/443/7187499702.png


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" :)


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');


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜