Color Temperature Conversion (K) to RGB: Algorithm and Sample Code

3r33426. 3r3-31. 3r33140. 3r33412. 3r33426. 3r33412. 3r33426. If you don't know what color temperature is, r3r37. start from here

. 3r33412. 3r33426. 3r33412. 3r33426. Working on the Color Temperature tool for PhotoDemon I spent the whole evening trying to define a simple and clear algorithm for the conversion between temperature values (in Kelvin) and RGB. I thought that such an algorithm would be easy to find, because in many photo editors there are tools for correcting color temperature, and in every modern camera, including smartphones, there is an adjustment of white balance based on lighting conditions. 3r33412. 3r33426. The source

[/i] 3r33412. 3r33426. 3r33412. 3r33426. It turned out to find a reliable formula for converting temperature to RGB is almost impossible. Of course, there are some algorithms, but most of them work by converting the temperature to the XYZ color space, to which you can then add an RGB conversion. Such algorithms seem to be based on the Robertson method, one implementation of which is 3r-334. here is

and another here is . 3r33412. 3r33426. 3r33412. 3r33426. Unfortunately, this approach does not provide a purely mathematical formula - it is just an interpolation on the conversion table. This may be reasonable under certain circumstances, but when you consider the additional XYZ → RGB conversion, it turns out too slow to simply adjust the color temperature in real time. 3r33412. 3r33426. 3r33412. 3r33426. So I wrote my own algorithm, and it works damn well. Here is how I did it. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Warnings regarding this algorithm

3r33412. 3r33426. 3r366. Warning 1 [/b] : my algorithm provides a high-quality approximation, but it is not accurate enough for serious scientific use. It is intended primarily for manipulating photographs - so do not try to use it for astronomy or medicine. 3r33412. 3r33426. 3r33412. 3r33426. 3r366. Warning 2 [/b] : Because of its relative simplicity, this algorithm is fast enough to work in real time on images of reasonable size (I tested it on 12-megapixel images), but to achieve the best results, you should apply the mathematical optimizations specific to your programming language. I show the algorithm without mathematical optimizations, so as not to complicate it. 3r33412. 3r33426. 3r33412. 3r33426. 3r366. Prevention 3 [/b] : The algorithm is intended only for use in the range from 1000 K to 40000 K, which is a good range for photography. (In fact, it is much more than may be required in most situations). Although it works for temperatures outside this range, but the quality will decline. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Special thanks to Mitchell Charity

3r33412. 3r33426. First, I have to pay a big debt and thank Mitchell Charity for the initial data that he used to create these algorithms: 3r3145. raw black file

. Charity provides two data sets, and my algorithm uses 3r378. 10-degree color matching function CIE 1964

. A discussion of the 2-degree function of the CIE 1931 with Judd Vos corrections versus a 10-degree set is beyond the scope of this article, but if you're interested, you can start a comprehensive analysis from 3–380. of this page

. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Algorithm: an example of the issue

3r33412. 3r33426. Here is the output of the algorithm in the range from 1000 K to 40000 K:

3r33426. 3r33412. 3r33426. 3r3394. 3r33412. 3r33426. * 3r33375. The output of my algorithm is from 1000 K to 40000 K. The white dot is at 6500−6600 K, which is ideal for processing photos on a modern LCD monitor 3r37676. * 3r33412. 3r33426. 3r33412. 3r33426. Here is a more detailed snapshot of the algorithm in the interesting range for photography from 1500 K to 15000 K: 3r33412. 3r33426. 3r33412. 3r33426. 3r33375. The same algorithm, but from 1500 K to 15000 K

[/i] 3r33412. 3r33426. 3r33412. 3r33426. As you can see, banding is minimal, which is a great improvement over the above-mentioned correspondence tables. The algorithm also does a great job of preserving the light yellow tint near the white point, which is important for simulating daylight in post-processing photos. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. How I came to this algorithm

3r33412. 3r33426. The first step to deriving a reliable formula was to build a graph. original black body values from Charity . You can download entire spreadsheet in the format of LibreOffice /OpenOffice .ods (430 KB) . 3r33412. 3r33426. 3r33412. 3r33426. Here are the data after plotting:

3r33426. 3r33412. 3r33426. 3r33140. 3r33412. 3r33426. * 3r33375. Data of original temperature (K) in RGB (sRGB), graph LibreOffice Calc. Again, the conversion is based on the 10-degree CMF function of the CIE 1964. The white dot, as required, is between 6500 K and 6600 K (the peak is on the left side of the graph). 3r3145. The source* 3r33412. 3r33426. 3r33412. 3r33426. It is easy to notice that there are several sections that simplify our algorithm. In particular: 3r33412. 3r33426. 3r33412. 3r33426.

3r33426. 3r3165. Red values below 6600 K are always 255 3r3r1616. 3r33426. 3r3165. Blue values below 2000 K are always 0

3r33426. 3r3165. Blue values above 6500 K are always 255

3r33426. 3r3168. 3r33412. 3r33426. It is also important to note that in order to fit the curve to the given green color, it is best viewed as two separate curves - one for temperatures below 6600 K and the other for temperatures above this point. 3r33412. 3r33426. 3r33412. 3r33426. From this point on, I divided the data (without the “always 0” and “always 255” segments) into separate color components. In an ideal world, the curve can be adjusted to each set of points, but, unfortunately, in reality it is not so simple. Since the graph has a strong discrepancy between X and Y values — all x values are greater than 1000 and are displayed in 100 dotted segments, while y values are between 255 and 0 — you had to transpose x data to get a better fit. In order to optimize, I first divided the value of x (temperature) by 100 for each color, and then subtracted as much as needed, if it greatly helped in fitting to the graph. Here are the resulting charts for each curve, as well as the most appropriate curve and the corresponding value of the coefficient of determination (R-square):

3r33426. 3r33412. 3r33426. 3r3179. 3r33412. 3r33426. 3r33412. 3r33426. 3r3184. 3r33412. 3r33426. 3r33412. 3r33426. 3r3189. 3r33412. 3r33426. 3r33412. 3r33426. 3r3194. 3r33412. 3r33426. 3r33412. 3r33426. I apologize for the terrible kerning of fonts and hinting on diagrams. LibreOffice has a lot of advantages, but the inability to smooth fonts on graphics is completely shameful. I also do not like to extract diagrams from screenshots, because they do not have the export option, but it is better to leave it for later. 3r33412. 3r33426. 3r33412. 3r33426. As you can see, all the curves are fairly well aligned, with the values of the coefficient of determination above ???. I could spend more time fitting the curves, but this is enough for processing photos. No average person would say that the curves do not exactly correspond to the original idealized black body observations, right? 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Algorithm

3r33412. 3r33426. Here is the algorithm in all its glory. 3r33412. 3r33426. 3r33412. 3r33426. First, the pseudo code: 3r33412. 3r33426. 3r33412. 3r33426.

` Let's start with the temperature in Kelvin somewhere between 1000 and 4?000. (Other values may work, but I cannot make any promises about the quality of the estimates of the algorithm above 4?000 K). Note that temperature and color variables must be declared as floating point variables. 3r33426. 3r33426. Set Temperature = Temperature 100`

3r33426. Calculating red:

3r33426. If Temperature 3r3303. Red = 255 r3r3426. Else

Red = Temperature - 60 r3r3426. Red = ??? * (Red ^ -???) 3r33426. If Red 3r33232. If Red> 255 Then Red = 255

End If

3r33426. Calculating green:

3r33426. If Temperature 3r3303. Green = Temperature

Green = ??? * Ln (Green) - ???r3r3426. If Green < 0 Then Green = 0

If Green> 255 Then Green = 255

Else

Green = Temperature - 60 r3r3426. Green = ??? * (Green ^ -???)

If Green < 0 Then Green = 0

If Green> 255 Then Green = 255

End If

3r33426. Calculating blue:

3r33426. If Temperature> = 66 Then

Blue = 255 r3r3426. Else

3r33426. If Temperature <= 19 Then

Blue = 0

Else

Blue = Temperature - 10

Blue = ??? * Ln (Blue) - ???r3r3426. If Blue < 0 Then Blue = 0

If Blue> 255 Then Blue = 255

End If

3r33426. End If

3r33333. 3r33412. 3r33426. Please note that in the above pseudocode Ln () means the natural logarithm of . Also note that you can omit the “if the color is less than 0” checks, if the temperatures are always in the recommended range. (However, you still need to leave the check "if the color is more than 255"). 3r33412. 3r33426. 3r33412. 3r33426. As for the actual code, here is the exact Visual Basic function that I use in 3r-3275. PhotoDemon. It is not yet optimized (for example, logarithms are much faster with correspondence tables), but at least the code is short and readable: 3r33412. 3r33426. 3r33412. 3r33426.

` 'For this temperature (in Kelvin) we calculate the RGB equivalent of Private Sub getRGBfromTemperature (ByRef r As Long, ByRef g As Long, ByRef b As Long, ByVal tmpKelvin As Long)`

3r33426. Static tmpCalc As Double

3r33426. 'The temperature should be in the range of 1000 to 4?000 degrees 3r33434. If tmpKelvin < 1000 Then tmpKelvin = 1000

If tmpKelvin> 40000 Then tmpKelvin = 40000

3r33426. 'All calculations require tmpKelvin 10? so you can get by with a single conversion

tmpKelvin = tmpKelvin 100

3r33426. 'We calculate all the colors in turn

3r33426. 'First red

If tmpKelvin <= 66 Then

r = 255

Else

'Note: The R-square value for this approximation is ???r3r3426. tmpCalc = tmpKelvin - 60

tmpCalc = ??? * (tmpCalc ^ -???)

r = tmpCalc

If r 3r3304. If r> 255 Then r = 255

End If

3r33426. 'Then green

If tmpKelvin <= 66 Then

'Note: The R-square value for this approximation is ???r3r3426. tmpCalc = tmpKelvin

tmpCalc = ??? * Log (tmpCalc) - ???r3r3426. g = tmpCalc

If g < 0 Then g = 0

If g> 255 Then g = 255

Else

'Note: The R-square value for this approximation is ???r3r3426. tmpCalc = tmpKelvin - 60

tmpCalc = ??? * (tmpCalc ^ -???)

g = tmpCalc

If g < 0 Then g = 0

If g> 255 Then g = 255

End If

3r33426. 'Finally, the blue

If tmpKelvin> = 66 Then

b = 253 r3r3426. ElseIf tmpKelvin <= 19 Then

b = 0

Else

'Note: The R-square value for this approximation is ???r3r3426. tmpCalc = tmpKelvin - 10

tmpCalc = ??? * Log (tmpCalc) - ???r3r3426. 3r33426. b = tmpCalc

If b < 0 Then b = 0

If b> 255 Then b = 255

End If

3r33426. End Sub 3r340. 3r33333. 3r33412. 3r33426. The function was used to generate a sample issue at the beginning of this article, so I can guarantee that it works. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Sample images 3r3013. 3r33412. 3r33426. Here is a great example of what color temperature adjustments can do. The image below — a poster for the HBO series “Real Blood” — spectacularly demonstrates the potential of adjusting color temperature. On the left - the original frame; on the right - adjusting the color temperature using the code above. With one click of the night scene can be remade for daylight. 3r33412. 3r33426. 3r33412. 3r33426. 3r33356. 3r33412. 3r33426. * 3r33375. Adjust the color temperature in action*

3r33412. 3r33426. 3r33412. 3r33426. The real color temperature tool in my PhotoDemon program looks like this:

3r33426. 3r33412. 3r33426. 3r33333. 3r33412. 3r33426. * 3r33375. PhotoDemon*

color temperature tool. 3r33412. 3r33426. 3r33412. 3r33426. 3r33382. Download

program and see it in action. 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Addition, October 2014 3-333401. 3r33412. 3r33426. Renault Bedar did a great online demo. for this algorithm. Thank you, Reno! 3r33412. 3r33426. 3r33412. 3r33426. 3r33400. Addition, April 2015 3-333401. 3r33412. 3r33426. Thanks to everyone who suggested improvements to the original algorithm. I know that the article has a lot of comments, but they are worth reading if you plan to implement your own version. 3r33412. 3r33426. 3r33412. 3r33426. I want to highlight two specific improvements. First, Neil B kindly provided the best version of dFor the original curve fitting functions, which slightly changes the temperature coefficients. The changes are described in detail in 3r3408. his superb article

. 3r33412. 3r33426. 3r33412. 3r33426. Then Francis Lough added some comments and sample images that are very useful if you want to apply these transformations to photos. Its modifications produce a much more detailed depiction,

as seen in examples 3r3155. . 3r33434. 3r33426. 3r33426. 3r33434. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d! ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r33420. 3r33426. 3r33434. 3r33426. 3r33426. 3r33426. 3r33426.

It may be interesting

#### weber

Author**22-10-2018, 21:12**

Publication Date
#### Image processing / Algorithms

Category- Comments: 0
- Views: 398

Wonder Woman Diana Prince Jacket

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.R_Tech

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.PBN

I have a mission that I’m just now working on, and I have been at the look out for such informationWeb designing

Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable articleSoftware House

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!Web Host