Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Color hex code does not output using Javascript function

I am using the following LightenDarkenColor function to collect lighten and darken colors.

After calling it few times as follows:

var LightenDarkenColor = function(col, amt) {
  var usePound = true;
  if (col[0] == "#") {
    col = col.slice(1);
    usePound = true;
  }
  var num = parseInt(col, 16);
  var r = (num >> 16) + amt;
  if (r > 255) r = 255;
  else if (r < 0) r = 0;
  var b = ((num >> 8) & 0x00FF) + amt;
  if (b > 255) b = 255;
  else if (b < 0) b = 0;
  var g = (num & 0x0000FF) + amt;
  if (g > 255) g = 255;
  else if (g < 0) g = 0;
  var output = (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
  console.log(col + " x " + amt + " = " + output);
  return output;
}

LightenDarkenColor('#1d4ed8', 80);
LightenDarkenColor('#1d4ed8', 50);
LightenDarkenColor('#1d4ed8', 20);
LightenDarkenColor('#1d4ed8', 10);
LightenDarkenColor('#1d4ed8', 0);
LightenDarkenColor('#1d4ed8', -10);
LightenDarkenColor('#1d4ed8', -20);
LightenDarkenColor('#1d4ed8', -50);

The output are:

1d4ed8 x 80 = #6d9eff
1d4ed8 x 50 = #4f80ff
1d4ed8 x 20 = #3162ec
1d4ed8 x 10 = #2758e2
1d4ed8 x 0 = #1d4ed8
1d4ed8 x -10 = #1344ce
1d4ed8 x -20 = #93ac4
1d4ed8 x -50 = #1ca6

What I am missing in the LightenDarkenColor() function? Last two color code does not a valid color code ...

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!