Skip to content

Instantly share code, notes, and snippets.

@vimiix
Created January 15, 2019 13:01
Show Gist options
  • Save vimiix/0d9fefda83e80c2bb2fc47198b40c741 to your computer and use it in GitHub Desktop.
Save vimiix/0d9fefda83e80c2bb2fc47198b40c741 to your computer and use it in GitHub Desktop.
解析字符串的颜色码,判断颜色是暖色还是冷色
function hexToRgb (hex) {
var rgb = [];
hex = hex.substr(1); //去除前缀 # 号
if (hex.length === 3) { // 处理 "#abc" 成 "#aabbcc"
hex = hex.replace(/(.)/g, '$1$1');
}
hex.replace(/../g, function(color){
rgb.push(parseInt(color, 0x10)); //按16进制将字符串转换为数字
});
return rgb
};
function isLight(rgb) {
return (
0.213 * rgb[0] +
0.715 * rgb[1] +
0.072 * rgb[2] >
255 / 2
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment