What's the formula to increase the luminance in an image in a similar way how the L component in Photoshop does it?
For example I have a pixel with these values:
CGFloat red = 34 // 0 - 255
CGF开发者_JAVA百科loat green = 128
CGFloat blue = 190
and I want to increase luminance so that blue is 255. NOTE: With "Luminance" I mean the L-component of Photoshop in LAB color space. See pictures!
There must be a special formula to compute the RGB values for the increased luminance, because the R G B values are modified in a non-linear way!
Proof: I did a test in Photoshop and created this same color, and then opened the color picker to examine it:
Then I activated the L component of the LAB color space, which controls luminance (at least that is what I am talking about - I mean the brightness controlled by that L component. Guess that is luminance).
So with L activated, I dragged the slider on the left upwards until B reached 255:
Now read the resulting R G B values:
CGFloat newRed = 112 // 0 - 255
CGFloat newGreen = 188
CGFloat newBlue = 255
The differences between these is:
newRed - red = +78
newGreen - green = +60
newBlue - blue = +65
The percentages are:
red shift: +38.42%
green shift: +29.55%
blue shift: +32.01%
This does not correspond with the known formula for computing Luminance out of R G B, which is something close to luminance = (red * 0.3) + (green * 0.6) + (blue * 0.1)
.
Obviously, they have been shifted in a non-linear manner.
Is there a known way to compute newRed, newGreen, newBlue out of red, green, blue in a similar fashion how Photoshop does it?
So you want to convert an RGB color into the Lab color space? Sure, that's possible.
But there are a lot of different standards that are all referred to with the general name "Lab" color. You'll need to select one to use when writing your algorithm. Photoshop in particular uses the CIELAB D50 standard, so you would want to use that one if you need to accurately simulate its processing.
Another thing you have to keep in mind is that the RGB model is device-dependent, which means that in order to convert from RGB to Lab, you'll first need to convert from RGB into some device-independent, absolute color space. Adobe does this with their "Adobe RGB" format, but you can use something standard like sRGB. The adjustment process considers the device-dependence of the RGB color, but transforms the color into one that is device-independent. Once you have that, you can take the last step of transforming the color into the Lab color space.
The Wikipedia article provides some background information and useful formulas when writing a conversion algorithm. And here's someone that has already written such a conversion algorithm. And one for MATLAB you could probably convert.
Depending on how serious you are (so probably not), you could also download the source for GIMP and see how they've implemented the conversion algorithms. Their software claims to support the Lab space.
Be careful, though, when converting back to RGB. The Lab color space can represent colors that are well outside the range of sRGB. Even in Photoshop, most of what you see in Lab mode is discarded once you convert the image back to RGB (or CMYK).
Edit: It's also worth considering that if your only goal is to lighten a color in a color space that better approximates human color perception than RGB, you might not need to fuss with Lab at all. Lots of applications do this by converting to either the HSL or HSV color space. (Photoshop calls HSV, HSB).
The advantage is that you can simply adjust the "Lightness" or "Value" values to alter the intensity of the color (much like you would adjust the "Luminance" value in the Lab color space), but the conversion algorithms are much simpler (and often built into the standard libraries of your language of choice).
For example, see my answer to this question for a conversion algorithm written in a .NET language.
精彩评论