kat*_*tie 0 javascript hex colors
如果可能的话,我想将给定的十六进制颜色转换为 3 位数字。例如:
#ffffff - #fff
#001122 - #012
#012345 - #012345
Run Code Online (Sandbox Code Playgroud)
有谁知道该怎么做?
我在谷歌网站上找到了这个正则表达式,但我不知道如何使用它们:/
# 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)'
Run Code Online (Sandbox Code Playgroud)
可能有一种更短、更甜蜜的方法,但是您可以在十六进制颜色的字符之间执行简单的字符比较,并在必要时手动组装字符串,例如:
var hex = "#aabb00";
if ((hex.charAt(1) == hex.charAt(2))
&& (hex.charAt(3) == hex.charAt(4))
&& (hex.charAt(5) == hex.charAt(6))) {
hex = "#" + hex.charAt(1) + hex.charAt(3) + hex.charAt(5);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3106 次 |
| 最近记录: |