# How do color values change mathematically as you change temperature and tint?

I was wondering what does color temperature do to your pixel's RGB/LAB values when you adjust it.

I get that things get more yellow or more blue in the case of temperature, but what equation is Lightroom/Camera RAW using? Is it simply 1-uping the B-channel values for every pixel when you move the temperature up a notch?

My other question is this: I noticed that changing the temperature and tint significantly changed the gray values when I converted my photo to black and white and worked in that space. Other color tools actually add color instead, like color grading, so it's no longer black and white, it gets a cast, and some don't work at all, like saturation. So does that mean that the A and B values of LAB influence luminance values in black and white? I'm guessing that yellow would make things brighter? Is Lightroom changing L values or does it preserve them and generate a preview on top where color values influence gray values?

I'm trying to program a photo software and maybe one day a camera app so I'm interested in what is specifically going on.

• Have you looked at code in open source image editing software like RawTherapee and Darktable? To me this is the place to start because editing pictures is an exercise in photometry not wave physics. The computations are typically non-linear because human visual perception is non-linear. Commented Jan 21, 2021 at 20:04
• Does this answer your question? How can I adjust the colour temperature of an image programmatically? Commented Jan 22, 2021 at 14:46
• @L.ScottJohnson It really helps. This is like finding buried treasure, both these answers and the ones over there. I think they are both helpful. Commented Jan 25, 2021 at 6:55
• I asked this question a while back with no satisfactory answer. What *exactly* is white balance? Commented Feb 1, 2021 at 9:56

This is a more tricky question that you might think.

First, the RGB value that you see with your eyes on the screen are the result of an operation on the processed output from Lightroom, that is defined by the calibration of you screen. So beware what RGB values you measure.

Second, when you are adjusting color temperature, what you are really doing is reverse-engineering the way your brain interprets the effect of outside light on the object you took a picture of. This is quite a complex process, given that outside lights are not simple RGB rays, but a mixture of. And on top of that not every brain does it the same. And they do not do it linearily. Most experienced and well-staffed software companies are still struggling in 2021 to get it right.

This has been first studied extensively with the determination of the CIE xy model in 1931, which is the godfather of every color processing today and was aimed at defining the "average" understanding of color by humans.

Long story short, you'll need a few hours, or rather days to grasp the math underneath it and if you haven't been taught matrixes, it may be weeks or months...

I suggest if you are interested you look for colorimetry theory to get familiar with the subject.

• @Hugues Your answer was actually really helpful. Much of what I need is just knowing what to study where. Do you happen to know of any textbooks or online classes that teach this stuff? I plan not to look at any GNU or CC-SF source code because then if it's GNU your code has to be too, from what I understand about code copyright law. Commented Jan 25, 2021 at 6:38
• Hi Ikari, If I may give you an advice, before wondering on coding, you need to work on the theory. I am a native French speaker, so my sources are in French. See profil-couleur.com for example which is an amazing source, and try maybe google translate or google search some keywords. Wikipedia can help too. Commented Jan 26, 2021 at 18:01
• For example, translate.google.com/… Commented Jan 26, 2021 at 18:08

Adobe actually describes extremely well their white balancing process in the Digital Negative (DNG) Specification, specifically in Chapter 6 Mapping Camera Color Space to CIE XYZ Space.

The process is not super trivial but the core principle is to compute a matrix converting from Camera Space, i.e. space the captured image is upon capture, to CIE XYZ tristimulus values under D50 illuminant for a given set of white balance chromaticity coordinates xy.

With the chromaticity coordinates, it is possible to compute the corresponding correlated colour temperature (CCT), i.e. colour temperature and D_uv, using Robertson (1968) method.

Once the CCT is computed, the matrix from Camera Space to CIE XYZ is obtained via linear interpolation of the ColorMatrix1 and ColorMatrix2 matrices that Adobe (or other vendors following the DNG specification) provide for the camera, and typically embedded in a DCP profile, or EXIF/XMP metadata. The two matrices are commonly given for tungsten-like light source, e.g. Illuminant A (CCT ~= 2856K) and daylight-like, e.g. D65 (CCT ~=6505K) and transform from CIE XYZ to camera space.

There are more details in the specification, but this is really the core of the algorithm.

We have a full Python implementation in colour-hdri in the colour_hdri.models.dng module. An example would be like that:

M_color_matrix_1 = np.array(
[[0.5309, -0.0229, -0.0336],
[-0.6241, 1.3265, 0.3337],
[-0.0817, 0.1215, 0.6664]])
M_color_matrix_2 = np.array(
[[0.4716, 0.0603, -0.0830],
[-0.7798, 1.5474, 0.2480],
[-0.1496, 0.1937, 0.6651]])
M_camera_calibration_1 = np.identity(3)
M_camera_calibration_2 = np.identity(3)
analog_balance = np.ones(3)
XYZ_to_camera_space_matrix(  # doctest: +ELLIPSIS
np.array([0.34510414, 0.35162252]),
2850,
6500,
M_color_matrix_1,
M_color_matrix_2,
M_camera_calibration_1,
M_camera_calibration_2,
analog_balance)

array([[ 0.4854908...,  0.0408106..., -0.0714282...],
[-0.7433278...,  1.4956549...,  0.2680749...],
[-0.1336946...,  0.1767874...,  0.6654045...]])


After some more research I think I have an answer to my question. I'm saying the other answers are wrong, they sound very informed, but this is just what I've found about how the sliders of color temperature work, though not necessarily how cameras work or auto-WB or LR/ACR's algorithm for interpreting metadata.

I watched a YouTube video about how to adjust the temperature and tint in Photoshop without going into ACR and what the instructor recommended was to go into LAB mode and open up the curves and in the A and B channel, scoot the top and bottom node in until they are touching the spike in the histogram. If you want to add more or less temperature or tint, adjust one of the nodes so that it is asymmetric, so that it is a little more to the left or right of the center than the other. I would say this method is more finicky than LR or ACR, as anyone whose messed with the LAB curves could guess, but it just goes to show that that's probably what's going on under the hood.

I deduced from this that while XYZ and LAB and matrixes must all be in play when picking colors, the LAB slider's job is only to create the opposite color of the color cast in order to cancel it out, and that can be done with simple LAB mixing.

LAB primaries are a good choice not only because the LAB color space is big enough to fit all the other color spaces, but also because it makes white balancing really simple for beginners. Anything on the warm end of the Kelvin scale can be canceled out with blue and green, which both slide to the left. Colors on the cool end of the Kelvin scale can be canceled out with yellow and magenta, which are both to the right. Nice and easy for the end user.