Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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].

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top