Welcome, guest | Sign In | My Account | Store | Cart

Want to replace all six-digit RGB notation (#rrggbb) to the short (three-digit #rgb) form? Or the opposite?

Bash, 8 lines
1
2
3
4
5
6
7
8
# shorten your CSS
sed -re 's/#(([0-9a-fA-F])\2)(([0-9a-fA-F])\4)(([0-9a-fA-F])\6)/#\2\4\6/'

# expand: the three-digit RGB notation (#rgb) is converted into six-digit form (#rrggbb) by replicating digits
sed -re 's/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])\b/#\1\1\2\2\3\3/'

# works in egrep too
grep -E '(([0-9a-fA-F])\2)(([0-9a-fA-F])\4)(([0-9a-fA-F])\6)'

Tested with GNU sed 4.2:

#000000 -> #000
#336699 -> #369
#123456 -> #123456
#01aa01 -> #01aa01
#a1a1a1 -> #a1a1a1
#0aa11a -> #0aa11a