<分区>
假设我的颜色是浅红色,我需要更多的红色形式,你能帮我看看这是怎么可能的吗?我有像 #00ff00 这样的十六进制颜色
谢谢
标签 javascript jquery css
<分区>
假设我的颜色是浅红色,我需要更多的红色形式,你能帮我看看这是怎么可能的吗?我有像 #00ff00 这样的十六进制颜色
谢谢
最佳答案
在这种情况下不需要 jQuery!
function ColorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00"+c).substr(c.length);
}
return rgb;
}
hex
为十六进制颜色值,lum
为亮度因子,-1 <= lum <= 1,负数表示较暗,正数表示较亮
用法:
var newColor = ColorLuminance("#00ff00", -0.5); // "#334d66" - 50% darker
来自 here 的代码片段
关于javascript - 我们如何使颜色比jquery中的给定颜色更暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16787880/