Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - "One shade darker"

Tags:

javascript

hex

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 difficult it would be great if the submenu could be one shade darker. Does anyone know how to achieve this effect?

like image 376
JasonS Avatar asked Jul 24 '26 15:07

JasonS


1 Answers

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

like image 138
bezmax Avatar answered Jul 26 '26 08:07

bezmax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!