import numpy as np
import PyOpenColorIO as ocio
from pprint import pprint
import colour
from colour import warning
from colour.plotting import *
OCIO_CONFIGURATION = '/colour-science/OpenColorIO-Configs/aces_1.0.0/config.ocio'
DEFAULT_COLOURSPACES = (
'ACES2065-1',
'ACEScg',
'Linear - ARRI Wide Gamut',
'Linear - Adobe RGB',
'Linear - Adobe Wide Gamut RGB',
'Linear - Canon Cinema Gamut Daylight',
'Linear - Canon Cinema Gamut Tungsten',
'Linear - Canon DCI-P3 Daylight',
'Linear - Canon DCI-P3 Tungsten',
'Linear - Canon Rec. 709 Daylight',
'Linear - Canon Rec. 709 Tungsten',
'Linear - DRAGONcolor',
'Linear - DRAGONcolor2',
'Linear - P3-D60',
'Linear - P3-DCI',
'Linear - Protune Native - Experimental',
'Linear - REDcolor',
'Linear - REDcolor2',
'Linear - REDcolor3',
'Linear - REDcolor4',
'Linear - RIMM ROMM (ProPhoto)',
'Linear - Rec.2020',
'Linear - Rec.709',
'Linear - S-Gamut',
'Linear - S-Gamut Daylight',
'Linear - S-Gamut Tungsten',
'Linear - S-Gamut3',
'Linear - S-Gamut3.Cine',
'Linear - V-Gamut')
def RGB_to_RGB_matrix_to_normalised_primary_matrix(RGB_to_RGB_matrix,
XYZ_to_RGB_matrix):
M = np.einsum('...ij,...jk->...ik', XYZ_to_RGB_matrix, np.identity(3))
M = np.einsum('...ij,...jk->...ik', RGB_to_RGB_matrix, M)
M = np.linalg.inv(M)
return M
def colourspace_derivation(name, RGB_to_RGB_matrix):
NPM = RGB_to_RGB_matrix_to_normalised_primary_matrix(
np.linalg.inv(RGB_to_RGB_matrix),
colour.RGB_COLOURSPACES['ACES2065-1'].XYZ_to_RGB_matrix)
P, W = colour.primaries_whitepoint(NPM)
colourspace = colour.RGB_Colourspace(
name,
primaries=P,
whitepoint=W,
illuminant='D60',
RGB_to_XYZ_matrix=NPM,
XYZ_to_RGB_matrix=np.linalg.inv(NPM))
return colourspace
def ocio_configuration_plot(path, colourspaces, single_plot=False, **kwargs):
configuration = ocio.Config.CreateFromFile(path)
colourspaces_c = configuration.getColorSpaces()
colourspaces_c = dict(
(colourspace.getName(), colourspace)
for colourspace in colourspaces_c
if colourspace.getName() in colourspaces)
candidates = []
for name, colourspace in sorted(colourspaces_c.items()):
transform = colourspace.getTransform(
ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
if not isinstance(transform, ocio.MatrixTransform):
warning('"{0}" colourspace has no transform matrix!'.format(name))
continue
direction = transform.getDirection()
if direction != 'forward':
warning(('"{0}" transform direction is currently '
'not supported!').format(direction))
continue
offset = np.asarray(transform.getOffset())
if np.any(offset):
warning('Transform offset is currently not supported!')
continue
matrix = np.asarray(transform.getMatrix())
if np.sqrt(matrix.shape) == 4:
matrix = np.reshape(matrix, (4, 4))[0:3, 0:3]
else:
matrix = np.reshape(matrix, (3, 3))
colour.RGB_COLOURSPACES[name] = colourspace_derivation(name, matrix)
if not single_plot:
RGB_colourspaces_CIE_1976_UCS_chromaticity_diagram_plot(
['Pointer Gamut', 'ACES2065-1', name], **kwargs)
else:
candidates.append(name)
if single_plot:
RGB_colourspaces_CIE_1976_UCS_chromaticity_diagram_plot(
['Pointer Gamut', 'ACES2065-1'] + candidates, **kwargs)
ocio_configuration_plot(OCIO_CONFIGURATION,
('Linear - ARRI Wide Gamut',
'Linear - V-Gamut',
'ACEScg'),
title='ACES OCIO Configuration',
colour_cycle_count=7,
bounding_box=(-0.1, 0.7, -0.8, 0.7))