Skip to content

Instantly share code, notes, and snippets.

@error454
Last active May 14, 2020 22:17
Show Gist options
  • Save error454/6b94c46d1f7512ffe5ee to your computer and use it in GitHub Desktop.
Save error454/6b94c46d1f7512ffe5ee to your computer and use it in GitHub Desktop.
Convert RGB to CIE Color Space
import math
# This is based on original code from http://stackoverflow.com/a/22649803
def EnhanceColor(normalized):
if normalized > 0.04045:
return math.pow( (normalized + 0.055) / (1.0 + 0.055), 2.4)
else:
return normalized / 12.92
def RGBtoXY(r, g, b):
rNorm = r / 255.0
gNorm = g / 255.0
bNorm = b / 255.0
rFinal = EnhanceColor(rNorm)
gFinal = EnhanceColor(gNorm)
bFinal = EnhanceColor(bNorm)
X = rFinal * 0.649926 + gFinal * 0.103455 + bFinal * 0.197109
Y = rFinal * 0.234327 + gFinal * 0.743075 + bFinal * 0.022598
Z = rFinal * 0.000000 + gFinal * 0.053077 + bFinal * 1.035763
if X + Y + Z == 0:
return (0,0)
else:
xFinal = X / (X + Y + Z)
yFinal = Y / (X + Y + Z)
return (xFinal, yFinal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment