File: color_appearance_models.py

package info (click to toggle)
python-colormath 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 300 kB
  • sloc: python: 3,657; makefile: 3
file content (1279 lines) | stat: -rw-r--r-- 47,394 bytes parent folder | download | duplicates (3)
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
"""
Copyright (c) 2014, Michael Mauderer, University of St Andrews
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 * Neither the name of the University of St Andrews nor the names of its
   contributors may be used to endorse or promote products derived from this
   software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from __future__ import division

import logging
import numpy

logger = logging.getLogger(__name__)


class Nayatani95(object):
    """
    **References**

    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.
    * Nayatani, Y., Sobagaki, H., & Yano, K. H. T. (1995). Lightness dependency of chroma scales of a nonlinear
      color-appearance model and its latest formulation. *Color Research & Application*, 20(3), 156-167.

    """

    @property
    def hue_angle(self):
        """
        Predicted hue angle :math:`\\theta`.
        """
        return self._hue_angle

    @property
    def chroma(self):
        """
        Predicted chroma :math:`C`.
        """
        return self._chroma

    @property
    def saturation(self):
        """
        Predicted saturation :math:`S`.
        """
        return self._saturation

    @property
    def brightness(self):
        """
        Predicted brightness :math:`B_r`.
        """
        return self._brightness

    @property
    def colorfulness(self):
        """
        Predicted colorfulness :math:`M`.
        """
        return self._colorfulness

    def __init__(self, x, y, z, x_n, y_n, z_n, y_ob, e_o, e_or, n=1):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_n: X value of reference white :math:`X_n`.
        :param y_n: Y value of reference white :math:`Y_n`.
        :param z_n: Z value of reference white :math:`Z_n`.
        :param y_ob: Luminance factor of achromatic background as percentage :math:`Y_o`.
                     Required to be larger than 0.18.
        :param e_o: Illuminance of the viewing field :math:`E_o` in lux.
        :param e_or: Normalising illuminance :math:`E_or` in lux.
        :param n: Noise term :math:`n`.
        """

        if numpy.any(y_ob <= 0.18):
            raise ValueError('y_ob hast be greater than 0.18.')

        l_o = y_ob * e_o / (100 * numpy.pi)
        l_or = y_ob * e_or / (100 * numpy.pi)
        logger.debug('L_o: {}'.format(l_o))
        logger.debug('L_or: {}'.format(l_or))

        x_o = x_n / (x_n + y_n + z_n)
        y_o = y_n / (x_n + y_n + z_n)
        logger.debug('x_o: {}'.format(x_o))
        logger.debug('y_o: {}'.format(y_o))

        xi = (0.48105 * x_o + 0.78841 * y_o - 0.08081) / y_o
        eta = (-0.27200 * x_o + 1.11962 * y_o + 0.04570) / y_o
        zeta = (0.91822 * (1 - x_o - y_o)) / y_o
        logger.debug('xi: {}'.format(xi))
        logger.debug('eta: {}'.format(eta))
        logger.debug('zeta: {}'.format(zeta))

        r_0, g_0, b_0 = rgb_0 = ((y_ob * e_o) / (100 * numpy.pi)) * numpy.array([xi, eta, zeta])
        logger.debug('rgb_0: {}'.format(rgb_0))

        r, g, b, = rgb = self.xyz_to_rgb(numpy.array([x, y, z]))
        logger.debug('rgb: {}'.format(rgb))

        e_r = self._compute_scaling_coefficient(r, xi)
        logger.debug('e(R): {}'.format(e_r))
        e_g = self._compute_scaling_coefficient(g, eta)
        logger.debug('e(G): {}'.format(e_g))

        beta_r = self._beta_1(r_0)
        logger.debug('beta1(rho): {}'.format(beta_r))
        beta_g = self._beta_1(g_0)
        logger.debug('beta1(eta): {}'.format(beta_g))
        beta_b = self._beta_2(b_0)
        logger.debug('beta2(zeta): {}'.format(beta_b))

        beta_l = self._beta_1(l_or)
        logger.debug('beta1(L_or): {}'.format(beta_l))

        # Opponent Color Dimension
        self._achromatic_response = (2 / 3) * beta_r * e_r * numpy.log10((r + n) / (20 * xi + n))
        self._achromatic_response += (1 / 3) * beta_g * e_g * numpy.log10((g + n) / (20 * eta + n))
        self._achromatic_response *= 41.69 / beta_l
        logger.debug('Q: {}'.format(self._achromatic_response))

        self._tritanopic_response = (1 / 1) * beta_r * numpy.log10((r + n) / (20 * xi + n))
        self._tritanopic_response += - (12 / 11) * beta_g * numpy.log10((g + n) / (20 * eta + n))
        self._tritanopic_response += (1 / 11) * beta_b * numpy.log10((b + n) / (20 * zeta + n))
        logger.debug('t: {}'.format(self._tritanopic_response))

        self._protanopic_response = (1 / 9) * beta_r * numpy.log10((r + n) / (20 * xi + n))
        self._protanopic_response += (1 / 9) * beta_g * numpy.log10((g + n) / (20 * eta + n))
        self._protanopic_response += - (2 / 9) * beta_b * numpy.log10((b + n) / (20 * zeta + n))
        logger.debug('p: {}'.format(self._protanopic_response))

        # Brightness
        self._brightness = (50 / beta_l) * ((2 / 3) * beta_r + (1 / 3) * beta_g) + self._achromatic_response

        self._brightness_ideal_white = (2 / 3) * beta_r * 1.758 * numpy.log10((100 * xi + n) / (20 * xi + n))
        self._brightness_ideal_white += (1 / 3) * beta_g * 1.758 * numpy.log10((100 * eta + n) / (20 * eta + n))
        self._brightness_ideal_white *= 41.69 / beta_l
        self._brightness_ideal_white += (50 / beta_l) * (2 / 3) * beta_r
        self._brightness_ideal_white += (50 / beta_l) * (1 / 3) * beta_g

        # Lightness
        self._lightness_achromatic = self._achromatic_response + 50
        self._lightness_achromatic_normalized = 100 * (self._brightness / self._brightness_ideal_white)

        # Hue
        hue_angle_rad = numpy.arctan2(self._protanopic_response, self._tritanopic_response)
        self._hue_angle = ((360 * hue_angle_rad / (2 * numpy.pi)) + 360) % 360
        logger.debug('theta: {}'.format(self._hue_angle))

        e_s_theta = self.chromatic_strength(hue_angle_rad)
        logger.debug('E_s(theta): {}'.format(e_s_theta))

        # Saturation
        self._saturation_rg = (488.93 / beta_l) * e_s_theta * self._tritanopic_response
        self._saturation_yb = (488.93 / beta_l) * e_s_theta * self._protanopic_response
        logger.debug('S_RG: {}'.format(self._saturation_rg))
        logger.debug('S_YB: {}'.format(self._saturation_yb))

        self._saturation = numpy.sqrt((self._saturation_rg ** 2) + (self._saturation_yb ** 2))
        logger.debug('S: {}'.format(self._saturation))

        # Chroma
        self._chroma_rg = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation_rg
        self._chroma_yb = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation_yb
        self._chroma = ((self._lightness_achromatic / 50) ** 0.7) * self._saturation
        logger.debug('C: {}'.format(self._chroma))

        # Colorfulness
        self._colorfulness_rg = self._chroma_rg * self._brightness_ideal_white / 100
        self._colorfulness_yb = self._chroma_yb * self._brightness_ideal_white / 100
        self._colorfulness = self._chroma * self._brightness_ideal_white / 100

    @staticmethod
    def chromatic_strength(angle):
        result = 0.9394
        result += - 0.2478 * numpy.sin(1 * angle)
        result += - 0.0743 * numpy.sin(2 * angle)
        result += + 0.0666 * numpy.sin(3 * angle)
        result += - 0.0186 * numpy.sin(4 * angle)
        result += - 0.0055 * numpy.cos(1 * angle)
        result += - 0.0521 * numpy.cos(2 * angle)
        result += - 0.0573 * numpy.cos(3 * angle)
        result += - 0.0061 * numpy.cos(4 * angle)
        return result

    @staticmethod
    def _compute_scaling_coefficient(a, b):
        return numpy.where(a >= (20 * b), 1.758, 1)

    @staticmethod
    def _beta_1(x):
        return (6.469 + 6.362 * (x ** 0.4495)) / (6.469 + (x ** 0.4495))

    @staticmethod
    def _beta_2(x):
        return 0.7844 * (8.414 + 8.091 * (x ** 0.5128)) / (8.414 + (x ** 0.5128))

    xyz_to_rgb_m = numpy.array([[0.40024, 0.70760, -0.08081],
                                [-0.22630, 1.16532, 0.04570],
                                [0, 0, 0.91822]])

    @classmethod
    def xyz_to_rgb(cls, xyz):
        return cls.xyz_to_rgb_m.dot(xyz)


class Hunt(object):
    """
    **References**

    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.
    * Hunt, R. W. G. (2005). *The reproduction of colour*. 5th Ed., John Wiley & Sons.

    """

    @property
    def hue_angle(self):
        """
        Predicted hue angle :math:`h_s`.
        """
        return self._hue_angle

    @property
    def chroma(self):
        """
        Predicted chroma :math:`C_{94}`.
        """
        return self._chroma

    @property
    def saturation(self):
        """
        Predicted saturation :math:`s`.
        """
        return self._saturation

    @property
    def brightness(self):
        """
        Predicted brightness :math:`Q`.
        """
        return self._brightness

    @property
    def colorfulness(self):
        """
        Predicted colorfulness :math:`M_{94}`.
        """
        return self._colorfulness

    @property
    def lightness(self):
        """
        Predicted colorfulness :math:`J`.
        """
        return self._lightness

    def __init__(self, x, y, z,
                 x_b, y_b, z_b,
                 x_w, y_w, z_w,
                 l_a,
                 n_c,
                 n_b,
                 l_as=None,
                 cct_w=None,
                 n_cb=None,
                 n_bb=None,
                 x_p=None,
                 y_p=None,
                 z_p=None,
                 p=None,
                 helson_judd=False,
                 discount_illuminant=True,
                 s=None,
                 s_w=None):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_b: X value of background :math:`X_b`.
        :param y_b: Y value of background :math:`Y_b`.
        :param z_b: Z value of background :math:`Z_b`.
        :param x_w: X value of reference white :math:`X_W`.
        :param y_w: Y value of reference white :math:`Y_W`.
        :param z_w: Z value of reference white :math:`Z_W`.
        :param l_a: Adapting luminance :math:`L_A`.
        :param n_c: Chromatic surround induction_factor :math:`N_c`.
        :param n_b: Brightness surround induction factor :math:`N_b`.
        :param l_as: Scotopic luminance of the illuminant :math:`L_{AS}`.
                     Will be approximated if not supplied.
        :param cct_w: Correlated color temperature of illuminant :math:`T`.
                      Will be used to approximate l_as if not supplied.
        :param n_cb: Chromatic background induction factor :math:`N_{cb}`.
                     Will be approximated using y_w and y_b if not supplied.
        :param n_bb: Brightness background induction factor :math:`N_{bb}`.
                     Will be approximated using y_w and y_b if not supplied.
        :param x_p: X value of proxima field :math:`X_p`.
                    If not supplied, will be assumed to equal background.
        :param y_p: Y value of proxima field :math:`Y_p`.
                    If not supplied, will be assumed to equal background.
        :param z_p: Z value of proxima field :math:`Z_p`.
                    If not supplied, will be assumed to equal background.
        :param p: Simultaneous contrast/assimilation parameter.
        :param helson_judd: Truth value indicating whether the Heslon-Judd effect should be accounted for.
                            Default False.
        :param discount_illuminant: Truth value whether discount-the-illuminant should be applied. Default True.
        :param s: Scotopic response to the stimulus.
        :param s_w: Scotopic response for th reference white.
        :raises ValueError: if illegal parameter combination is supplied.
        """
        if x_p is None:
            x_p = x_b
            logger.warn('Approximated x_p with x_b.')
        if y_p is None:
            y_p = y_b
            logger.warn('Approximated y_p with y_b.')
        if z_p is None:
            z_p = y_b
            logger.warn('Approximated z_p with z_b.')

        if n_cb is None:
            n_cb = 0.725 * (y_w / y_b) ** 0.2
            logger.warn('Approximated n_cb.')
        logger.debug('N_cb: {}'.format(n_cb))
        if n_bb is None:
            n_bb = 0.725 * (y_w / y_b) ** 0.2
            logger.warn('Approximated n_bb.')
        logger.debug('N_bb: {}'.format(n_cb))

        if l_as is None:
            logger.warn('Approximated scotopic luminance.')
            if cct_w is None:
                cct_w = self._get_cct(x_w, y_w, z_w)
                logger.warn('Approximated cct_w: {}'.format(cct_w))
            l_as = 2.26 * l_a
            l_as *= ((cct_w / 4000) - 0.4) ** (1 / 3)
        logger.debug('LA_S: {}'.format(l_as))

        if s is None != s_w is None:
            raise ValueError("Either both scotopic responses (s, s_w) need to be supplied or none.")
        elif s is None and s_w is None:
            s = y
            s_w = y_w
            logger.warn('Approximated scotopic response to stimulus and reference white.')

        if p is None:
            logger.warn('p not supplied. Model will not account for simultaneous chromatic contrast .')

        xyz = numpy.array([x, y, z])
        logger.debug('XYZ: {}'.format(xyz))
        xyz_w = numpy.array([x_w, y_w, z_w])
        logger.debug('XYZ_W: {}'.format(xyz_w))
        xyz_b = numpy.array([x_b, y_b, z_b])
        xyz_p = numpy.array([x_p, y_p, z_p])

        k = 1 / (5 * l_a + 1)
        logger.debug('k: {}'.format(k))
        # luminance adaptation factor
        f_l = 0.2 * (k ** 4) * (5 * l_a) + 0.1 * ((1 - (k ** 4)) ** 2) * ((5 * l_a) ** (1 / 3))
        logger.debug('F_L: {}'.format(f_l))

        logger.debug('--- Stimulus RGB adaptation start ----')
        rgb_a = self._adaptation(f_l, l_a, xyz, xyz_w, xyz_b, xyz_p, p, helson_judd, discount_illuminant)
        logger.debug('--- Stimulus RGB adaptation end ----')
        r_a, g_a, b_a = rgb_a
        logger.debug('RGB_A: {}'.format(rgb_a))
        logger.debug('--- White RGB adaptation start ----')
        rgb_aw = self._adaptation(f_l, l_a, xyz_w, xyz_w, xyz_b, xyz_p, p, helson_judd, discount_illuminant)
        logger.debug('--- White RGB adaptation end ----')
        r_aw, g_aw, b_aw = rgb_aw
        logger.debug('RGB_AW: {}'.format(rgb_aw))

        # ---------------------------
        # Opponent Color Dimensions
        # ---------------------------

        # achromatic_cone_signal
        a_a = 2 * r_a + g_a + (1 / 20) * b_a - 3.05 + 1
        logger.debug('A_A: {}'.format(a_a))
        a_aw = 2 * r_aw + g_aw + (1 / 20) * b_aw - 3.05 + 1
        logger.debug('A_AW: {}'.format(a_aw))

        c1 = r_a - g_a
        logger.debug('C1: {}'.format(c1))
        c2 = g_a - b_a
        logger.debug('C2: {}'.format(c2))
        c3 = b_a - r_a
        logger.debug('C3: {}'.format(c3))

        c1_w = r_aw - g_aw
        logger.debug('C1_W: {}'.format(c1_w))
        c2_w = g_aw - b_aw
        logger.debug('C2_W: {}'.format(c2_w))
        c3_w = b_aw - r_aw
        logger.debug('C3_W: {}'.format(c3_w))

        # -----
        # Hue
        # -----
        self._hue_angle = (180 * numpy.arctan2(0.5 * (c2 - c3) / 4.5, c1 - (c2 / 11)) / numpy.pi) % 360
        hue_angle_w = (180 * numpy.arctan2(0.5 * (c2_w - c3_w) / 4.5, c1_w - (c2_w / 11)) / numpy.pi) % 360

        # -------------
        # Saturation
        # -------------
        e_s = self._calculate_eccentricity_factor(self.hue_angle)
        logger.debug('es: {}'.format(e_s))
        e_s_w = self._calculate_eccentricity_factor(hue_angle_w)

        f_t = l_a / (l_a + 0.1)
        logger.debug('F_t: {}'.format(f_t))
        m_yb = 100 * (0.5 * (c2 - c3) / 4.5) * (e_s * (10 / 13) * n_c * n_cb * f_t)
        logger.debug('m_yb: {}'.format(m_yb))
        m_rg = 100 * (c1 - (c2 / 11)) * (e_s * (10 / 13) * n_c * n_cb)
        logger.debug('m_rg: {}'.format(m_rg))
        m = ((m_rg ** 2) + (m_yb ** 2)) ** 0.5
        logger.debug('m: {}'.format(m))

        self._saturation = 50 * m / rgb_a.sum(axis=0)

        m_yb_w = 100 * (0.5 * (c2_w - c3_w) / 4.5) * (e_s_w * (10 / 13) * n_c * n_cb * f_t)
        m_rg_w = 100 * (c1_w - (c2_w / 11)) * (e_s_w * (10 / 13) * n_c * n_cb)
        m_w = ((m_rg_w ** 2) + (m_yb_w ** 2)) ** 0.5

        # ------------
        # Brightness
        # ------------
        logger.debug('--- Stimulus achromatic signal START ----')
        a = self._calculate_achromatic_signal(l_as, s, s_w, n_bb, a_a)
        logger.debug('--- Stimulus achromatic signal END ----')
        logger.debug('A: {}'.format(a))

        logger.debug('--- White achromatic signal START ----')
        a_w = self._calculate_achromatic_signal(l_as, s_w, s_w, n_bb, a_aw)
        logger.debug('--- White achromatic signal END ----')
        logger.debug('A_w: {}'.format(a_w))

        n1 = ((7 * a_w) ** 0.5) / (5.33 * n_b ** 0.13)
        n2 = (7 * a_w * n_b ** 0.362) / 200
        logger.debug('N1: {}'.format(n1))
        logger.debug('N2: {}'.format(n2))

        self._brightness = ((7 * (a + (m / 100))) ** 0.6) * n1 - n2
        brightness_w = ((7 * (a_w + (m_w / 100))) ** 0.6) * n1 - n2
        logger.debug('Q: {}'.format(self.brightness))
        logger.debug('Q_W: {}'.format(brightness_w))

        # ----------
        # Lightness
        # ----------
        z = 1 + (y_b / y_w) ** 0.5
        logger.debug('z: {}'.format(z))
        self._lightness = 100 * (self.brightness / brightness_w) ** z

        # -------
        # Chroma
        # -------
        self._chroma = 2.44 * (self.saturation ** 0.69) * ((self.brightness / brightness_w) ** (y_b / y_w)) * (
            1.64 - 0.29 ** (y_b / y_w))

        # -------------
        # Colorfulness
        # -------------
        self._colorfulness = (f_l ** 0.15) * self.chroma

    xyz_to_rgb_m = numpy.array([[0.38971, 0.68898, -0.07868],
                                [-0.22981, 1.18340, 0.04641],
                                [0, 0, 1]])

    @classmethod
    def xyz_to_rgb(cls, xyz):
        return cls.xyz_to_rgb_m.dot(xyz)

    def _adaptation(self, f_l, l_a, xyz, xyz_w, xyz_b, xyz_p=None, p=None, helson_judd=False, discount_illuminant=True):
        """
        :param f_l: Luminance adaptation factor
        :param l_a: Adapting luminance
        :param xyz: Stimulus color in XYZ
        :param xyz_w: Reference white color in XYZ
        :param xyz_b: Background color in XYZ
        :param xyz_p: Proxima field color in XYZ
        :param p: Simultaneous contrast/assimilation parameter.
        """
        rgb = self.xyz_to_rgb(xyz)
        logger.debug('RGB: {}'.format(rgb))
        rgb_w = self.xyz_to_rgb(xyz_w)
        logger.debug('RGB_W: {}'.format(rgb_w))
        y_w = xyz_w[1]
        y_b = xyz_b[1]

        h_rgb = 3 * rgb_w / (rgb_w.sum())
        logger.debug('H_RGB: {}'.format(h_rgb))

        # Chromatic adaptation factors
        if not discount_illuminant:
            f_rgb = (1 + (l_a ** (1 / 3)) + h_rgb) / (1 + (l_a ** (1 / 3)) + (1 / h_rgb))
        else:
            f_rgb = numpy.ones(numpy.shape(h_rgb))
        logger.debug('F_RGB: {}'.format(f_rgb))

        # Adaptation factor
        if helson_judd:
            d_rgb = self._f_n((y_b / y_w) * f_l * f_rgb[1]) - self._f_n((y_b / y_w) * f_l * f_rgb)
            assert d_rgb[1] == 0
        else:
            d_rgb = numpy.zeros(numpy.shape(f_rgb))
        logger.debug('D_RGB: {}'.format(d_rgb))

        # Cone bleaching factors
        rgb_b = (10 ** 7) / ((10 ** 7) + 5 * l_a * (rgb_w / 100))
        logger.debug('B_RGB: {}'.format(rgb_b))

        if xyz_p is not None and p is not None:
            logger.debug('Account for simultaneous chromatic contrast')
            rgb_p = self.xyz_to_rgb(xyz_p)
            rgb_w = self.adjust_white_for_scc(rgb_p, rgb_b, rgb_w, p)

        # Adapt rgb using modified
        rgb_a = 1 + rgb_b * (self._f_n(f_l * f_rgb * rgb / rgb_w) + d_rgb)
        logger.debug('RGB_A: {}'.format(rgb_a))

        return rgb_a

    @classmethod
    def adjust_white_for_scc(cls, rgb_p, rgb_b, rgb_w, p):
        """
        Adjust the white point for simultaneous chromatic contrast.

        :param rgb_p: Cone signals of proxima field.
        :param rgb_b: Cone signals of background.
        :param rgb_w: Cone signals of reference white.
        :param p: Simultaneous contrast/assimilation parameter.
        :return: Adjusted cone signals for reference white.
        """
        p_rgb = rgb_p / rgb_b
        rgb_w = rgb_w * (((1 - p) * p_rgb + (1 + p) / p_rgb) ** 0.5) / (((1 + p) * p_rgb + (1 - p) / p_rgb) ** 0.5)
        return rgb_w

    @staticmethod
    def _get_cct(x, y, z):
        """
        Reference
        Hernandez-Andres, J., Lee, R. L., & Romero, J. (1999).
        Calculating correlated color temperatures across the entire gamut of daylight and skylight chromaticities.
        Applied Optics, 38(27), 5703-5709.
        """
        x_e = 0.3320
        y_e = 0.1858

        n = ((x / (x + z + z)) - x_e) / ((y / (x + z + z)) - y_e)

        a_0 = -949.86315
        a_1 = 6253.80338
        a_2 = 28.70599
        a_3 = 0.00004

        t_1 = 0.92159
        t_2 = 0.20039
        t_3 = 0.07125

        cct = a_0 + a_1 * numpy.exp(-n / t_1) + a_2 * numpy.exp(-n / t_2) + a_3 * numpy.exp(-n / t_3)
        return cct

    @staticmethod
    def calculate_scotopic_luminance(photopic_luminance, color_temperature):
        return 2.26 * photopic_luminance * ((color_temperature / 4000) - 0.4) ** (1 / 3)

    @classmethod
    def _calculate_achromatic_signal(cls, l_as, s, s_w, n_bb, a_a):

        j = 0.00001 / ((5 * l_as / 2.26) + 0.00001)
        logger.debug('j: {}'.format(j))

        f_ls = 3800 * (j ** 2) * (5 * l_as / 2.26)
        f_ls += 0.2 * ((1 - (j ** 2)) ** 0.4) * ((5 * l_as / 2.26) ** (1 / 6))
        logger.debug('F_LS: {}'.format(f_ls))

        b_s = 0.5 / (1 + 0.3 * ((5 * l_as / 2.26) * (s / s_w)) ** 0.3)
        b_s += 0.5 / (1 + 5 * (5 * l_as / 2.26))
        logger.debug('B_S: {}'.format(b_s))

        a_s = (cls._f_n(f_ls * s / s_w) * 3.05 * b_s) + 0.3
        logger.debug('A_S: {}'.format(a_s))

        return n_bb * (a_a - 1 + a_s - 0.3 + numpy.sqrt((1 + (0.3 ** 2))))

    @staticmethod
    def _f_n(i):
        """
        Nonlinear response function.
        """
        return 40 * ((i ** 0.73) / (i ** 0.73 + 2))

    @staticmethod
    def _calculate_eccentricity_factor(hue_angle):
        h = numpy.array([20.14, 90, 164.25, 237.53])
        e = numpy.array([0.8, 0.7, 1.0, 1.2])

        out = numpy.interp(hue_angle, h, e)
        out = numpy.where(hue_angle < 20.14, 0.856 - (hue_angle / 20.14) * 0.056, out)
        out = numpy.where(hue_angle > 237.53, 0.856 + 0.344 * (360 - hue_angle) / (360 - 237.53), out)

        return out


class RLAB(object):
    """
    **References**

    * Fairchild, M. D. (1996). Refinement of the RLAB color space. *Color Research & Application*, 21(5), 338-346.
    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.

    """

    @property
    def hue_angle(self):
        """
        Predicted hue angle :math:`h^R`.
        """
        return self._hue_angle

    @property
    def chroma(self):
        """
        Predicted chroma :math:`C^R`.
        """
        return self._chroma

    @property
    def saturation(self):
        """
        Predicted saturation :math:`s^R`.
        """
        return self._saturation

    @property
    def lightness(self):
        """
        Predicted colorfulness :math:`L^R`.
        """
        return self._lightness

    @property
    def a(self):
        """
        Predicted red-green chromatic response :math:`a^R`.
        """
        return self._a

    @property
    def b(self):
        """
        Predicted yellow-blue chromatic response :math:`b^R`.
        """
        return self._b

    R = numpy.array([[1.9569, -1.1882, 0.2313],
                     [0.3612, 0.6388, 0],
                     [0, 0, 1]])

    def __init__(self, x, y, z, x_n, y_n, z_n, y_n_abs, sigma, d):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_n: X value of reference white :math:`X_n`.
        :param y_n: Y value of reference white :math:`Y_n`.
        :param z_n: Z value of reference white :math:`Z_n`.
        :param y_n_abs: Absolute luminance :math:`Y_n` of a white object in cd/m^2.
        :param sigma: Relative luminance parameter :math:`\sigma`. For average surround set :math:`\sigma=1/2.3`,
                      for dim surround :math:`\sigma=1/2.9` and for dark surround :math:`\sigma=1/3.5`.
        :param d: Degree of adaptation :math:`D`.
        """
        xyz = numpy.array([x, y, z])
        xyz_n = numpy.array([x_n, y_n, z_n])

        lms = Hunt.xyz_to_rgb(xyz)
        lms_n = Hunt.xyz_to_rgb(xyz_n)
        logger.debug('LMS: {}'.format(lms))
        logger.debug('LMS_n: {}'.format(lms_n))

        lms_e = (3 * lms_n) / (lms_n[0] + lms_n[1] + lms_n[2])
        lms_p = (1 + (y_n_abs ** (1 / 3)) + lms_e) / (
            1 + (y_n_abs ** (1 / 3)) + (1 / lms_e))
        logger.debug('LMS_e: {}'.format(lms_e))
        logger.debug('LMS_p: {}'.format(lms_p))

        lms_a = (lms_p + d * (1 - lms_p)) / lms_n
        logger.debug('LMS_a: {}'.format(lms_a))

        # If we want to allow arrays as input we need special handling here.
        if len(numpy.shape(x)) == 0:
            # Okay so just a number, we can do things by the book.
            a = numpy.diag(lms_a)
            logger.debug('A: {}'.format(a))
            xyz_ref = self.R.dot(a).dot(Hunt.xyz_to_rgb_m).dot(xyz)
        else:
            # So we have an array. Since constructing huge multidimensional
            # arrays might not bee the best idea, we will handle each input
            # dimension separately. First figure out how many values we have to
            # deal with.
            input_dim = len(x)
            # No create the ouput array that we will fill layer by layer
            xyz_ref = numpy.zeros((3, input_dim))
            for layer in range(input_dim):
                a = numpy.diag(lms_a[..., layer])
                logger.debug('A layer {}: {}'.format(layer, a))
                xyz_ref[..., layer] = self.R.dot(a).dot(Hunt.xyz_to_rgb_m).dot(xyz[..., layer])

        logger.debug('XYZ_ref: {}'.format(xyz_ref))
        x_ref, y_ref, z_ref = xyz_ref

        # Lightness
        self._lightness = 100 * (y_ref ** sigma)
        logger.debug('lightness: {}'.format(self.lightness))

        # Opponent Color Dimensions
        self._a = 430 * ((x_ref ** sigma) - (y_ref ** sigma))
        self._b = 170 * ((y_ref ** sigma) - (z_ref ** sigma))
        logger.debug('a: {}'.format(self._a))
        logger.debug('b: {}'.format(self._b))

        # Hue
        self._hue_angle = (360 * numpy.arctan2(self._b, self._a) / (2 * numpy.pi) + 360) % 360

        # Chroma
        self._chroma = numpy.sqrt((self._a ** 2) + (self._b ** 2))

        # Saturation
        self._saturation = self.chroma / self.lightness


class ATD95(object):
    """
    **References**


    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.
    * Guth, S. L. (1995, April). Further applications of the ATD model for color vision. In *IS&T/SPIE's Symposium
      on Electronic Imaging: Science & Technology* (pp. 12-26). International Society for Optics and Photonics.

    """

    @property
    def hue(self):
        """
        Predicted hue :math:`H`.
        """
        return self._hue

    @property
    def brightness(self):
        """
        Predicted brightness :math:`Br`.
        """
        return self._brightness

    @property
    def saturation(self):
        """
        Predicted saturation :math:`C`.
        """
        return self._saturation

    def __init__(self, x, y, z, x_0, y_0, z_0, y_0_abs, k_1, k_2, sigma=300):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_0: X value of reference white :math:`X_0`.
        :param y_0: Y value of reference white :math:`Y_0`.
        :param z_0: Z value of reference white :math:`Z_0`.
        :param y_0_abs: Absolute adapting luminance :math:`Y_0` in cd/m^2.
        :param k_1: :math:`k_1`
        :param k_2: :math:`k_2`
        :param sigma: :math:`\sigma`
        """
        xyz = self._scale_to_luminance(numpy.array([x, y, z]), y_0_abs)
        xyz_0 = self._scale_to_luminance(numpy.array([x_0, y_0, z_0]), y_0_abs)
        logger.debug('Scaled XYZ: {}'.format(xyz))
        logger.debug('Scaled XYZ_0: {}'.format(xyz))

        # Adaptation Model
        lms = self._xyz_to_lms(xyz)
        logger.debug('LMS: {}'.format(lms))

        xyz_a = k_1 * xyz + k_2 * xyz_0
        logger.debug('XYZ_a: {}'.format(xyz_a))

        lms_a = self._xyz_to_lms(xyz_a)
        logger.debug('LMS_a: {}'.format(lms_a))

        l_g, m_g, s_g = lms * (sigma / (sigma + lms_a))

        # Opponent Color Dimensions
        a_1i = 3.57 * l_g + 2.64 * m_g
        t_1i = 7.18 * l_g - 6.21 * m_g
        d_1i = -0.7 * l_g + 0.085 * m_g + s_g
        a_2i = 0.09 * a_1i
        t_2i = 0.43 * t_1i + 0.76 * d_1i
        d_2i = d_1i

        self._a_1 = self._calculate_final_response(a_1i)
        self._t_1 = self._calculate_final_response(t_1i)
        self._d_1 = self._calculate_final_response(d_1i)
        self._a_2 = self._calculate_final_response(a_2i)
        self._t_2 = self._calculate_final_response(t_2i)
        self._d_2 = self._calculate_final_response(d_2i)

        # Perceptual Correlates
        self._brightness = (self._a_1 ** 2 + self._t_1 ** 2 + self._d_1 ** 2) ** 0.5
        self._saturation = (self._t_2 ** 2 + self._d_2 ** 2) ** 0.5 / self._a_2
        self._hue = self._t_2 / self._d_2

    @staticmethod
    def _calculate_final_response(value):
        return value / (200 + abs(value))

    @staticmethod
    def _scale_to_luminance(xyz, absolute_adapting_luminance):
        return 18 * (absolute_adapting_luminance * xyz / 100) ** 0.8

    @staticmethod
    def _xyz_to_lms(xyz):
        x, y, z = xyz
        l = ((0.66 * (0.2435 * x + 0.8524 * y - 0.0516 * z)) ** 0.7) + 0.024
        m = ((-0.3954 * x + 1.1642 * y + 0.0837 * z) ** 0.7) + 0.036
        s = ((0.43 * (0.04 * y + 0.6225 * z)) ** 0.7) + 0.31
        return numpy.array([l, m, s])


class LLAB(object):
    """
    **References**

    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.
    * Luo, M. R., & Morovic, J. (1996, September). Two unsolved issues in colour management-colour appearance and
      gamut mapping. In *5th International Conference on High Technology* (pp. 136-147).
    * Luo, M. R., Lo, M. C., & Kuo, W. G. (1996). The LLAB (l: c) colour model.
      *Color Research & Application*, 21(6), 412-429.
    """

    @property
    def hue_angle(self):
        """
        Predicted hue angle :math:`h_L`.
        """
        return self._hue_angle

    @property
    def chroma(self):
        """
        Predicted chroma :math:`Ch_L`.
        """
        return self._chroma

    @property
    def saturation(self):
        """
        Predicted saturation :math:`s_L`.
        """
        return self._saturation

    @property
    def lightness(self):
        """
        Predicted colorfulness :math:`L_L`.
        """
        return self._lightness

    @property
    def a_l(self):
        """
        Predicted red-green chromatic response :math:`A_L`.
        """
        return self._a_l

    @property
    def b_l(self):
        """
        Predicted yellow-blue chromatic response :math:`B_L`.
        """
        return self._b_l

    def __init__(self, x, y, z, x_0, y_0, z_0, y_b, f_s, f_l, f_c, l, d=1):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_0: X value of reference white :math:`X_0`.
        :param y_0: Y value of reference white :math:`Y_0`.
        :param z_0: Z value of reference white :math:`Z_0`. 
        :param y_b: Luminance factor of the background :math:`Y_b` in cd/m^2.
        :param f_s: Surround induction factor :math:`F_S`.
        :param f_l: Lightness induction factor :math:`F_L`.
        :param f_c: Chroma induction factor :math:`F_C`.
        :param l: Absolute luminance of reference white :math:`L` in cd/m^2.
        :param d: Discounting-the-Illuminant factor :math:`D`.
        """
        xyz = numpy.array([x, y, z])
        logger.debug('XYZ: {}'.format([x, y, z]))
        xyz_0 = numpy.array([x_0, y_0, z_0])

        r, g, b = self.xyz_to_rgb(xyz)
        logger.debug('RGB: {}'.format([r, g, b]))
        r_0, g_0, b_0 = self.xyz_to_rgb(xyz_0)
        logger.debug('RGB_0: {}'.format([r_0, g_0, b_0]))

        xyz_0r = numpy.array([95.05, 100, 108.88])
        r_0r, g_0r, b_0r = self.xyz_to_rgb(xyz_0r)
        logger.debug('RGB_0r: {}'.format([r_0r, g_0r, b_0r]))

        beta = (b_0 / b_0r) ** 0.0834
        logger.debug('beta: {}'.format(beta))
        r_r = (d * (r_0r / r_0) + 1 - d) * r
        g_r = (d * (g_0r / g_0) + 1 - d) * g
        b_r = (d * (b_0r / (b_0 ** beta)) + 1 - d) * (abs(b) ** beta)
        logger.debug('RGB_r: {}'.format([r_r, g_r, b_r]))

        rgb_r = numpy.array([r_r, g_r, b_r])

        # m_inv = numpy.linalg.inv(self.xyz_to_rgb_m)
        m_inv = numpy.array([[0.987, -0.1471, 0.16],
                             [0.4323, 0.5184, 0.0493],
                             [-0.0085, 0.04, 0.9685]])
        x_r, y_r, z_r = m_inv.dot(rgb_r * y)
        logger.debug('XYZ_r: {}'.format([x_r, y_r, z_r]))

        # Opponent Color Dimension
        def f(w):
            return numpy.where(w > 0.008856,
                               w ** (1 / f_s),
                               (((0.008856 ** (1 / f_s)) - (16 / 116)) / 0.008856) * w + (16 / 116))

        # lightness_contrast_exponent
        z = 1 + f_l * ((y_b / 100) ** 0.5)
        logger.debug('z: {}'.format(z))

        self._lightness = 116 * (f(y_r / 100) ** z) - 16
        a = 500 * (f(x_r / 95.05) - f(y_r / 100))
        b = 200 * (f(y_r / 100) - f(z_r / 108.88))
        logger.debug('A: {}'.format(a))
        logger.debug('B: {}'.format(b))

        logger.debug('f(Xr): {}'.format(f(x_r / 95.05)))
        logger.debug('f(Yr): {}'.format(f(y_r / 100)))
        logger.debug('f(Zr): {}'.format(f(z_r / 108.88)))

        # Perceptual Correlates
        c = (a ** 2 + b ** 2) ** 0.5
        self._chroma = 25 * numpy.log(1 + 0.05 * c)

        s_c = 1 + 0.47 * numpy.log10(l) - 0.057 * numpy.log10(l) ** 2
        s_m = 0.7 + 0.02 * self._lightness - 0.0002 * self._lightness ** 2
        c_l = self._chroma * s_m * s_c * f_c

        self._saturation = self._chroma / self._lightness

        hue_angle_rad = numpy.arctan2(b, a)
        self._hue_angle = hue_angle_rad * 360 / (2 * numpy.pi) % 360

        self._a_l = c_l * numpy.cos(hue_angle_rad)
        self._b_l = c_l * numpy.sin(hue_angle_rad)

    xyz_to_rgb_m = numpy.array([[0.8951, 0.2664, -0.1614],
                                [-0.7502, 1.7135, 0.0367],
                                [0.0389, -0.0685, 1.0296]])

    @classmethod
    def xyz_to_rgb(cls, xyz):
        return cls.xyz_to_rgb_m.dot(xyz / xyz[1])


class CIECAM02(object):
    """
    **References**

    * CIE TC 8-01 (2004). A Color appearance model for color management systems.
      Publication 159. Vienna: CIE Central Bureau. ISBN 3-901906-29-0.
    * Fairchild, M. D. (2013). *Color appearance models*, 3rd Ed. John Wiley & Sons.
    """

    @property
    def hue_angle(self):
        """
        Predicted hue angle :math:`h`.
        """
        return self._h

    @property
    def chroma(self):
        """
        Predicted chroma :math:`C`.
        """
        return self._chroma

    @property
    def saturation(self):
        """
        Predicted saturation :math:`s_L`.
        """
        return self._saturation

    @property
    def lightness(self):
        """
        Predicted colorfulness :math:`J`.
        """
        return self._lightness

    @property
    def brightness(self):
        """
        Predicted colorfulness :math:`Q`.
        """
        return self._brightness

    @property
    def colorfulness(self):
        """
        Predicted colorfulness :math:`M`.
        """
        return self._colorfulness

    @property
    def a(self):
        """
        Predicted red-green chromatic response :math:`a`.
        """
        return self._a

    @property
    def b(self):
        """
        Predicted yellow-blue chromatic response :math:`b`.
        """
        return self._b

    M_CAT02 = numpy.array([[0.7328, 0.4296, -0.1624],
                           [-0.7036, 1.6975, 0.0061],
                           [0.0030, 0.0136, 0.9834]])

    M_CAT02_inv = numpy.linalg.inv(M_CAT02)

    M_HPE = numpy.array([[0.38971, 0.68898, -0.07868],
                         [-0.22981, 1.18340, 0.04641],
                         [0, 0, 1]])

    def __init__(self, x, y, z, x_w, y_w, z_w, y_b, l_a, c, n_c, f, d=False):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_w: X value of reference white :math:`X_W`.
        :param y_w: Y value of reference white :math:`Y_W`.
        :param z_w: Z value of reference white :math:`Z_W`.
        :param y_b: Background relative luminance :math:`Y_b`.
        :param l_a: Adapting luminance :math:`L_A` in cd/m^2.
        :param c: Exponential nonlinearity :math:`c`. (Average/Dim/Dark) (0.69/0.59/0.525).
        :param n_c: Chromatic induction factor :math:`N_c`. (Average/Dim/Dark) (1.0,0.9,0.8).
        :param f: Maximum degree of adaptation :math:`F`. (Average/Dim/Dark) (1.0/0.9/0.8).
        :param d: Discount-the-Illuminant factor :math:`D`.
        """

        xyz = numpy.array([x, y, z])
        xyz_w = numpy.array([x_w, y_w, z_w])

        # Determine the degree of adaptation
        if not d:
            d = self._compute_degree_of_adaptation(f, l_a)
        else:
            d = 1
        logger.debug("D: {}".format(d))

        # Compute viewing condition dependant components
        k = 1 / (5 * l_a + 1)
        logger.debug("k: {}".format(k))

        f_l = 0.2 * (k ** 4) * 5 * l_a + 0.1 * (1 - k ** 4) ** 2 * (5 * l_a) ** (1 / 3)
        logger.debug("F_L: {}".format(f_l))
        n = y_b / y_w
        logger.debug("n: {}".format(n))
        self.n_bb = self.n_cb = 0.725 * n ** -0.2
        z = 1.48 + numpy.sqrt(n)
        logger.debug("z".format(z))

        rgb_a, rgb_aw = self._compute_adaptation(xyz, xyz_w, f_l, d)
        logger.debug("RGB'a: {}".format(rgb_a))
        logger.debug("RGB'aw: {}".format(rgb_aw))

        r_a, g_a, b_a = rgb_a
        r_aw, g_aw, b_aw = rgb_aw

        # Opponent Color Dimensions
        self._a = r_a - 12 * g_a / 11 + b_a / 11
        self._b = (1 / 9) * (r_a + g_a - 2 * b_a)

        # Hue
        self._h = 360 * numpy.arctan2(self._b, self._a) / (2 * numpy.pi)
        e_t = (1 / 4) * (numpy.cos(2 + self._h * numpy.pi / 180) + 3.8)

        # Lightness
        a = self._compute_achromatic_response(r_a, g_a, b_a, self.n_bb)
        logger.debug('A: {}'.format(a))
        a_w = self._compute_achromatic_response(r_aw, g_aw, b_aw, self.n_bb)
        logger.debug('A_W: {}'.format(a_w))
        self._lightness = 100 * (a / a_w) ** (c * z)  # 16.24

        # Brightness
        # self._brightness = self.compute_brightness(self.lightness, surround, a_w, f_l)
        self._brightness = (4 / c) * numpy.sqrt(self._lightness / 100) * (a_w + 4) * f_l ** 0.25

        # Chroma
        # self.chroma = self.compute_chroma(rgb_a, self.lightness, surround, self.N_cb, e_t, self.a, self.b, n)
        t = ((50000 / 13) * n_c * self.n_cb * e_t * numpy.sqrt((self._a ** 2) + (self._b ** 2))) / (
            rgb_a[0] + rgb_a[1] + (21 / 20) * rgb_a[2])
        self._chroma = (t ** 0.9) * numpy.sqrt(self._lightness / 100) * ((1.64 - 0.29 ** n) ** 0.73)

        # Colorfulness
        self._colorfulness = self.chroma * f_l ** 0.25

        # Saturation
        self._saturation = 100 * numpy.sqrt(self._colorfulness / self._brightness)

        # Cartesian coordinates
        self.a_c, self.b_c = self._compute_cartesian_coordinates(self.chroma, self._h)
        self.a_m, self.b_m = self._compute_cartesian_coordinates(self._colorfulness, self._h)
        self.a_s, self.b_s = self._compute_cartesian_coordinates(self.saturation, self._h)

    @classmethod
    def _compute_adaptation(cls, xyz, xyz_w, f_l, d):
        # Transform input colors to cone responses
        rgb = cls._xyz_to_rgb(xyz)
        logger.debug("RGB: {}".format(rgb))
        rgb_w = cls._xyz_to_rgb(xyz_w)
        logger.debug("RGB_W: {}".format(rgb_w))

        # Compute adapted tristimulus-responses
        rgb_c = cls._white_adaption(rgb, rgb_w, d)
        logger.debug("RGB_C: {}".format(rgb_c))
        rgb_cw = cls._white_adaption(rgb_w, rgb_w, d)
        logger.debug("RGB_CW: {}".format(rgb_cw))

        # Convert adapted tristimulus-responses to Hunt-Pointer-Estevez fundamentals
        rgb_p = cls._compute_hunt_pointer_estevez_fundamentals(rgb_c)
        logger.debug("RGB': {}".format(rgb_p))
        rgb_wp = cls._compute_hunt_pointer_estevez_fundamentals(rgb_cw)
        logger.debug("RGB'_W: {}".format(rgb_wp))

        # Compute post-adaptation non-linearities
        rgb_ap = cls._compute_nonlinearities(f_l, rgb_p)
        rgb_awp = cls._compute_nonlinearities(f_l, rgb_wp)

        return rgb_ap, rgb_awp

    @staticmethod
    def _xyz_to_rgb(xyz):
        return numpy.dot(CIECAM02.M_CAT02, xyz)

    @staticmethod
    def _rgb_to_xyz(rgb):
        return numpy.dot(CIECAM02.M_CAT02_inv, rgb)

    @staticmethod
    def _white_adaption(rgb, rgb_w, d=1):
        return ((100 * d / rgb_w) + (1 - d)) * rgb

    @staticmethod
    def _compute_degree_of_adaptation(surround_conditions, adapting_luminance):
        return surround_conditions * (1 - (1 / 3.6) * numpy.exp((-adapting_luminance - 42) / 92))

    @staticmethod
    def _compute_hunt_pointer_estevez_fundamentals(rgb):
        return numpy.dot(numpy.dot(CIECAM02.M_HPE, CIECAM02.M_CAT02_inv), rgb)

    @staticmethod
    def _compute_nonlinearities(f_l, rgb):
        return 0.1 + (400 * (f_l * rgb / 100) ** 0.42) / (27.13 + (f_l * rgb / 100) ** 0.42)

    @staticmethod
    def _compute_achromatic_response(r, g, b, n_bb):
        return (2 * r + g + (1 / 20) * b - 0.305) * n_bb

    @staticmethod
    def _compute_cartesian_coordinates(value, hue):
        a = value * numpy.cos(hue * numpy.pi / 180)  # 16.30
        b = value * numpy.sin(hue * numpy.pi / 180)  # 16.31
        return a, b


class CIECAM02m1(CIECAM02):
    """
    **References**

    * Wu, R. C., & Wardman, R. H. (2007). Proposed modification to the CIECAM02 colour appearance model to include the
      simultaneous contrast effects. *Color Research & Application*, 32(2), 121-129.
    """
    def __init__(self, x, y, z, x_w, y_w, z_w, x_b, y_b, z_b, l_a, c, n_c, f, p, d=False):
        """
        :param x: X value of test sample :math:`X`.
        :param y: Y value of test sample :math:`Y`.
        :param z: Z value of test sample :math:`Z`.
        :param x_w: X value of reference white :math:`X_W`.
        :param y_w: Y value of reference white :math:`Y_W`.
        :param z_w: Z value of reference white :math:`Z_W`.
        :param x_b: X value of background :math:`X_b`.
        :param y_b: Y value of background :math:`Y_b`.
        :param z_b: Z value of background :math:`Z_b`.
        :param l_a: Adapting luminance :math:`L_A` in cd/m^2.
        :param c: Exponential nonlinearity :math:`c`. (Average/Dim/Dark) (0.69/0.59/5.25).
        :param n_c: Chromatic induction factor :math:`N_c`. (Average/Dim/Dark) (1.0,0.9,0.8).
        :param f: Maximum degree of adaptation :math:`F`. (Average/Dim/Dark) (1.0/0.9/0.8).
        :param p: Simultaneous contrast/assimilation parameter.
        :param d: Discount-the-Illuminant factor :math:`D`.
        """

        self._p = p
        self._xyz_b = numpy.array([x_b, y_b, z_b])

        super(CIECAM02m1, self).__init__(x, y, z, x_w, y_w, z_w, y_b, l_a, c, n_c, f, d)

    def _compute_adaptation(self, xyz, xyz_w, f_l, d):
        """
        Modified adaptation procedure incorporating simultaneous chromatic contrast from Hunt model.

        :param xyz: Stimulus XYZ.
        :param xyz_w: Reference white XYZ.
        :param f_l: Luminance adaptation factor
        :param d: Degree of adaptation.
        :return: Tuple of adapted rgb and rgb_w arrays.
        """
        # Transform input colors to cone responses
        rgb = self._xyz_to_rgb(xyz)
        logger.debug("RGB: {}".format(rgb))

        rgb_b = self._xyz_to_rgb(self._xyz_b)
        rgb_w = self._xyz_to_rgb(xyz_w)
        rgb_w = Hunt.adjust_white_for_scc(rgb, rgb_b, rgb_w, self._p)
        logger.debug("RGB_W: {}".format(rgb_w))

        # Compute adapted tristimulus-responses
        rgb_c = self._white_adaption(rgb, rgb_w, d)
        logger.debug("RGB_C: {}".format(rgb_c))
        rgb_cw = self._white_adaption(rgb_w, rgb_w, d)
        logger.debug("RGB_CW: {}".format(rgb_cw))

        # Convert adapted tristimulus-responses to Hunt-Pointer-Estevez fundamentals
        rgb_p = self._compute_hunt_pointer_estevez_fundamentals(rgb_c)
        logger.debug("RGB': {}".format(rgb_p))
        rgb_wp = self._compute_hunt_pointer_estevez_fundamentals(rgb_cw)
        logger.debug("RGB'_W: {}".format(rgb_wp))

        # Compute post-adaptation non-linearities
        rgb_ap = self._compute_nonlinearities(f_l, rgb_p)
        rgb_awp = self._compute_nonlinearities(f_l, rgb_wp)

        return rgb_ap, rgb_awp