% matplotlib inline
import numpy as np
from copy import deepcopy
import colour
from colour.characterisation.dataset.colour_checkers.spds import (
COLOURCHECKER_INDEXES_TO_NAMES_MAPPING)
from colour.plotting import *
from colour.models.dataset.aces import AP1
CMFS = colour.CMFS['CIE 1931 2 Degree Standard Observer']
SHAPE = CMFS.shape
COLOUR_CHECKER = 'SG'
if COLOUR_CHECKER == 'Classic':
COLOUR_CHECKER = deepcopy(
colour.COLOURCHECKERS_SPDS['ColorChecker N Ohta'])
COLOUR_CHECKER_MAPPING = COLOURCHECKER_INDEXES_TO_NAMES_MAPPING
elif COLOUR_CHECKER == 'SG':
xrite_spds = colour.read_spds_from_xrite_file(
'resources/others/Digital_ColorChecker_SG.txt')
xrite_spds_names = [xrite_spd.name for xrite_spd in xrite_spds.values()]
xrite_spds_indexes = range(len(xrite_spds_names))
COLOUR_CHECKER = dict(zip(xrite_spds_names, xrite_spds.values()))
COLOUR_CHECKER_MAPPING = dict(zip(xrite_spds_indexes, xrite_spds_names))
[spd.align(SHAPE) for spd in COLOUR_CHECKER.values()]
def get_colour_checker_spds(colour_checker=COLOUR_CHECKER,
mapping=COLOUR_CHECKER_MAPPING):
spds = []
for index, sample in sorted(mapping.items()):
spds.append(colour_checker[sample].clone())
return spds
def spectral_to_XYZ(spds, colourspace):
return [colour.spectral_to_XYZ(
spd,
illuminant=(
colour.ILLUMINANTS_RELATIVE_SPDS[
colourspace.illuminant])) / 100
for spd in spds]
def spectral_to_RGB(spds, colourspace):
XYZ = spectral_to_XYZ(spds, colourspace)
return [colour.XYZ_to_RGB(
x,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.XYZ_to_RGB_matrix) for x in XYZ]
def RGB_to_RGB(RGB, colourspace_i, colourspace_o):
return [colour.RGB_to_RGB(
x,
colourspace_i,
colourspace_o) for x in RGB]
def RGB_to_Lab(RGB, colourspace):
Lab = [colour.XYZ_to_Lab(
colour.RGB_to_XYZ(x,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.RGB_to_XYZ_matrix),
colourspace.whitepoint) for x in RGB]
return Lab
def delta_E_CIE2000(Lab1, Lab2):
return [colour.delta_E_CIE2000(Lab1[i], Lab2[i])
for i in range(len(Lab1))]
def aces_ap1_rec_2020():
# Defining some reference *sRGB* colourspace *RGB* colours from a colour rendition
# chart.
sRGB_RGB = spectral_to_RGB(get_colour_checker_spds(),
colour.sRGB_COLOURSPACE)
# Converting the reference *RGB* colours to *ACEScg* and *Rec. 2020* colourspaces.
aces_cg_RGB = RGB_to_RGB(sRGB_RGB,
colour.sRGB_COLOURSPACE,
colour.ACES_CG_COLOURSPACE)
rec_2020_RGB = RGB_to_RGB(sRGB_RGB,
colour.sRGB_COLOURSPACE,
colour.REC_2020_COLOURSPACE)
# Converting the *ACEScg* and *Rec. 2020* *RGB* colours to *sRGB* colourspace while
# assuming both are *Rec. 2020* *RGB* colours in order to get the visual discrepancies
# between the two primaries sets.
aces_cg_sRGB = RGB_to_RGB(aces_cg_RGB,
colour.REC_2020_COLOURSPACE,
colour.sRGB_COLOURSPACE)
rec_2020_sRGB = RGB_to_RGB(rec_2020_RGB,
colour.REC_2020_COLOURSPACE,
colour.sRGB_COLOURSPACE)
# Computing the *CIE Lab* values for both sets of colours.
aces_cg_Lab = RGB_to_Lab(aces_cg_sRGB,
colour.sRGB_COLOURSPACE)
rec_2020_Lab = RGB_to_Lab(rec_2020_sRGB,
colour.sRGB_COLOURSPACE)
colour_parameters = []
for k, v in enumerate(COLOUR_CHECKER_MAPPING.items()):
delta_E = colour.delta_E_CIE2000(aces_cg_Lab[k], rec_2020_Lab[k])
colour_parameters.append(
colour_parameter('{0}\n\nACEScg'.format(v[1]),
np.clip(aces_cg_sRGB[k], 0, 1)))
colour_parameters.append(
colour_parameter('Delta E:\n{0:f}\n\nRec. 2020'.format(delta_E),
np.clip(rec_2020_sRGB[k], 0, 1)))
multi_colour_plot(colour_parameters, across=2, figure_size=(256, 256))
aces_ap1_rec_2020()
# Discarding chromatic adaptation between *ACEScg* and *Rec. 2020* colourspaces
# by using the same illuminant / whitepoint which is the same as the reference
# *sRGB* colourspace.
colour.ACES_CG_COLOURSPACE.illuminant = colour.REC_2020_COLOURSPACE.illuminant
colour.ACES_CG_COLOURSPACE.RGB_to_XYZ_matrix = colour.normalised_primary_matrix(
AP1, colour.REC_2020_COLOURSPACE.whitepoint)
colour.ACES_CG_COLOURSPACE.XYZ_to_RGB_matrix = np.linalg.inv(
colour.ACES_CG_COLOURSPACE.RGB_to_XYZ_matrix)
aces_ap1_rec_2020()