Javascript - "一个阴影更暗"

Jas*_*onS 3 javascript hex

是否可以使用javascript来确定哪个颜色比当前背景更暗?也许一些十六进制加法/减法?

我有一个菜单,可以是任何颜色,如果它不是太难,如果子菜单可能是一个更暗的颜色将是伟大的.有谁知道如何实现这种效果?

bez*_*max 9

像这样的东西:

function shadeColor(color, shade) {
    var colorInt = parseInt(color.substring(1),16);

    var R = (colorInt & 0xFF0000) >> 16;
    var G = (colorInt & 0x00FF00) >> 8;
    var B = (colorInt & 0x0000FF) >> 0;

    R = R + Math.floor((shade/255)*R);
    G = G + Math.floor((shade/255)*G);
    B = B + Math.floor((shade/255)*B);

    var newColorInt = (R<<16) + (G<<8) + (B);
    var newColorStr = "#"+newColorInt.toString(16);

    return newColorStr;
}
Run Code Online (Sandbox Code Playgroud)

用法:

var newColor = shadeColor("#AA2222", -10);
alert(newColor); //Results in #a32020
Run Code Online (Sandbox Code Playgroud)

以下是测试它的示例代码:http://pastebin.com/g6phySEv