Monday, August 5, 2019

TI-84 Plus CE and HP Prime: Calculating Color Temperature

TI-84 Plus CE and HP Prime:  Calculating Color Temperature

Introduction

We can estimate a color temperature for a given color.  However, the estimate is most useful when the color is close to white or the black body curve.  The color temperature, known as the CCT, is just one particular aspect of a color and its light quality. 

For more detailed information, please check the article by Waveform Lighting, https://www.waveformlighting.com/tech/calculate-color-temperature-cct-from-cie-1931-xy-coordinates.  The article will be listed in the Sources section at the end of this blog entry.

Calculation

The program RGBTEMP will estimate CCT from a color given its RGB (red-green-blue) coordinates. 

The calculation involves several steps:

1.  Scaling RGB values from a scale of 0-255 to 0-1.  This is accomplished by dividing each of the parameters of the color's RGB value by 255.  Standard RGB values (sRGB) are used. 

2.  The scaled RGB values will be converted to CIE 1931 XYZ color space values.  In 19031, the International Commission on Illumination created the CIE 1931 color space, designed to describe how the distribution of color wavelengths affect how colors are perceived.  The conversion is done in two parts.

2a.  Adjust each of the scaled RGB values to account for the gamma correction:

Let I be the scaled parameter for R, G, and B.  Then:

If I ≤ 0.04045, Then:

I_adj = I/12.92

Else:

I_adj = ( (I + 0.055) / 1.055) ^ 2.4

2b.  Calculate the XYZ values.   This can be accomplished by matrix multiplication:

[ [ X ] [ Y ] [ Z ] ] =

[[ 0.4124, 0.3576, 0.1805 ] [ 0.2126, 0.7152, 0.0722 ] [ 0.0193, 0.1192, 0.9504 ]]
*  [ [ R ] [ G ] [ B ] ]

3.  With the XYZ coordinates, calculate CIE Yxy coordinates.  Y is already done, so only (small) x and (small) y are needed:

x = X / (X + Y + Z)

y = Y / (X + Y + Z)

4.  Calculate the CCT.  There are several methods: one of them is a cubic approximation from Calvin McCamy:

n = (x - 0.3320) / (0.1858 - y)

CCT = 437 * n^3 + 3601 * n^2 + 6861 * n + 5517

CCT is in Kelvins.

TI-84 Plus Program RGBTEMP

"2019-07-11 EWS"
Disp "COLOR TEMP FROM RGB"
Input "RED   :",R
Input "GREEN :",G
Input "BLUE  :",B

[[R/255][G/255][B/255]]→[I]

For(I,1,3)
If [I](I,1)≤0.04045
Then
[I](I,1)/12.92→[I](I,1)
Else
(([I](I,1)+.055)/1.055)^2.4→[I](I,1)
End
End

[[.4124,.3576,.1805][.2126,.7152,.0722][.0193,.1192,.9504]]*[I]→[J]

[J](1,1)/([J](1,1)+[J](2,1)+[J](3,1))→X
[J](2,1)/([J](1,1)+[J](2,1)+[J](3,1))→Y

(X-.332)/(.1858-Y)→N
437*N^3+3601*N^2+6861*N+5517→C
Disp "CCT (K):",C

HP Prime Program RGBTEMP

The HP Prime version returns CCT, the XYZ coordinates, and the x and y parameters in a four element list.

EXPORT RGBTEMP(R,G,B)
BEGIN
// 2019-07-11 EWS
// RGB to color temperature
LOCAL M0,M1,X,Y,N,C;
LOCAL I,U,V,N,CCT;

// initialize
M0:=[[R/255],[G/255],[B/255]];

// correction
FOR I FROM 1 TO 3 DO
IF M0(I,1)≤0.04045 THEN
M0(I,1):=M0(I,1)/12.92;
ELSE
M0(I,1):=((M0(I,1)+0.055)
/1.055)^2.4;
END;
END;

// RGB to CIE XYZ 1931
M1:=[[0.4124,0.3576,0.1805],
[0.2126,0.7152,0.0722],
[0.0193,0.1192,0.9504]]*M0;

// XYZ to Yxy
X:=M1(1,1)/(M1(1,1)+M1(2,1)+
M1(3,1));
Y:=M1(2,1)/(M1(1,1)+M1(2,1)+
M1(3,1));

// xy to CCT (Kelvins)
// McCamy approximation
N:=(X-0.3320)/(.1858-Y);
C:=437*N^3+3601*N^2+6861*N+5517;

RETURN {C,M1,X,Y};

END;

Examples
(CCT rounded to four decimal places)

Remember the closer to the Black Body Curve, the more accurate the answer. 

Red:  RGB (255, 0, 0)
CCT:  3034.8988 K

Green:  RGB (0, 255, 0)
CCT:  6068.7576 K

Orange:  RGB (255, 165, 0)
CCT:  2429.5395 K

Sky Blue:  RGB (136, 206, 235)
CCT:  13207.1056 K

White:  RGB (255, 255, 255)
CCT:  6506.6551 K

Sources

"Convert color data into different standards and color spaces" and "Color math and programming code examples"   EASYRGB.  IRO Group Limited.  2019. https://www.easyrgb.com/en/convert.php  and https://www.easyrgb.com/en/math.php    Retrieved July 11, 2019

"Calculate color temperature (CCT) from CIE 1931 xy coordinates"  Waveform Lighting. 2019.  https://www.waveformlighting.com/tech/calculate-color-temperature-cct-from-cie-1931-xy-coordinates   Retrieved July 6, 2019

"sRGB"  Wikipedia.  Last edited May 18, 2019.  https://en.wikipedia.org/wiki/SRGB
Retrieved June 16, 2019.

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...