1

I'm trying to achieve what this guy here is doing, only in PHP or jQuery. Basically I have a hex color code, say #FF0000, which is red. How would I find darker or lighter hex color codes of this color.

To clearify: I want to take a hex color code (#FF0000), and find the correct hex color code of lighter or darker shades of that color code.

Either done in PHP, or jQuery, something that I can change the color via PHP, as the server processes the page.

I prefer not to use third party jQuery plugins to achieve this, but I will if its super duper complicated.

15

3 Answers 3

5

When you say "a lighter version" (or "a darker version") there are a very large number of possibilities. For instance, you could take #ff0000 and have 253 "darker versions" ranging from #010000 to #fe0000. Similarly, you can have 253 "lighter versions" ranging from #ff0101 to #fffefe. So your question is not very well defined.

I will assume in this answer that by "lighter version", you mean the result of overying a 50% transparent white on the colour, and by "darker" the same but black.

In any case, you should always start by extracting the numbers from the hex code:

// assuming input of form "#RRGGBB"
$col = Array(
    hexdec(substr($input,1,2)),
    hexdec(substr($input,3,2)),
    hexdec(substr($input,5,2))
);

Now that you have that, you can easily apply the "overlay":

$darker = Array(
    $col[0]/2,
    $col[1]/2,
    $col[2]/2
);
$lighter = Array(
    255-(255-$col[0])/2,
    255-(255-$col[1])/2,
    255-(255-$col[2])/2
);

Then it's a simple matter to convert them back into hex codes:

$darker = "#".sprintf("%02X%02X%02X", $darker[0], $darker[1], $darker[2]);
$lighter = "#".sprintf("%02X%02X%02X", $lighter[0], $lighter[1], $lighter[2]);

Done!

4
  • Perfect. Thanks so much, might I ask how much lighter or darker this makes the color? How would I control it so it only makes it slightly lighter or slightly darker? Jun 18, 2012 at 22:07
  • All those /2 bits could be better written as *0.5 - that's the 50% I mentioned above. You can adjust that amount to get more or less light/dark. Jun 18, 2012 at 22:14
  • Thanks :) I appreciate it. Shoulda figured it was the /2. Jun 18, 2012 at 22:16
  • github.com/mbostock/d3/blob/master/src/core/rgb.js d3 library has an implementation of lighter and darker. Jun 18, 2012 at 22:16
5

I recommend using the TinyColor color manipulation microframework for this.

tinycolor.darken('#FF0000').toHexString()

1
  • tinycolor("#f00").lighten().toString(); // "#ff3333" - lighten tinycolor("#f00").darken().toString(); // "#cc0000" - darken
    – RicardO
    Mar 24, 2015 at 7:33
2

All you have to do is split the hex code into it's R G and B values, and then run this part of the actionscript on them:

factor = percent/100;
r+=(255-r)*factor;
b+=(255-b)*factor;
g+=(255-g)*factor;

So the full functions would be something like this in pure javascript:

function lighten(color,percent){
    factor = percent/100;
    r = parseInt(color.substring(1,2),16);
    b = parseInt(color.substring(3,4),16);
    g = parseInt(color.substring(5,6),16);
    r+=(255-r)*factor;
    r=r.toString(16);
    if(r.length==1)
        r = '0'+r;
    b+=(255-b)*factor;
    b=b.toString(16);
    if(b.length==1)
        b = '0'+b;
    g+=(255-g)*factor;
    g=g.toString(16);
    if(g.length==1)
        g = '0'+g;
    return "#"+r+g+b;
}

and

function darken(color,percent){
    factor = percent/100;
    r = parseInt(color.substring(1,2),16);
    b = parseInt(color.substring(3,4),16);
    g = parseInt(color.substring(5,6),16);
    r-=(255-r)*factor;
    r=r.toString(16);
    if(r.length==1)
        r = '0'+r;
    b-=(255-b)*factor;
    b=b.toString(16);
    if(b.length==1)
        b = '0'+b;
    g-=(255-g)*factor;
    g=g.toString(16);
    if(g.length==1)
        g = '0'+g;
    return "#"+r+g+b;
}

You can intermix pure javascript and jQuery, so this should work just fine.

3
  • Um... You missed the conversion from hex-dec and dec-hex in your code ;) Jun 18, 2012 at 22:07
  • @Kolink Yeah, I realized that right after I posted it. It's fixed now.
    – gcochard
    Jun 18, 2012 at 22:07
  • Hate to disappoint, Greg, but what if, for instance, r=4, g=20 and b=9? The result is #4149 Jun 18, 2012 at 22:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.