Created
February 3, 2021 13:52
-
-
Save MrLixm/fc45d3646028492666c37706b7c02df8 to your computer and use it in GitHub Desktop.
Convert Kelvin Temperatures to RGB values with given colorspace primaries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Convert Kelvin Temperatures to RGB values with given colorspace primaries. | |
Author: Liam Collod | |
Last Modified: 02/02/2021 | |
Require colour-science >= 0.3.16 | |
""" | |
import colour | |
import numpy | |
def blackbody_to_RGB(temperature, colorspace, illuminants=None, cctf_encoding=False, clip=True): | |
""" | |
Args: | |
clip(bool): If True value should be clipped between the 0-1 range | |
illuminants: if specified this iiluminant will be used instead of hte colorspace whitepoint. ex: 'E' | |
temperature(int): temperature in Kelvin to compute | |
colorspace(str): colorspace model, ex: 'sRGB' or 'ACEScg' | |
cctf_encoding(bool): apply the cttf encoding ? | |
Returns: | |
numpy.ndarray: 1D array | |
""" | |
csm = colour.RGB_COLOURSPACES[colorspace] | |
if illuminants: | |
illuminant_lab = colour.CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][illuminants] | |
else: | |
illuminant_lab = csm.whitepoint | |
XYZ = colour.sd_to_XYZ(colour.sd_blackbody(temperature), k=683) | |
RGB = colour.XYZ_to_RGB(XYZ, illuminant_lab, csm.whitepoint, csm.matrix_XYZ_to_RGB) | |
if cctf_encoding: | |
colour.cctf_encoding(RGB, colorspace) | |
if clip: | |
RGB = colour.utilities.normalise_maximum(RGB, clip=clip) | |
return RGB | |
def display_temp2rgb_range(min_temp, max_temp, increment, colorspace, clip): | |
""" Print the RGB values for the temparature in the given range. | |
Args: | |
min_temp(int): Kelvin temparature: beginning of range | |
max_temp(int): Kelvin temparature: end of range | |
increment(int): step of range | |
colorspace(str): colorspace model, ex: 'sRGB' or 'ACEScg' | |
Returns: | |
None | |
""" | |
print(f"RGB values with {user_colorspace} primaries for temperature in range [{min_temp}, {max_temp}] : \n") | |
for temp in range(min_temp, max_temp, increment): | |
rgb_result = blackbody_to_RGB(temp, colorspace=colorspace, cctf_encoding=False, clip=clip) | |
print(f"{temp}K: {rgb_result}") | |
return | |
if __name__ == '__main__': | |
user_temperature = 1500 | |
user_colorspace = 'ACEScg' | |
""" Bottom code is to display the above temperature to an RGB value with the given primaries above.""" | |
# result = blackbody_to_RGB(1500, 'sRGB', cctf_encoding=False, clip=True) | |
# print(f"RGB values with {user_colorspace} primaries for temperature {user_temperature} : \n" | |
# f" > {result}") | |
""" Bottom code is to display multiples temperature to RGB at the same time (just uncomment)""" | |
display_temp2rgb_range(min_temp=1000, max_temp=11500, increment=250, colorspace=user_colorspace, clip=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment