# Creating a function to brighten or darken colors

I want to create a function $f : [0,255]^3 \rightarrow [0,255]^3$ over the integers such that if you pass in three RGB color values, a triple with a darker or lighter color will be returned, and I want the values to increase in some sort of proportion to how dark or light they are already; lightness and darkness being determined by, say, taking the average of the values.

Is there a relatively simple function that will do this? If not, is there a way to emulate it?

-
Why not convert to HSB, perform the operations there, and convert back? – J. M. Dec 13 '10 at 2:52
There is a bijection used by my dad when he taught elementary school to scale grades, which can be modified to be $h(x) = 16 * \sqrt{x}$ for $x\in [0,256]$. You can perform this entry-wise on RGB. To change the dependence on the starting value, you can change $\sqrt{x}$ to $|x|^p$ for $p < 1$. – Willie Wong Dec 13 '10 at 3:09
[real-analysis] is inappropriate. [functions] seems inappropriate too, but I have left that in. – Aryabhata Dec 13 '10 at 3:16
@Willie: In image processing, they call that gamma correction. – Rahul Dec 13 '10 at 3:25
@Rahul: Thanks! Learned something new everyday. – Willie Wong Dec 13 '10 at 3:45

There is a bijection used by my dad when he taught elementary school to scale grades, which can be modified to be $h(x)=16 \sqrt{x}$ for $x\in [0,256]$. You can perform this entry-wise on RGB. To change the dependence on the starting value, you can change $\sqrt{x}$ to $|x|^p$ for $p<1$.