views:

456

answers:

7

What's the easiest way to programmatically darken a hex colour? Without using any built-in functions. The language is irrelevant, it can just be pseudo-code if you want.

Thanks.

+2  A: 
  • Split the hex color into its RGB components.
  • Convert each of these components into an integer value.
  • Multiply that integer by a fraction, such as 0.5, making sure the result is also integer.
    • Alternatively, subtract a set amount from that integer, being sure not to go below 0.
  • Convert the result back to hex.
  • Concatenate these values in RGB order, and use.
Adam Bellaire
+4  A: 
given arg darken_factor # a number from 0 to 1, 0=no change, 1=black
for each byte in rgb_value
    byte = byte * (1 - darken_factor)
John Zwinck
+3  A: 

A function implemented in javascript:

// credits: richard maloney 2006
function getTintedColor(color, v) {
    if (color.length >6) { color= color.substring(1,color.length)}
    var rgb = parseInt(color, 16); 
    var r = Math.abs(((rgb >> 16) & 0xFF)+v); if (r>255) r=r-(r-255);
    var g = Math.abs(((rgb >> 8) & 0xFF)+v); if (g>255) g=g-(g-255);
    var b = Math.abs((rgb & 0xFF)+v); if (b>255) b=b-(b-255);
    r = Number(r < 0 || isNaN(r)) ? 0 : ((r > 255) ? 255 : r).toString(16); 
    if (r.length == 1) r = '0' + r;
    g = Number(g < 0 || isNaN(g)) ? 0 : ((g > 255) ? 255 : g).toString(16); 
    if (g.length == 1) g = '0' + g;
    b = Number(b < 0 || isNaN(b)) ? 0 : ((b > 255) ? 255 : b).toString(16); 
    if (b.length == 1) b = '0' + b;
    return "#" + r + g + b;
}

Example:

> getTintedColor("ABCEDEF", 10)
> #c6f7f9
The MYYN
A: 

A hex colour such as #FCFCFC consists of three pairs representing RGB. The second part of each pair can be reduced to darken any colour without altering the colour considerably.

eg. to darken #FCFCFC, lower the values of C to give #F0F0F0

Reducing the first part of each pair by a small amount will also darken the colour, but you will start to affect the colour more (eg. turning a green to a blue).

adrianbanks
+2  A: 

If you're not bothered about too much control, and just want a generally darker version of a colour, then:

col = (col & 0xfefefe) >> 1;

Is a nice quick way to halve a colour value (assuming it's packed as a byte per channel, obviously).

James Sutherland
Wow this actually works perfectly for what I need it for! Thanks, great tip!
Mk12
Is there a way to make it darken by a quarter instead of half?
Mk12
Or what about making it brighter instead of darker?
Mk12
cobbal
+4  A: 

Well, I don't have any pseudocode for you, but a tip. If you want to darken a color and maintain its hue, you should convert that hex to HSB (hue, saturation, brightness) rather than RGB. This way, you can adjust the brightness and it will still look like the same color without hue shifting. You can then convert that HSB back to hex.

Nate B
works, but overly complex for a simple task
cobbal
A: 

Convert hex color into integer RBG components:

#FF6600 = rbg(255, 102, 0)

If you want to make it darker by 5%, then simply reduce all integer values by 5%:

255 - 5% = 242
102 - 5% = 96
0 - 5% = 0

= rbg(242, 96, 0)

Convert back to hex color

= #F26000
lubos hasko