Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit fcb4637

Browse files
committed
Normalises some colourspace models.
1 parent 5f1dade commit fcb4637

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

colour_analysis.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from colour import (Lab_to_XYZ, LCHab_to_Lab, LOG_DECODING_CURVES,
2020
POINTER_GAMUT_DATA, POINTER_GAMUT_ILLUMINANT,
2121
OETFS_REVERSE, RGB_COLOURSPACES, RGB_to_RGB, RGB_to_XYZ,
22-
XYZ_to_RGB, read_image)
22+
XYZ_to_RGB, XYZ_to_JzAzBz, XYZ_to_OSA_UCS, read_image)
2323
from colour.models import (XYZ_to_colourspace_model, function_gamma,
2424
function_linear)
2525
from colour.plotting import filter_cmfs, filter_RGB_colourspaces
@@ -38,7 +38,7 @@
3838
'COLOURSPACE_MODELS', 'COLOURSPACE_MODELS_LABELS', 'DECODING_CCTFS',
3939
'PRIMARY_COLOURSPACE', 'SECONDARY_COLOURSPACE', 'IMAGE_COLOURSPACE',
4040
'IMAGE_DECODING_CCTF', 'COLOURSPACE_MODEL', 'IMAGE_CACHE', 'load_image',
41-
'colourspace_model_axis_reorder', 'colourspace_model_faces_reorder',
41+
'XYZ_to_colourspace_model_normalised','colourspace_model_axis_reorder', 'colourspace_model_faces_reorder',
4242
'decoding_cctfs', 'colourspace_models', 'RGB_colourspaces',
4343
'buffer_geometry', 'create_plane', 'create_box', 'image_data',
4444
'RGB_colourspace_volume_visual', 'spectral_locus_visual',
@@ -230,6 +230,46 @@ def load_image(path, decoding_cctf='sRGB'):
230230
return RGB
231231

232232

233+
def XYZ_to_colourspace_model_normalised(XYZ, illuminant, model, **kwargs):
234+
"""
235+
Converts from *CIE XYZ* tristimulus values to given colourspace model while
236+
normalising for visual convenience some of the models.
237+
238+
Parameters
239+
----------
240+
XYZ : array_like
241+
*CIE XYZ* tristimulus values.
242+
illuminant : array_like
243+
*CIE XYZ* tristimulus values *illuminant* *xy* chromaticity
244+
coordinates.
245+
model : unicode
246+
**{'CIE XYZ', 'CIE xyY', 'CIE xy', 'CIE Lab', 'CIE LCHab', 'CIE Luv',
247+
'CIE Luv uv', 'CIE LCHuv', 'CIE UCS', 'CIE UCS uv', 'CIE UVW',
248+
'DIN 99', 'Hunter Lab', 'Hunter Rdab', 'IPT', 'JzAzBz, 'OSA UCS',
249+
'hdr-CIELAB', 'hdr-IPT'}**,
250+
Colourspace model to convert the *CIE XYZ* tristimulus values to.
251+
252+
Other Parameters
253+
----------------
254+
\**kwargs : dict, optional
255+
Keywords arguments.
256+
257+
Returns
258+
-------
259+
ndarray
260+
Colourspace model values.
261+
"""
262+
263+
ijk = XYZ_to_colourspace_model(XYZ, illuminant, model, **kwargs)
264+
265+
if model == 'JzAzBz':
266+
ijk /= XYZ_to_JzAzBz([1, 1, 1])[0]
267+
if model == 'OSA UCS':
268+
ijk /= XYZ_to_OSA_UCS([1, 1, 1])[0]
269+
270+
return ijk
271+
272+
233273
def colourspace_model_axis_reorder(a, model=None):
234274
"""
235275
Reorder the axes of given colourspace model :math:`a` array so that
@@ -723,8 +763,8 @@ def RGB_colourspace_volume_visual(colourspace=PRIMARY_COLOURSPACE,
723763
XYZ = RGB_to_XYZ(vertices, colourspace.whitepoint, colourspace.whitepoint,
724764
colourspace.RGB_to_XYZ_matrix)
725765
vertices = colourspace_model_axis_reorder(
726-
XYZ_to_colourspace_model(XYZ, colourspace.whitepoint,
727-
colourspace_model), colourspace_model)
766+
XYZ_to_colourspace_model_normalised(
767+
XYZ, colourspace.whitepoint, colourspace_model), colourspace_model)
728768

729769
return buffer_geometry(position=vertices, color=RGB, index=faces)
730770

@@ -816,8 +856,8 @@ def RGB_image_scatter_visual(path,
816856
XYZ = RGB_to_XYZ(RGB, colourspace.whitepoint, colourspace.whitepoint,
817857
colourspace.RGB_to_XYZ_matrix)
818858
vertices = colourspace_model_axis_reorder(
819-
XYZ_to_colourspace_model(XYZ, colourspace.whitepoint,
820-
colourspace_model), colourspace_model)
859+
XYZ_to_colourspace_model_normalised(
860+
XYZ, colourspace.whitepoint, colourspace_model), colourspace_model)
821861

822862
if out_of_primary_colourspace_gamut or out_of_secondary_colourspace_gamut:
823863
RGB = np.ones(RGB.shape)
@@ -856,8 +896,8 @@ def spectral_locus_visual(colourspace=PRIMARY_COLOURSPACE,
856896
XYZ = np.vstack((XYZ, XYZ[0, ...]))
857897

858898
vertices = colourspace_model_axis_reorder(
859-
XYZ_to_colourspace_model(XYZ, colourspace.whitepoint,
860-
colourspace_model), colourspace_model)
899+
XYZ_to_colourspace_model_normalised(
900+
XYZ, colourspace.whitepoint, colourspace_model), colourspace_model)
861901

862902
RGB = normalise_maximum(
863903
XYZ_to_RGB(XYZ, colourspace.whitepoint, colourspace.whitepoint,
@@ -886,7 +926,7 @@ def pointer_gamut_visual(colourspace_model='CIE xyY'):
886926
vertices = []
887927
for i in range(16):
888928
section = colourspace_model_axis_reorder(
889-
XYZ_to_colourspace_model(
929+
XYZ_to_colourspace_model_normalised(
890930
np.vstack((pointer_gamut_data[i],
891931
pointer_gamut_data[i][0, ...])),
892932
POINTER_GAMUT_ILLUMINANT, colourspace_model),

0 commit comments

Comments
 (0)