tags:

views:

439

answers:

3

Is there a formula I can use to make a hex colour value brighter?

+13  A: 

You could convert to HSV (using a formula from wikipedia here) Then increase the Value, then convert back, with the formula from lower on the same page.

Mike Cooper
A: 

Well seeing as colours are just 3 bytes of r/g/b what you can do is convert each byte to an int (0-255) and increase each byte by the same arbitrary number. Finish by converting each byte back to hex. Example:

Starting with colour: #224466

Converted to ints thats r = 34, g = 68 and b = 102

Increment by an arbitrary number (say 60) you get r = 94, g = 128 and b = 162

Convert back to hex gives you: #5E80A2

Note that the top limit is 255 (white) and bottom is 0 (black)

EDIT: As Mark points out theres a flaw with this. It can be fixed by an additional rule: If the initial value is at 0 then dont change it ;)

Darko Z
That would change the color, imo.
ChrisW
To expand on ChrisW's comment, imagine a color #800000, a dark but intense red. Brightening it in this way would result in #ff7f7f, closer to pink than red.
Mark Ransom
There's 16 million colors in that gamut. Changing *any* value changes the "color". I think that out may mean that it changes the r/g/b ratios, which is just an arbitrary axis for a single dimension of "color".
RBarryYoung
This is more of a quick, approximate brightening used to avoid dicking around with HSL - i probably should have stated this. Also thanks for pointing that flaw in logic out Mark, have amended with new rule.
Darko Z
Mark Ransom, you just made the famous request for a light red color, but not pink...
Marius
The new rule doesn't help. Imagine starting at #800101 instead of #800000.
Mark Ransom
+6  A: 

The standard answer is to convert to a different color space that has brightness as one axis, then manipulate that value directly. I don't like that answer, because it doesn't tell you what you'll get when you exceed the color space gamut.

If by brighter you mean more intense, you can just multiply each R,G,B value by the same value, where that value is > 1. For example, to make it 20% brighter, multiply each value by 1.2.

If any of the resulting values are greater than 255, you've exceeded the limits of the RGB gamut. Probably the best thing to do in that case is to bring the color closer to white, so it's lighter but less saturated or intense. Let's take the example of a starting RGB of (50,192,240) that you want to make 20% brighter. The result is (60,230,288) - red and green are within bounds, but blue is too bright and has overflowed. Take the excess and distribute it to the other colors equally - 288-255 is 33, so add 16.5 to the red and green; rounding gives you a result of (41,247,255).

Mark Ransom
Thanks for bringing this issue to my attention. Wasn't even aware of it.
Michael Todd