Skip to content

Instantly share code, notes, and snippets.

@Chandler
Last active November 28, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chandler/1ca5a55430ddecd8f50d83ebb1a46bdf to your computer and use it in GitHub Desktop.
Save Chandler/1ca5a55430ddecd8f50d83ebb1a46bdf to your computer and use it in GitHub Desktop.
import numpy
XYZ_to_sRGB_matrix = \
numpy.array(
[[3.2404542, -1.5371385, -0.4985314],
[-0.9692660, 1.8760108, 0.0415560],
[0.0556434, -0.2040259, 1.0572252]])
def apply_srgb_gamma(RGB):
non_linear = []
for v in RGB:
if v <= 0.0031308:
non_linear.append(v * 12.92)
else:
non_linear.append(1.055 * math.pow(v, 1 / 2.4) - 0.055)
return non_linear
def XYZ_to_sRGB(XYZ, illuminant_XYZ):
# first adapt the XYZ values to the sRGB Illuminant
# find this method in colour-science.org
adaptation_matrix = \
chromatic_adaptation_matrix_VonKries(source_illuminant, D65_illuminant)
XYZ_adapted = numpy.dot(adaptation_matrix, XYZ)
# do the linear conversion into sRGB space
sRGB_linear = numpy.dot(XYZ_to_sRGB_matrix, XYZ_adapted)
# apply the non-linearity function
sRGB = np.array(apply_srgb_gamma(sRGB_linear))
return sRGB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment