Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to modify brightness for RGB image?

I know there is formula for going RGB -> Luminance, but I need given a brightness parameter to modify the RGB values of an image. How do I do that?

Thanks

like image 316
user1475859 Avatar asked Jun 22 '12 20:06

user1475859


2 Answers

The easiest way is to multiply each of the R,G,B values by some constant - if the constant is >1 it will make it brighter, and if <1 it will be darker. If you're making it brighter then you must test each value to make sure it doesn't go over the maximum (usually 255).

Not only is this simpler than the translation from RGB to HSL and back again, but it more closely approximates what happens when you shine a different amount of light at a physical object.

like image 143
Mark Ransom Avatar answered Oct 05 '22 06:10

Mark Ransom


Adding to Mark Ransom's Answer: It would be better to use the said factor with a 255 constant and add it to the current color-value:

float brightnessFac = //between -1.0 and 1.0    
byte brightnessRed = red + (255f * brightnessFac);

If you just do with a factor between 0.0 and 1.0

byte brightnessRed = red * brightnessFac;

A value of 0 stays zero.

like image 36
Patrick Favre Avatar answered Oct 05 '22 05:10

Patrick Favre