Javascript - "One Shade Darker"

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

I have a menu that can be of any color, and if it werenโ€™t too complicated, it would be great if the submenu could be one shade darker. Does anyone know how to achieve this effect?

+3
source share
2 answers

as AB comments, the โ€œshadeโ€ is not very clearly defined. however, it would be easier to think about it in some other color representation, such as the โ€œVโ€ in hsv .

, v , , v- rgb hex

+2

- :

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

:

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

: http://pastebin.com/g6phySEv

+9

Source: https://habr.com/ru/post/1757964/


All Articles