4

I've been looking around for a simple algorithm to get and set the brightness of a pixel, but can't find anything - only research papers and complex libraries.

So does anyone know what is the formula to calculate the brightness of a pixel? And which formula should I use to change the brightness?

Edit: to clarify the question. I'm using Qt with C++ but I'm mainly looking for a generic math formula - I will adapt it to the language. I'm talking about RGB pixels of an image in memory. By "brightness", I mean the same as in Photoshop - changing the brightness makes the image more "white" (a brightness value of 1.0 is completely white), decreasing it makes it more "black" (value of 0.0).

laurent
  • 88,262
  • 77
  • 290
  • 428
  • 1
    do you mean the apparent brightness? as in a grayscale? – El Ronnoco Aug 08 '11 at 15:43
  • 2
    Do you mean a pixel on the screen? In memory? On a printer? Do you mean the absolute brightness? Relative brightness? What is the gamma value of the colour space you're working in? What operating system are you targeting? What programming language are you using? – Jonathan Grynspan Aug 08 '11 at 15:44
  • Sorry for the lack of precision, I have updated the question. – laurent Aug 08 '11 at 15:49
  • The question is likely to be closed if you don't get more specific. I'd guess that you have RGB values, and you want to be able to adjust the brightness? In which case, go to/from HSV, as noted in answer. – Ed Staub Aug 08 '11 at 15:49

2 Answers2

5

Change the color representation to HSV. The V component stands for value and represents the brightness!

  • Here the algorithm implemented in PHP.
  • Here is a description of how to do it in C.
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Changing each pixel from RGB to HSV and back to RGB might be slow. Is there any way to change the brightness of an RGB pixel directly? – laurent Aug 08 '11 at 15:52
  • In rgb each component represents the intensity of each color, thus (r+g+b)/3 could be used. Dividing all components by 2 should in that case for example half the brightness. – aioobe Aug 08 '11 at 16:09
  • 1
    @aioobe: that fails to account for gamma correction. This is not how RGB works. – Svante Aug 08 '11 at 16:24
-1

What do you mean by a pixel?

You can set the brightness of a pixel in an image with '=' you just need to know the memory layout of the image

To set a pixel on the screen is a little more complicated

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263