1
+ ######################
2
+ #Colour Math Library
3
+ ######################
4
+
5
+
6
+ ######################
7
+ #Constants
8
+ ######################
9
+ #Reference Whites
10
+
11
+
12
+ ######################
13
+ #CIE 1931 Colour Space
14
+ ######################
15
+ #RGB to Tristim
16
+
17
+ #Tristim to x y
18
+ def TriTOxy (TriX ,TriY ,TriZ ):
19
+
20
+ if (not isinstance (TriX ,(int ,float ))):
21
+ raise TypeError ("Tristim value must be float or int" )
22
+
23
+ if (not isinstance (TriY ,(int ,float ))):
24
+ raise TypeError ("Tristim value must be float or int" )
25
+
26
+ if (not isinstance (TriZ ,(int ,float ))):
27
+ raise TypeError ("Tristim value must be float or int" )
28
+
29
+ xCoord = (TriX / (TriX + TriY + TriZ ))
30
+ yCoord = (TriY / (TriX + TriY + TriZ ))
31
+ return [xCoord ,yCoord ]
32
+
33
+ #Tristim to x
34
+ def TriTOx (TriX ,TriY ,TriZ ):
35
+
36
+ if (not isinstance (TriX ,(int ,float ))):
37
+ raise TypeError ("Tristim value must be float or int" )
38
+
39
+ if (not isinstance (TriY ,(int ,float ))):
40
+ raise TypeError ("Tristim value must be float or int" )
41
+
42
+ if (not isinstance (TriZ ,(int ,float ))):
43
+ raise TypeError ("Tristim value must be float or int" )
44
+
45
+ xCoord = (TriX / (TriX + TriY + TriZ ))
46
+ return xCoord
47
+
48
+ #Tristim to y
49
+ def TriTOy (TriX ,TriY ,TriZ ):
50
+
51
+ if (not isinstance (TriX ,(int ,float ))):
52
+ raise TypeError ("Tristim value must be float or int" )
53
+
54
+ if (not isinstance (TriY ,(int ,float ))):
55
+ raise TypeError ("Tristim value must be float or int" )
56
+
57
+ if (not isinstance (TriZ ,(int ,float ))):
58
+ raise TypeError ("Tristim value must be float or int" )
59
+
60
+ yCoord = (TriY / (TriX + TriY + TriZ ))
61
+ return yCoord
62
+
63
+ #xy to Tristim
64
+ def xyTOTri (x ,y ,TriY ):
65
+
66
+ if (not isinstance (x ,(int ,float ))):
67
+ raise TypeError ("x value must be float or int" )
68
+
69
+ if (not isinstance (y ,(int ,float ))):
70
+ raise TypeError ("y value must be float or int" )
71
+
72
+ if (not isinstance (TriY ,(int ,float ))):
73
+ raise TypeError ("TriY value must be float or int" )
74
+
75
+ XCoord = ((x / y )* TriY )
76
+ ZCoord = (((1 - x - y )/ y )* TriY )
77
+ return [XCoord ,YCoord ,ZCoord ]
78
+
79
+ ######################
80
+ #CIE 1960 Colour Space
81
+ ######################
82
+ #Lightness from Luminance
83
+ def YtoL (luminance ):
84
+
85
+ #Reference White Luminance
86
+ Y_n = 1 ##TODO
87
+
88
+ if (not isinstance (luminance ,(int ,float ))):
89
+ raise TypeError ("Luminance value must be float or int" )
90
+
91
+ return lightness
92
+
93
+ #Lightness from normalized Luminance
94
+ def YNormalizedtoL (luminance ):
95
+
96
+ if (not isinstance (luminance ,(int ,float ))):
97
+ raise TypeError ("Luminance value must be float or int" )
98
+
99
+ return lightness
100
+
101
+ #Tristim to u v
102
+ def TriTOuv (TriX ,TriY ,TriZ ):
103
+
104
+ if (not isinstance (TriX ,(int ,float ))):
105
+ raise TypeError ("Tristim value must be float or int" )
106
+
107
+ if (not isinstance (TriY ,(int ,float ))):
108
+ raise TypeError ("Tristim value must be float or int" )
109
+
110
+ if (not isinstance (TriZ ,(int ,float ))):
111
+ raise TypeError ("Tristim value must be float or int" )
112
+
113
+ uCoord = ((4 * TriX )/ (TriX + 15 * TriY + 3 * TriZ ))
114
+ vCoord = ((9 * TriY )/ (TriX + 15 * TriY + 3 * TriZ ))
115
+ return [uCoord ,vCoord ]
116
+
117
+ #Tristim to u
118
+ def TriTOu (TriX ,TriY ,TriZ ):
119
+
120
+ if (not isinstance (TriX ,(int ,float ))):
121
+ raise TypeError ("Tristim value must be float or int" )
122
+
123
+ if (not isinstance (TriY ,(int ,float ))):
124
+ raise TypeError ("Tristim value must be float or int" )
125
+
126
+ if (not isinstance (TriZ ,(int ,float ))):
127
+ raise TypeError ("Tristim value must be float or int" )
128
+
129
+ uCoord = ((4 * TriX )/ (TriX + 15 * TriY + 3 * TriZ ))
130
+ return uCoord
131
+
132
+ #Tristim to v
133
+ def TriTOv (TriX ,TriY ,TriZ ):
134
+
135
+ if (not isinstance (TriX ,(int ,float ))):
136
+ raise TypeError ("Tristim value must be float or int" )
137
+
138
+ if (not isinstance (TriY ,(int ,float ))):
139
+ raise TypeError ("Tristim value must be float or int" )
140
+
141
+ if (not isinstance (TriZ ,(int ,float ))):
142
+ raise TypeError ("Tristim value must be float or int" )
143
+
144
+ vCoord = ((9 * TriY )/ (TriX + 15 * TriY + 3 * TriZ ))
145
+ return vCoord
146
+ ###################################
147
+ #CIE 1931 to/from 1960 Colour Space
148
+ ###################################
149
+ #x y to u v
150
+ def xyTOuv (xCoord , yCoord ):
151
+
152
+ if (not isinstance (xCoord ,(int ,float ))):
153
+ raise TypeError ("xCoord value must be float or int" )
154
+
155
+ if (not isinstance (yCoord ,(int ,float ))):
156
+ raise TypeError ("yCoord value must be float or int" )
157
+
158
+ uCoord = ((4 * xCoord )/ (3 - 2 * xCoord + 12 * yCoord ))
159
+ vCoord = ((9 * yCoord )/ (3 - 2 * xCoord + 12 * yCoord ))
160
+ return [uCoord ,vCoord ]
161
+
162
+ #uv to xy
163
+ def uvTOxy (uCoord , vCoord ):
164
+
165
+ if (not isinstance (uCoord ,(int ,float ))):
166
+ raise TypeError ("uCoord value must be float or int" )
167
+
168
+ if (not isinstance (vCoord ,(int ,float ))):
169
+ raise TypeError ("vCoord value must be float or int" )
170
+
171
+ xCoord = ((9 * uCoord )/ (6 * uCoord - 16 * vCoord + 12 ))
172
+ yCoord = ((4 * vCoord )/ (6 * uCoord - 16 * vCoord + 12 ))
173
+ return [xCoord ,yCoord ]
174
+
175
+ ##############
176
+ #Calculate CCT
177
+ ##############
178
+ #CCT from xy
179
+ def CCTxy (xCoord , yCoord ):
180
+
181
+ if (not isinstance (xCoord ,(int ,float ))):
182
+ raise TypeError ("xCoord value must be float or int" )
183
+
184
+ if (not isinstance (yCoord ,(int ,float ))):
185
+ raise TypeError ("yCoord value must be float or int" )
186
+
187
+ n = ((xCoord - 0.320 )/ (0.1858 - yCoord ))
188
+ CCT = 449 * (n ** 3 ) + 3525 * (n ** 2 ) + 6823.3 * (n ** 2 ) + 5520.33 *
189
+
190
+ return CCT
191
+
192
+ #CCT from TriStim
193
+ def CCT_Tri (TriX ,TriY ,TriZ ):
194
+
195
+ if (not isinstance (TriX ,(int ,float ))):
196
+ raise TypeError ("TriX value must be float or int" )
197
+
198
+ if (not isinstance (TriY ,(int ,float ))):
199
+ raise TypeError ("TriY value must be float or int" )
200
+
201
+ if (not isinstance (TriZ ,(int ,float ))):
202
+ raise TypeError ("TriZ value must be float or int" )
203
+
204
+ n = ((xCoord - 0.320 )/ (0.1858 - yCoord ))
205
+ CCT = 449 * (n ** 3 ) + 3525 * (n ** 2 ) + 6823.3 * (n ** 2 ) + 5520.33 *
206
+
207
+ return CCT
208
+
209
+ #cct from RGB --> noep
210
+ #def CCT_RGB(RedVal, GreenVal, BlueVal):
211
+ #
212
+ # if (not isinstance(RedVal,(int,float))):
213
+ # raise TypeError("RedVal value must be float or int")
214
+ #
215
+ # if (not isinstance(GreenVal,(int,float))):
216
+ # raise TypeError("GreenVal value must be float or int")
217
+ #
218
+ # if (not isinstance(BlueVal,(int,float))):
219
+ # raise TypeError("BlueVal value must be float or int")
220
+ #
221
+ # n = ((xCoord-0.320)/(0.1858-yCoord))
222
+ # CCT = 449*(n**3) + 3525*(n**2) + 6823.3*(n**2) + 5520.33*
223
+ #
224
+ # return CCT
225
+
226
+ ##TODO
227
+ #Standard Observer Lookup Table
228
+ def CIE1931Observer (wavelength ):
229
+ CMF = { 350 : thing1 ,
230
+ 351 : thing2 ,
231
+
232
+
233
+ }
234
+
0 commit comments