Skip to main content
Inspiring
December 20, 2016
Question

How does the "Color Picker" convert from RGB to LAB?

  • December 20, 2016
  • 4 replies
  • 4543 views

I'm writing a script that converts a document to Index color, then makes a selection of each of the colors it was indexed to.

The scripting command to convert to Index Color seems to only accept RGB values for the colors it will use to index to. To select the colors after indexing, Color Range only accepts LAB colors as arguments. So I have to convert my RGB numbers to LAB values to select the same colors I just used to separate. (scripting the wand only accepts locations to click, not directly entering color values through scripting)

If I bring up the "Color Picker" in Photoshop and enter an RGB value I used to separate, and take the LAB values shown in the color picker and put them in the Color Range in the script, it gets a perfect selection every time. No decimal places on the lab values or anything, just using the whole numbers shown, with the Color Range Min and Max values set to the same LAB value shown and the "Fuzziness" set to 0, it fully selects the exact color needed. But I need to script the conversion, I can't manually bring up the Color Picker, it defeats the whole purpose.

Of course, there's a "Convert color" scripting command. When I use this, I get a different LAB value for the same RGB number than the Color Picker gives.

If I change my Color Settings in Photoshop, the Color Picker always returns the same LAB value for the same RGB value, regardless. (Which tells you that what it's doing is fundamentally wrong, because it only makes sense to convert relative values like RGB number to absolute values like LAB numbers through a color profile, and the same RGB equates to different LAB depending on the profile, but that's another story.)

However, changing the color settings in Photoshop does make the scripting command "Convert color" return (often wildly) different LAB values for the same RGB number.

So, does anyone know what the "Color Picker" does to convert an entered RGB value to LAB, so I can attempt to replicate it and get my selections?

I've noticed the values seem to be closest to what I get using Convert Color with an sRGB profile in Color Settings, but not close enough to make it work.

Thanks,

Tom.

This topic has been closed for replies.

4 replies

Inspiring
December 20, 2016

Also, just to note, Colorsync Utility provides the same values for converting that RGB to LAB using Perceptual, Relative, or Saturation intent, but very different LAB numbers for Absolute Colorimetric rendering intent.

Using Convert Color definitely uses the current working space for the profile, but the ColorSync result leads me to believe that Convert Color is ignoring the Rendering Intent from Color Settings.

D Fosse
Community Expert
Community Expert
December 20, 2016

Standard working space profiles like sRGB or Adobe RGB have only one rendering intent, and that's relative colorimetric. The profile only contains one table.

Whatever intent you pick in Photoshop, it ends up relative colorimetric.

There's a new version 4 sRGB specification from icc that supports other intents, but the standard sRGB IEC61966-2.1 doesn't.

EDIT: to be more precise, matrix profiles don't contain the tables for perceptual. I'm not sure about the situation with absolute colorimetric, which is identical to relative except it doesn't remap the white point.

Inspiring
December 20, 2016

I think I found the answer:

http://www.rags-int-inc.com/phototechstuff/colorcalculator/adobemath.html

It looks like this is due to bugs in how Photoshop does math with it's 15+1 bit internal lab values, sometimes treating them as if they were true 16 bit. It appears that the Info Panel and Color Picker share a math bug that the scripting component Convert Color does not.

I am not wholly convinced this is the problem, but my problem seems very similar to the one documented here.

If that is the case, I am wasting my time messing around with color profiles. I would either need to reverse engineer exactly what math bug leads to these differences and re-implement it in my script, or else I can take the more realistic route of continuing to use the hack I pasted above.

Thanks to everyone for trying to help, and of course let me know if you think I've got this wrong and there's a better explanation than the linked bug. Special thanks to davescm for the insight on Color Picker using document profiles.

D Fosse
Community Expert
Community Expert
December 20, 2016

TomT-shirts wrote:

the insight on Color Picker using document profiles.

That's exactly what I tried to tell you. But OK, I wasn't clear enough, my bad.

If this is a rounding error, I'd like to know if anyone else can reproduce this, and if so how. I can't, I'm going back and forth between sRGB/Lab and Adobe RGB/Lab several different ways, and the numbers always check out.

I'm not denying it's a real issue, I just haven't seen it. It would be nice to know about.

Inspiring
December 20, 2016

You're using the Convert Color scripting function? I mean, when you say you keep going back and forth and back and forth, between what? And when you say the numbers check out - the numbers check out relative to what? How are you doing the color conversion between your RGB numbers/profile and LAB other than in Color Picker, so you know that any of your Color Picker Numbers "check out?" What's the "check" on them checking out?

To get a third value to check PS's against, I'm using Apple's Colorsync utility to do the conversions, and it makes it appear that it is Convert Color that is off and the Color Picker / Info window are correct.

Using sRGB IEC61966-2.1, with Perceptual, Relative, or Saturation intent, it converts RGB {250,200,150} to LAB {84,14,42}.

That's the values for Photoshop's Color Picker / Info Window. So if a math bug is the answer, the bug would appear to be in Convert Color.

Inspiring
December 20, 2016

So, while I have not found the correct answer, I did find a hacky work-around for this problem, which I am posting in case someone else finds this thread in the future and it helps them.

The Color Sampler Tool, when set to LAB readout, does return the same LAB values as the Color Picker. Color Samplers can be created via scripting, but unfortunately, it appears to be impossible to set the mode of the sample via scripting. New samplers default to reading in the document's color mode, so you can convert the document mode to the desired output and then create the sampler. But the mode conversion honors the Color Settings, so now the Color Sampler will be returning the same values that Convert Color provides... not good. Converting the document back to the original mode after creating the sampler... changes the sampler's output mode too...

So, I created a 1 pixel square empty RGB document with a pre-made Color Sampler with LAB readout already on it. The script opens this saved document every run. It fills the pixel with the RGB number, reads the LAB value off the Color Sampler, and repeats the process through all values. Then it closes the document.

So this works - it is automated, and it converts RGB values to LAB values using whatever the Color Picker uses to do the same, and the values I get from this work perfectly every time for selecting the colors in the document with Color Range with 0 tolerance.

I am still interested to know what is going on in the "Color Picker," if anyone actually knows the answer to that. This problem reminds me how sad I am that Bruce Fraser died, because perhaps he alone both understood Photoshop's byzantine rules for using color, and was willing to share that knowledge with the world.

D Fosse
Community Expert
Community Expert
December 20, 2016

OK, have it your way, but you're really overcomplicating this to the point where it confuses everyone, including yourself.

As long as color space is consistent, numbers are consistent.

It really is that simple, as Bruce Fraser would have told you. There are no "byzantine rules" at play here.

Inspiring
December 20, 2016

D Fosse wrote:

OK, have it your way, but you're really overcomplicating this to the point where it confuses everyone, including yourself.

As long as color space is consistent, numbers are consistent.

It really is that simple, as Bruce Fraser would have told you. There are no "byzantine rules" at play here.

I agree that if the color space (and color conversion settings and CMM) are consistent, then the numbers will be consistent. In practice, the numbers are not consistent, therefore, the color space is not consistent. This is the reasoning that led me to ask this question, "How does the "Color Picker" convert from RGB to LAB?" I am attempting to get consistent numbers, so if I knew what it was doing, then I might be able to recreate it.

I am sorry this is so complicated.  I keep try to find a way to state it more simply. I want to know how Photoshop calculates the color conversions it does in the "Color Picker" dialog. I am struggling to find a simpler way to word it.

Theresa J
Community Expert
Community Expert
December 20, 2016

From my understanding, LAB is the true color. RGB is a value of the LAB color based on the intended display or output, which is what the profiles are for. So it makes complete sense that different profiles would return different RGB values of the same LAB color. I've never considered how the conversion works in the opposite direction though, from RGB to LAB.

You said the color picker always returns the same LAB value for the same RGB value which makes sense when I think about it. The values are absolutes. The RGB profiles just control the perception of the values. It seems to me that if you started with an sRGB document as your first step, you should get consistent numbers for sampled colors. But maybe I missed something in your explanation.

Inspiring
December 20, 2016

The documents all start off in sRGB as both the working space and the document profile.

If I enter an RGB number in "Color Picker," it gives me one set of LAB values. If I use the scripting function "convert color" to convert the same RGB number to LAB, I get a different LAB number.

Only the LAB number provided by "Color Picker" works to actually select the colors in the document when used with the Color Range function. The LAB numbers provided by "Convert Color" for the same RGB number do not select the colors in the document.

So I need to know how to get the LAB number provided by the "Color Picker" without manually typing every RGB number into the "Color Picker" and writing down the LAB numbers it generates.

It does not make sense to go either direction between RGB and LAB without a color profile. It's impossible. It's equating real, defined colors (LAB) to numbers that represent a different color on every device. You have to know the profile to equate the RGB numbers to real colors.

There is some conversion going on in the Color Picker. If I could find out what assumptions (ICC profile, rendering intent, possible rounding) underlie that conversion, I might be able to convert my document to color settings that will make "Convert Color" behave the same as "Color Picker."

It is really annoying that asking Photoshop to convert an RGB number to LAB in the same document, with the same color settings, gives very different answers depending on where you ask.

The numbers are similar enough with an sRGB profile that I suspect that may be involved with what Color Picker does, but I can't find settings that make it close enough to work.

Inspiring
December 20, 2016

TomT-shirts wrote:

It does not make sense to go either direction between RGB and LAB without a color profile

Absolutely correct, so for everyone's benefit you should call it sRGB if that's what it is, not "RGB" which is undefined.

There is some conversion going on in the Color Picker.

Yes, and all of it is based on what you have as defaults and policies in Color Settings. The open document overrides Color Settings if they're different.

If document profile and working space are the same, the readouts should be identical. If they're not, I can think of four possible reasons:

  • a color defined in Lab is out of sRGB gamut
  • the eydropper is set to a large sample size, picking up neighboring color
  • it's a jpeg compression artifact (yes, jpeg can do that)
  • it's a rounding error working in 16 bit depth and an 8 bit readout

The monitor profile doesn't come into play here, unless you're sampling from screenshots. If that's what you're doing, that explains it right there.


D Fosse wrote:

TomT-shirts wrote:

It does not make sense to go either direction between RGB and LAB without a color profile

Absolutely correct, so for everyone's benefit you should call it sRGB if that's what it is, not "RGB" which is undefined.

Why would I call it "sRGB" when I type RGB values into Color Picker? The fact that Photoshop returns different LAB values for the conversion than "Convert Color" returns for the same RGB values in the sRGB profile implies that is is not sRGB. I don't know what it is... the point of the question is asking what it's doing, which probably mostly means "what profile and settings is it using for conversion." If sRGB were correct, I wouldn't have had a question.

There is some conversion going on in the Color Picker.

Yes, and all of it is based on what you have as defaults and policies in Color Settings. The open document overrides Color Settings if they're different.

This is inaccurate, as stated in my initial question. Try it yourself. You can make the most radical changes to your Color Settings, and the Color Picker always returns the same LAB values for the same RGB values.

If document profile and working space are the same, the readouts should be identical. If they're not, I can think of four possible reasons:

The values returned for Convert Color vary wildly based on Color Settings - which they should. But the values for Color Picker never change. I'm attempting to find a color conversion that equals what Convert Color does. I can find no case where the outputs are identical, that is the problem I'm trying to solve.

  • a color defined in Lab is out of sRGB gamut

I'm having trouble seeing how this is relevant - I am always starting with an RGB number, and converting it to LAB using two different UI's. All RGB is within LAB gamut. But even if I were performing an out-of-gamut conversion, that doesn't explain why I get different results from Photoshop for the same conversion, or what Color Picker does to convert.

  • the eydropper is set to a large sample size, picking up neighboring color

Since I am manually typing in RGB values, I doubt the eyedropper settings are affecting the outcome.

  • it's a jpeg compression artifact (yes, jpeg can do that)

The idea that JPG compression artifacts are affecting this seems far fetched to me. I don't even have to have any document open in Photoshop to do these conversions, both Color Picker and Convert Color function with no documents open.

  • it's a rounding error working in 16 bit depth and an 8 bit readout

This could only be the cause if the suggested answer is "the Color Picker uses sRGB, and the differences you're still seeing when you use sRGB in Color Setting are due to rounding errors." However, this can not be the cause of the error, because the difference are much too great in magnitude to be rounding errors.

The monitor profile doesn't come into play here, unless you're sampling from screenshots. If that's what you're doing, that explains it right there.

It would not actually have any effect if I were sampling from screenshots, or even directly from the screen itself. Where I sample from to get an RGB number does not change the returned LAB values for that number by either conversion method. But I am not sampling, I am typing in RGB numbers.