javascript ligten a color

Solutions on MaxInterview for javascript ligten a color by the best coders in the world

showing results for - "javascript ligten a color"
Lilli
04 Apr 2018
1function LightenDarkenColor(col,amt) {
2    var usePound = false;
3    if ( col[0] == "#" ) {
4        col = col.slice(1);
5        usePound = true;
6    }
7
8    var num = parseInt(col,16);
9
10    var r = (num >> 16) + amt;
11
12    if ( r > 255 ) r = 255;
13    else if  (r < 0) r = 0;
14
15    var b = ((num >> 8) & 0x00FF) + amt;
16
17    if ( b > 255 ) b = 255;
18    else if  (b < 0) b = 0;
19
20    var g = (num & 0x0000FF) + amt;
21
22    if ( g > 255 ) g = 255;
23    else if  ( g < 0 ) g = 0;
24
25    return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);
26}
27//usage example:
28console.log(LightenDarkenColor("3F6D2A", -40)); //also supports passing #3F6D2A