This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
colour_analysis.py
835 lines (677 loc) · 25.3 KB
/
colour_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# -*- coding: utf-8 -*-
"""
Colour - Analysis
=================
Defines various objects that typically output the geometry as JSON to be
loaded by "Three.js".
"""
import json
import numpy as np
import os
import re
from cachelib import SimpleCache
from colour import (CCS_ILLUMINANTS, CCTF_DECODINGS, Lab_to_XYZ, LCHab_to_Lab,
RGB_COLOURSPACES, RGB_to_RGB, RGB_to_XYZ, XYZ_to_RGB,
XYZ_to_JzAzBz, XYZ_to_OSA_UCS, convert,
is_within_pointer_gamut, read_image)
from colour.geometry import primitive_cube
from colour.models import (CCS_ILLUMINANT_POINTER_GAMUT,
DATA_POINTER_GAMUT_VOLUME, linear_function)
from colour.plotting import filter_cmfs, filter_RGB_colourspaces
from colour.utilities import (as_float_array, first_item, normalise_maximum,
tsplit, tstack)
from colour.volume import XYZ_outer_surface
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2018-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '[email protected]'
__status__ = 'Production'
__all__ = [
'LINEAR_FILE_FORMATS', 'DTYPE_POSITION', 'DTYPE_COLOUR',
'COLOURSPACE_MODELS', 'COLOURSPACE_MODEL_LABELS', 'PRIMARY_COLOURSPACE',
'SECONDARY_COLOURSPACE', 'IMAGE_COLOURSPACE', 'IMAGE_CCTF_DECODING',
'COLOURSPACE_MODEL', 'IMAGE_CACHE', 'load_image',
'XYZ_to_colourspace_model', 'colourspace_model_axis_reorder',
'colourspace_model_faces_reorder', 'cctf_decodings', 'colourspace_models',
'RGB_colourspaces', 'buffer_geometry', 'conform_primitive_dtype',
'image_data', 'RGB_colourspace_volume_visual', 'spectral_locus_visual',
'RGB_image_scatter_visual', 'pointer_gamut_visual',
'visible_spectrum_visual'
]
LINEAR_FILE_FORMATS = ('.exr', '.hdr')
"""
Assumed linear image formats.
LINEAR_IMAGE_FORMATS : tuple
"""
DTYPE_POSITION = np.sctypeDict.get(
os.environ.get('COLOUR_SCIENCE__COLOUR_ANALYSIS_DTYPE_POSITION',
'float32'))
"""
Default floating point number dtype for visual data except colour. "float32" is
usually chosen over "float16" or "float64" as a good compromise between
precision and data size.
DTYPE_POSITION : type
"""
DTYPE_COLOUR = np.sctypeDict.get(
os.environ.get('COLOUR_SCIENCE__COLOUR_ANALYSIS_DTYPE_COLOUR', 'float16'))
"""
Default floating point number dtype for visual colour and image data. "float16"
is usually chosen over "float32" and "float64" because it is lighter and thus
more adapted to send data from the server to client.
DTYPE_COLOUR : type
"""
COLOURSPACE_MODELS = ('CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD',
'CAM16SCD', 'CAM16UCS', 'CIE XYZ', 'CIE xyY', 'CIE Lab',
'CIE Luv', 'CIE UCS', 'CIE UVW', 'DIN 99', 'Hunter Lab',
'Hunter Rdab', 'ICTCP', 'IGPGTG', 'IPT', 'JzAzBz',
'OSA UCS', 'hdr-CIELAB', 'hdr-IPT')
"""
Reference colourspace models defining available colour transformations from
CIE XYZ tristimulus values.
COLOURSPACE_MODELS : tuple
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD', 'CAM16UCS',
'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW', 'DIN 99',
'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG', 'IPT', 'JzAzBz', 'OSA UCS',
'hdr-CIELAB', 'hdr-IPT'}**
"""
COLOURSPACE_MODEL_LABELS = {
'CAM02LCD': ('M', 'J', 'h'),
'CAM02SCD': ('M', 'J', 'h'),
'CAM02UCS': ('M', 'J', 'h'),
'CAM16LCD': ('M', 'J', 'h'),
'CAM16SCD': ('M', 'J', 'h'),
'CAM16UCS': ('M', 'J', 'h'),
'CIE XYZ': ('X', 'Y', 'Z'),
'CIE xyY': ('x', 'Y', 'y'),
'CIE Lab': ('a*', 'L*', 'b*'),
'CIE Luv': ('u*', 'L*', 'v*'),
'CIE UCS': ('U', 'W', 'V'),
'CIE UVW': ('U*', 'W*', 'V*'),
'DIN 99': ('a99', 'L99', 'b99'),
'Hunter Lab': ('a', 'L', 'b'),
'Hunter Rdab': ('a', 'Rd', 'b'),
'ICTCP': ('CT', 'I', 'CP'),
'IGPGTG': ('PG', 'IG', 'TG'),
'IPT': ('P', 'I', 'T'),
'JzAzBz': ('Az', 'Jz', 'Bz'),
'OSA UCS': ('j', 'J', 'g'),
'hdr-CIELAB': ('a hdr', 'L hdr', 'b hdr'),
'hdr-IPT': ('P hdr', 'I hdr', 'T hdr')
}
"""
Reference colourspace models axes labels, ordered so that luminance is on *Y*
axis.
COLOURSPACE_MODELS : dict
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD', 'CAM16UCS',
'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW', 'DIN 99',
'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG', 'IPT', 'JzAzBz', 'OSA UCS',
'hdr-CIELAB', 'hdr-IPT'}**
"""
CCTF_DECODINGS.update({
'Linear': linear_function,
})
PRIMARY_COLOURSPACE = 'sRGB'
"""
Primary analysis RGB colourspace.
PRIMARY_COLOURSPACE : unicode
"""
SECONDARY_COLOURSPACE = 'DCI-P3'
"""
Secondary analysis RGB colourspace.
SECONDARY_COLOURSPACE : unicode
"""
IMAGE_COLOURSPACE = 'Primary'
"""
Analysed image RGB colourspace either *Primary* or *Secondary*.
IMAGE_COLOURSPACE : unicode
"""
IMAGE_CCTF_DECODING = 'sRGB'
"""
Analysed image RGB colourspace decoding colour component transfer function.
IMAGE_CCTF_DECODING : unicode
"""
COLOURSPACE_MODEL = 'CIE xyY'
"""
Analysis colour model.
COLOURSPACE_MODEL : unicode
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD', 'CAM16UCS',
'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS', 'CIE UVW', 'DIN 99',
'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG', 'IPT', 'JzAzBz', 'OSA UCS',
'hdr-CIELAB', 'hdr-IPT'}**
"""
DATA_POINTER_GAMUT = Lab_to_XYZ(
LCHab_to_Lab(DATA_POINTER_GAMUT_VOLUME), CCS_ILLUMINANT_POINTER_GAMUT)
"""
Pointer's Gamut data converted to *CIE XYZ* tristimulus values.
DATA_POINTER_GAMUT : ndarray
"""
IMAGE_CACHE = SimpleCache(default_timeout=60 * 24 * 7)
"""
Server side cache for images.
IMAGE_CACHE : SimpleCache
"""
def load_image(path, decoding_cctf='sRGB'):
"""
Loads the image at given path and caches it in `IMAGE_CACHE` cache. If the
image is already cached, it is returned directly.
Parameters
----------
path : unicode
Image path.
decoding_cctf : unicode, optional
Decoding colour component transfer function (Decoding CCTF) /
electro-optical transfer function (EOTF / EOCF) that maps an
:math:`R'G'B'` video component signal value to tristimulus values at
the display.
Returns
-------
ndarray
Image as a ndarray.
"""
is_linear_image = os.path.splitext(path)[-1].lower() in LINEAR_FILE_FORMATS
key = path if is_linear_image else '{0}-{1}'.format(path, decoding_cctf)
RGB = IMAGE_CACHE.get(key)
if RGB is None:
RGB = read_image(path)
if not is_linear_image:
RGB = CCTF_DECODINGS[decoding_cctf](RGB)
IMAGE_CACHE.set(key, RGB)
return RGB
def XYZ_to_colourspace_model(XYZ, illuminant, model, **kwargs):
"""
Converts from *CIE XYZ* tristimulus values to given colourspace model while
normalising for visual convenience some of the models.
Parameters
----------
XYZ : array_like
*CIE XYZ* tristimulus values.
illuminant : array_like
*CIE XYZ* tristimulus values *illuminant* *xy* chromaticity
coordinates.
model : unicode
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD',
'CAM16UCS', 'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS',
'CIE UVW', 'DIN 99', 'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG',
'IPT', 'JzAzBz', 'OSA UCS', 'hdr-CIELAB', 'hdr-IPT'}**,
Colourspace model to convert the *CIE XYZ* tristimulus values to.
Other Parameters
----------------
\\**kwargs : dict, optional
Keywords arguments.
Returns
-------
ndarray
Colourspace model values.
"""
ijk = convert(
XYZ,
'CIE XYZ',
model,
illuminant=illuminant,
verbose={'mode': 'Short'},
**kwargs)
if model == 'JzAzBz':
ijk /= XYZ_to_JzAzBz([1, 1, 1])[0]
elif model == 'OSA UCS':
ijk /= XYZ_to_OSA_UCS([1, 1, 1])[0]
return ijk
def colourspace_model_axis_reorder(a, model=None):
"""
Reorder the axes of given colourspace model :math:`a` array so that
luminance is on *Y* axis.
Parameters
----------
a : array_like
Colourspace model :math:`a` array.
model : unicode, optional
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD',
'CAM16UCS', 'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS',
'CIE UVW', 'DIN 99', 'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG',
'IPT', 'JzAzBz', 'OSA UCS', 'hdr-CIELAB', 'hdr-IPT'}**,
Colourspace model.
Returns
-------
ndarray
Reordered colourspace model :math:`a` array.
"""
i, j, k = tsplit(a)
if model in ('CIE XYZ', ):
a = tstack([k, j, i])
elif model in ('CIE UCS', 'CIE UVW', 'CIE xyY'):
a = tstack([j, k, i])
elif model in ('CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD',
'CAM16UCS', 'CIE Lab', 'CIE LCHab', 'CIE Luv', 'CIE LCHuv',
'DIN 99', 'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG',
'IPT', 'JzAzBz', 'OSA UCS', 'hdr-CIELAB', 'hdr-IPT'):
a = tstack([k, i, j])
return a
def colourspace_model_faces_reorder(a, model=None):
"""
Reorder the faces of given colourspace model :math:`a` array.
Parameters
----------
a : array_like
Colourspace model :math:`a` array.
model : unicode, optional
**{'CAM02LCD', 'CAM02SCD', 'CAM02UCS', 'CAM16LCD', 'CAM16SCD',
'CAM16UCS', 'CIE XYZ', 'CIE xyY', 'CIE Lab', 'CIE Luv', 'CIE UCS',
'CIE UVW', 'DIN 99', 'Hunter Lab', 'Hunter Rdab', 'ICTCP', 'IGPGTG',
'IPT', 'JzAzBz', 'OSA UCS', 'hdr-CIELAB', 'hdr-IPT'}**,
Colourspace model.
Returns
-------
Figure
Reordered colourspace model :math:`a` array.
"""
if model in ('CIE XYZ', ):
a = a[::-1]
return a
def cctf_decodings():
"""
Returns the decoding colour component transfer functions formatted as
*JSON*.
Returns
-------
unicode
Decoding colour component transfer functions formatted as *JSON*.
"""
return json.dumps(list(CCTF_DECODINGS.keys()))
def colourspace_models():
"""
Returns the colourspace models formatted as *JSON*.
Returns
-------
unicode
Colourspace models formatted as *JSON*.
"""
return json.dumps(COLOURSPACE_MODEL_LABELS)
def RGB_colourspaces():
"""
Returns the RGB colourspaces formatted as *JSON*.
Returns
-------
unicode
RGB colourspaces formatted as *JSON*.
"""
return json.dumps(list(RGB_COLOURSPACES.keys()))
def buffer_geometry(**kwargs):
"""
Returns given geometry formatted as *JSON* compatible with *Three.js*
`BufferGeometryLoader <https://threejs.org/docs/#api/loaders/\
BufferGeometryLoader>`__.
Other Parameters
----------------
\\**kwargs : dict, optional
Valid attributes from `BufferGeometryLoader <https://threejs.org/docs/\
#api/loaders/BufferGeometryLoader>`__.
Returns
-------
unicode
Geometry formatted as *JSON*.
"""
data = {
'metadata': {
'version': 4,
'type': 'BufferGeometry',
'generator': 'colour-three'
},
'data': {
'attributes': {}
}
}
data_types_conversion = {
'float16': 'Float32Array', # Unsupported, casted up.
'float32': 'Float32Array',
'float64': 'Float32Array', # Unsupported, casted down.
'uint16': 'Uint16Array',
'uint32': 'Uint32Array',
'uint64': 'Uint32Array', # Unsupported, casted down.
}
for attribute, values in kwargs.items():
values = np.asarray(values)
shape = values.shape
dtype = values.dtype.name
values = np.ravel(values)
if 'float' in dtype:
dtype = (DTYPE_COLOUR if attribute == 'color' else DTYPE_POSITION)
values = np.around(values, np.finfo(dtype).precision)
values = np.nan_to_num(values)
dtype = np.dtype(dtype).name
data['data']['attributes'][attribute] = {
'itemSize': shape[-1],
'type': data_types_conversion[dtype],
'array': values.tolist()
}
return json.dumps(data)
def conform_primitive_dtype(primitive):
"""
Conform the given primitive to the required dtype.
Parameters
----------
primitive : array_like
Primitive to conform to the required dtype.
Returns
-------
tuple
Conformed primitive.
"""
vertices, faces, outline = primitive
return (
vertices.astype(
[('position', np.float32, (3, )), ('uv', np.float32, (2, )),
('normal', np.float32, (3, )), ('colour', np.float32, (4, ))]),
faces.astype(np.uint32),
outline.astype(np.uint32),
)
def image_data(path,
primary_colourspace=PRIMARY_COLOURSPACE,
secondary_colourspace=SECONDARY_COLOURSPACE,
image_colourspace=IMAGE_COLOURSPACE,
image_decoding_cctf=IMAGE_CCTF_DECODING,
out_of_primary_colourspace_gamut=False,
out_of_secondary_colourspace_gamut=False,
out_of_pointer_gamut=False,
saturate=False):
"""
Returns given image RGB data or its out of gamut values formatted as
*JSON*.
Parameters
----------
path : unicode
Server side path of the image to read.
primary_colourspace : unicode, optional
Primary RGB colourspace used to generate out of gamut values.
secondary_colourspace: unicode, optional
Secondary RGB colourspace used to generate out of gamut values.
image_colourspace: unicode, optional
**{'Primary', 'Secondary'}**,
Analysed image RGB colourspace.
image_decoding_cctf : unicode, optional
Analysed image decoding colour component transfer function
(Decoding CCTF) / electro-optical transfer function (EOTF / EOCF) that
maps an :math:`R'G'B'` video component signal value to tristimulus
values at the display.
out_of_primary_colourspace_gamut : bool, optional
Whether to only generate the out of primary RGB colourspace gamut
values.
out_of_secondary_colourspace_gamut : bool, optional
Whether to only generate the out of secondary RGB colourspace gamut
values.
out_of_pointer_gamut : bool, optional
Whether to only generate the out of *Pointer's Gamut* values.
saturate : bool, optional
Whether to clip the image in domain [0, 1].
Returns
-------
unicode
RGB image data or its out of gamut values formatted as *JSON*.
"""
primary_colourspace = first_item(
filter_RGB_colourspaces(re.escape(primary_colourspace)).values())
secondary_colourspace = first_item(
filter_RGB_colourspaces(re.escape(secondary_colourspace)).values())
colourspace = (primary_colourspace if image_colourspace == 'Primary' else
secondary_colourspace)
RGB = load_image(path, image_decoding_cctf)
if saturate:
RGB = np.clip(RGB, 0, 1)
if out_of_primary_colourspace_gamut:
if image_colourspace == 'Secondary':
RGB = RGB_to_RGB(RGB, secondary_colourspace, primary_colourspace)
RGB[np.logical_and(RGB >= 0, RGB <= 1)] = 0
RGB[RGB != 0] = 1
RGB[np.any(RGB == 1, axis=-1)] = 1
if out_of_secondary_colourspace_gamut:
if image_colourspace == 'Primary':
RGB = RGB_to_RGB(RGB, primary_colourspace, secondary_colourspace)
RGB[np.logical_and(RGB >= 0, RGB <= 1)] = 0
RGB[RGB != 0] = 1
RGB[np.any(RGB == 1, axis=-1)] = 1
if out_of_pointer_gamut:
O_PG = is_within_pointer_gamut(
RGB_to_XYZ(
RGB,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.matrix_RGB_to_XYZ,
)).astype(np.int_)
O_PG = 1 - O_PG
RGB[O_PG != 1] = 0
RGB[O_PG == 1] = 1
shape = RGB.shape
RGB = np.ravel(RGB[..., 0:3].reshape(-1, 3))
RGB = np.around(RGB, np.finfo(DTYPE_COLOUR).precision)
return json.dumps({
'width': shape[1],
'height': shape[0],
'data': RGB.tolist()
})
def RGB_colourspace_volume_visual(colourspace=PRIMARY_COLOURSPACE,
colourspace_model=COLOURSPACE_MODEL,
segments=16,
wireframe=False):
"""
Returns a RGB colourspace volume visual geometry formatted as *JSON*.
Parameters
----------
colourspace : unicode, optional
RGB colourspace used to generate the visual geometry.
colourspace_model : unicode, optional
Colourspace model used to generate the visual geometry.
segments : int, optional
Segments count per side of the *box* used to generate the visual
geometry.
wireframe : bool, optional
Whether the visual geometry must represent a wireframe visual.
Returns
-------
unicode
RGB colourspace volume visual geometry formatted as *JSON*.
"""
colourspace = first_item(
filter_RGB_colourspaces(re.escape(colourspace)).values())
cube = conform_primitive_dtype(
primitive_cube(
width_segments=segments,
height_segments=segments,
depth_segments=segments))
vertices = cube[0]['position'] + 0.5
faces = colourspace_model_faces_reorder(
np.reshape(cube[1], (-1, 1)), colourspace_model)
RGB = cube[0]['colour']
XYZ = RGB_to_XYZ(
vertices,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.matrix_RGB_to_XYZ,
)
vertices = colourspace_model_axis_reorder(
XYZ_to_colourspace_model(
XYZ,
colourspace.whitepoint,
colourspace_model,
), colourspace_model)
return buffer_geometry(position=vertices, color=RGB, index=faces)
def RGB_image_scatter_visual(path,
primary_colourspace=PRIMARY_COLOURSPACE,
secondary_colourspace=SECONDARY_COLOURSPACE,
image_colourspace=IMAGE_COLOURSPACE,
image_decoding_cctf=IMAGE_CCTF_DECODING,
colourspace_model=COLOURSPACE_MODEL,
out_of_primary_colourspace_gamut=False,
out_of_secondary_colourspace_gamut=False,
out_of_pointer_gamut=False,
sub_sampling=25,
saturate=False):
"""
Returns a RGB image scatter visual geometry formatted as *JSON* for
given image.
Parameters
----------
path : unicode
Server side path of the image to read to generate the scatter points.
primary_colourspace : unicode, optional
Primary RGB colourspace used to generate the visual geometry.
secondary_colourspace: unicode, optional
Secondary RGB colourspace used to generate the visual geometry.
image_colourspace: unicode, optional
**{'Primary', 'Secondary'}**,
Analysed image RGB colourspace.
image_decoding_cctf : unicode, optional
Analysed image decoding colour component transfer function
(Decoding CCTF) / electro-optical transfer function (EOTF / EOCF) that
maps an :math:`R'G'B'` video component signal value to tristimulus
values at the display.
colourspace_model : unicode, optional
Colourspace model used to generate the visual geometry.
out_of_primary_colourspace_gamut : bool, optional
Whether to only generate the out of primary RGB colourspace gamut
visual geometry.
out_of_secondary_colourspace_gamut : bool, optional
Whether to only generate the out of secondary RGB colourspace gamut
visual geometry.
out_of_pointer_gamut : bool, optional
Whether to only generate the out of *Pointer's Gamut* visual geometry.
sub_sampling : int, optional
Consider every ``sub_sampling`` pixels of the image to generate the
visual geometry. Using a low number will yield a large quantity of
points, e.g. *1* yields *2073600* points for a *1080p* image.
saturate : bool, optional
Whether to clip the image in domain [0, 1].
Returns
-------
unicode
RGB image scatter visual geometry formatted as *JSON*.
"""
primary_colourspace = first_item(
filter_RGB_colourspaces(re.escape(primary_colourspace)).values())
secondary_colourspace = first_item(
filter_RGB_colourspaces(re.escape(secondary_colourspace)).values())
colourspace = (primary_colourspace if image_colourspace == 'Primary' else
secondary_colourspace)
RGB = load_image(path, image_decoding_cctf)
if saturate:
RGB = np.clip(RGB, 0, 1)
RGB = RGB[..., 0:3].reshape(-1, 3)[::sub_sampling]
if out_of_primary_colourspace_gamut:
RGB_c = np.copy(RGB)
if image_colourspace == 'Secondary':
RGB_c = RGB_to_RGB(RGB, secondary_colourspace, primary_colourspace)
RGB = RGB[np.any(np.logical_or(RGB_c < 0, RGB_c > 1), axis=-1)]
if out_of_secondary_colourspace_gamut:
RGB_c = np.copy(RGB)
if image_colourspace == 'Primary':
RGB_c = RGB_to_RGB(RGB, primary_colourspace, secondary_colourspace)
RGB = RGB[np.any(np.logical_or(RGB_c < 0, RGB_c > 1), axis=-1)]
if out_of_pointer_gamut:
O_PG = is_within_pointer_gamut(
RGB_to_XYZ(
RGB,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.matrix_RGB_to_XYZ,
)).astype(np.int_)
O_PG = 1 - O_PG
RGB = RGB[O_PG == 1]
XYZ = RGB_to_XYZ(
RGB,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.matrix_RGB_to_XYZ,
)
vertices = colourspace_model_axis_reorder(
XYZ_to_colourspace_model(
XYZ,
colourspace.whitepoint,
colourspace_model,
), colourspace_model)
if (out_of_primary_colourspace_gamut or
out_of_secondary_colourspace_gamut or out_of_pointer_gamut):
RGB = np.ones(RGB.shape)
return buffer_geometry(position=vertices, color=RGB)
def spectral_locus_visual(colourspace=PRIMARY_COLOURSPACE,
colourspace_model=COLOURSPACE_MODEL,
cmfs='CIE 1931 2 Degree Standard Observer'):
"""
Returns the spectral locus visual geometry formatted as *JSON*.
Parameters
----------
colourspace : unicode, optional
RGB colourspace used to generate the visual geometry.
colourspace_model : unicode, optional
Colourspace model used to generate the visual geometry.
cmfs : unicode, optional
Standard observer colour matching functions used to draw the spectral
locus.
Returns
-------
unicode
Spectral locus visual geometry formatted as *JSON*.
"""
colourspace = first_item(
filter_RGB_colourspaces(re.escape(colourspace)).values())
cmfs = first_item(filter_cmfs(cmfs).values())
XYZ = cmfs.values
XYZ = np.vstack([XYZ, XYZ[0, ...]])
vertices = colourspace_model_axis_reorder(
XYZ_to_colourspace_model(
XYZ,
colourspace.whitepoint,
colourspace_model,
), colourspace_model)
RGB = normalise_maximum(
XYZ_to_RGB(
XYZ,
colourspace.whitepoint,
colourspace.whitepoint,
colourspace.matrix_XYZ_to_RGB,
),
axis=-1)
return buffer_geometry(position=vertices, color=RGB)
def pointer_gamut_visual(colourspace_model='CIE xyY'):
"""
Returns the *Pointer's Gamut* visual geometry formatted as *JSON*.
Parameters
----------
colourspace_model : unicode, optional
Colourspace model used to generate the visual geometry.
Returns
-------
unicode
*Pointer's Gamut* visual geometry formatted as *JSON*.
"""
data_pointer_gamut = np.reshape(DATA_POINTER_GAMUT, (16, -1, 3))
vertices = []
for i in range(16):
section = colourspace_model_axis_reorder(
XYZ_to_colourspace_model(
np.vstack(
[data_pointer_gamut[i], data_pointer_gamut[i][0, ...]]),
CCS_ILLUMINANT_POINTER_GAMUT,
colourspace_model,
), colourspace_model)
vertices.append(list(zip(section, section[1:])))
vertices = as_float_array(vertices)
return buffer_geometry(position=vertices)
def visible_spectrum_visual(colourspace_model='CIE xyY'):
"""
Returns the visible spectrum visual geometry formatted as *JSON*.
Parameters
----------
colourspace_model : unicode, optional
Colourspace model used to generate the visual geometry.
Returns
-------
unicode
Visible spectrum visual geometry formatted as *JSON*.
"""
XYZ = XYZ_outer_surface()
vertices = colourspace_model_axis_reorder(
XYZ_to_colourspace_model(
XYZ,
CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['E'],
colourspace_model,
), colourspace_model)
vertices = as_float_array(list(zip(vertices, vertices[1:])))
return buffer_geometry(position=vertices)