开发者

Javascript - "One shade darker"

开发者 https://www.devze.com 2023-01-10 08:11 出处:网络
Is it possible to use javascript to determine what color is one shade darker than the current background? Maybe some hexadecimal addition/subtraction?

Is it possible to use javascript to determine what color is one shade darker than the current background? Maybe some hexadecimal addition/subtraction?

I have a menu that can be any color and if it wasn't too d开发者_StackOverflowifficult it would be great if the submenu could be one shade darker. Does anyone know how to achieve this effect?


Something like this:

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;
}

Usage:

var newColor = shadeColor("#AA2222", -10);
alert(newColor); //Results in #a32020

Here is an example code to test it: http://pastebin.com/g6phySEv


as AB comments, 'shade' isn't very well defined. nonetheless, it might be easier to think of this in some other colour representation, such as 'V' in hsv.

you could either convert, decrease v and convert back, or figure out what decreasing v maps to in rgb hex

0

精彩评论

暂无评论...
验证码 换一张
取 消