我们怎么能在jquery中制作比给定颜色更暗的颜色

时间:2013-05-28 08:57:02

标签: javascript jquery css

假设我的颜色浅红色,我需要更多的红色形状,请你帮助我如何实现。我有#00ff00

之类的hexadeciaml颜色

谢谢

1 个答案:

答案 0 :(得分:11)

在这种情况下不需要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&lt; = lum&lt; = 1,负数表示较暗,正面较浅

用法:

var newColor = ColorLuminance("#00ff00", -0.5); // "#334d66" - 50% darker

查看working JSFiddle

来自here

的代码段