-
Notifications
You must be signed in to change notification settings - Fork 1
/
coords.py
117 lines (84 loc) · 2.54 KB
/
coords.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
from typing_extensions import Self
import mpmath
from mpmath import mpf
class RGB:
'''
RGB based color coordinate.
Colorspace is not specified.
'''
def __init__(self, R: mpf, G: mpf, B: mpf) -> None:
self.R = mpf(R)
self.G = mpf(G)
self.B = mpf(B)
def __repr__(self) -> str:
return f"RGB({self.R}, {self.G}, {self.B})"
def to_matrix(self) -> mpmath.matrix:
return mpmath.matrix([self.R, self.G, self.B])
def to_RGB(self) -> Self:
return self
class XYZ:
'''
CIE XYZ based color coordinate.
Chromatic adaption status is not specified.
'''
def __init__(self, X: mpf, Y: mpf, Z: mpf) -> None:
self.X = mpf(X)
self.Y = mpf(Y)
self.Z = mpf(Z)
def __repr__(self) -> str:
return f"XYZ({self.X}, {self.Y}, {self.Z})"
def to_matrix(self) -> mpmath.matrix:
return mpmath.matrix([self.X, self.Y, self.Z])
def to_XYZ(self) -> Self:
return self
def to_xy(self) -> 'xy':
sum_XYZ = self.X + self.Y + self.Z
x = self.X / sum_XYZ
y = self.Y / sum_XYZ
return xy(x, y)
def to_xyY(self) -> 'xyY':
sum_XYZ = self.X + self.Y + self.Z
x = self.X / sum_XYZ
y = self.Y / sum_XYZ
Y = self.Y
return xyY(x, y, Y)
class xy:
'''
CIE xy based color coordinate.
Chromatic adaption status is not specified.
'''
def __init__(self, x: mpf, y: mpf) -> None:
self.x = mpf(x)
self.y = mpf(y)
def __repr__(self) -> str:
return f"xy({self.x}, {self.y})"
def to_matrix(self) -> mpmath.matrix:
return mpmath.matrix([self.x, self.y])
def to_XYZ(self, Y: mpf = 1) -> 'XYZ':
X = self.x * Y / self.y
Z = (1 - self.x - self.y) * Y / self.y
return XYZ(X, Y, Z)
def to_xy(self) -> Self:
return self
def to_xyY(self, Y: mpf = 1) -> 'xyY':
return xyY(self.x, self.y, Y)
class xyY:
'''
CIE xyY based color coordinate.
Chromatic adaption status is not specified.
'''
def __init__(self, x: mpf, y: mpf, Y: mpf) -> None:
self.x = mpf(x)
self.y = mpf(y)
self.Y = mpf(Y)
def __repr__(self) -> str:
return f"xyY({self.x}, {self.y}, {self.Y})"
def to_XYZ(self) -> 'XYZ':
X = self.x * self.Y / self.y
Y = self.Y
Z = (1 - self.x - self.y) * self.Y / self.y
return XYZ(X, Y, Z)
def to_xy(self) -> 'xy':
return xy(self.x, self.y)
def to_xyY(self) -> Self:
return self