3

I have done the brightness increasing but unable to decrease after trying a lot. For example: my rgb values of a pixel are r=100 , g = 200 ; b =125 . I'm using numericupdown to increase and decrease the value. When I add ,for example, 100 using numupdown. the new values will be r=200 , g=300 and b=255. But we take g=300 -> g=255 because we can't go further than 255. When I decrease the value to 100 , the values should be r=100 , g=200 , b=125 back. Due to changing the value of g It would be no more g=200 because g is equal to 255 and 255-100=155 which is not equal to 200..Seeking help to set the pixel values again to same while decreasing .

P.s : I'm a learner

Ali Raza
  • 33
  • 5
  • 1
    The most simple solution would be to keep the original image in memory and display a copy at all times. You only modify the copy. – Ed S. Mar 09 '14 at 19:51
  • @EdS. has the only real answer. The brightness changes can't be reversed with an additive logic. – Emile Bergeron Mar 09 '14 at 19:53

3 Answers3

3

Store the original image and display a copy. Every time you run your algorithm you read the pixel values of the original and write the modified pixel values into the copy.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • This is the proper way. Possibly combine with an approved brightness equation / algorithm instead of directly incrementing R,G and B channels. – bastijn Mar 09 '14 at 21:23
  • Ahaan! That was a wonderful idea , Thank you Sir. I applied it and succeeded – Ali Raza Mar 10 '14 at 14:11
  • @AliRaza and for the mechanics of changing brightness itself I would start at http://stackoverflow.com/questions/141855/programmatically-lighten-a-color – Mark Ransom Mar 13 '14 at 15:57
0

Note: this is a very simple approach. Brightness is a well discussed subject with a lot of options. For sophisticated solutions you often also drag in saturation and much more. Per pixel options are maybe not the best approach, but for the sake of this post I have constructed an answer that will solve your specific problem below.

// edit 2 Thinking about this some more, I did not think about the solution to the equation not being unique. You indeed need to store the original and recalculate from the original image. I would still advice using an approved brightness equation like the ones found in the link above. Simply modifying R,G, and B channels might not be what your users expect.

The below answer must be combined with working on the original image and displaying a modified copy as mentioned in other answers.

I would not increase R, G, and B channels directly but go with a perceived brightness option like found in here.

Lets say you take:

L = (0.299*R + 0.587*G + 0.114*B)

You know the min(L) will be 0, and the max(L) will be 255. This is where your numeric up/down will be limited to [0,255]. Next you simply increase/decrease L and calculate the RGB using the formula.

//edit You case as example:

r=100 , g = 200 ; b =125
L = (0.299*100 + 0.587*200 + 0.114*125)
L = 161.5

Now lets go to the max (limited) to get the extreme case and see this still works:

L = 255
L = (0.299*255 + 0.587 * 255 + 0.114 * 255)
RGB = (255,255,255)

Going back will also always work, 0 gives black everything in between has a guaranteed RGB in range R,G,B in [0,255].

Community
  • 1
  • 1
bastijn
  • 5,841
  • 5
  • 27
  • 43
  • ahaan! Will the new values be rgb=(L,L,L) ? – Ali Raza Mar 09 '14 at 20:34
  • No, the new values can be found solving the equation I mentioned in my post. See the examples I added. You always store the current L, and modify the L using your numeric up/down keys. Then knowing the new L you solve the equation for L. – bastijn Mar 09 '14 at 21:11
  • I did not verify if solving the equation will bring a unique answer. You have to verify that or this method won't work :). – bastijn Mar 09 '14 at 21:18
  • Thank you a lot sir for your cooperation . – Ali Raza Mar 10 '14 at 14:16
0

Another solution, possibly more elegant, would be to map your RGB values to the HSV color space.

Once you are in the HSV color space you can increase and decrease the value (V) to control brightness without losing hue or saturation information.

This question gives some pointers on how to do the conversion from RGB to HSV.

Community
  • 1
  • 1
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83