Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from RGB values to color temperature?

Tags:

python

colors

How does one take a color expressed in an RGB value (say, three coordinates from 0-255) and produce from it a color temperature in kelvin (or mireds)?

I see this question which looks pretty close. However, the question mentions x and y, an answer mentions R1 and S1, which I think are CIE XYZ color space coordinates. I'm not quite sure how to get to those either. Someone else links to a paper. Someone else says RGB values are meaningless without "stating a color space" (I thought my monitor decided to display something simply from RGB values?).

Can someone just lay out the whole thing without pointing to other places and assuming I know what all the color terminology is?

like image 542
dfrankow Avatar asked Aug 10 '16 14:08

dfrankow


2 Answers

You could use Colour to perform that computation using the colour.xy_to_CCT_Hernandez1999 definition:

  import numpy as np
  import colour

  # Assuming sRGB encoded colour values.
  RGB = np.array([255.0, 235.0, 12.0])

  # Conversion to tristimulus values.
  XYZ = colour.sRGB_to_XYZ(RGB / 255)

  # Conversion to chromaticity coordinates.
  xy = colour.XYZ_to_xy(XYZ)

  # Conversion to correlated colour temperature in K.
  CCT = colour.xy_to_CCT(xy, 'hernandez1999')
  print(CCT)

  # 3557.10272422
like image 131
Kel Solaar Avatar answered Nov 09 '22 02:11

Kel Solaar


Here is a formula in Java,

  double n = ((0.23881) * Color.red(previewColor) + (0.25499) * Color.green(previewColor) + (-0.58291) * Color.blue(previewColor)) / ((0.11109) * Color.red(previewColor) + (-0.85406) * Color.green(previewColor) + (0.52289) * Color.blue(previewColor));
  double cct  = 449 * Math.pow(n,3) + 3525 * Math.pow(n,2) + 6823.3 * n + 5520.33;

here the value for previewColor is the integer representation of the color

like image 20
Hashir Labs Avatar answered Nov 09 '22 01:11

Hashir Labs