-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb_cie.py
221 lines (168 loc) · 7.96 KB
/
rgb_cie.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
"""
Library for RGB / CIE1931 coversion.
Ported and extended from Bryan Johnson's JavaScript implementation:
https://github.com/bjohnso5/hue-hacking
Copyright (c) 2014 Benjamin Knight / MIT License.
"""
import math
import random
from collections import namedtuple
# Represents a CIE 1931 XY coordinate pair.
XYPoint = namedtuple('XYPoint', ['x', 'y'])
class ColorHelper:
Red = XYPoint(0.675, 0.322)
Lime = XYPoint(0.4091, 0.518)
Blue = XYPoint(0.167, 0.04)
def hexToRed(self, hex):
"""Parses a valid hex color string and returns the Red RGB integer value."""
return int(hex[0:2], 16)
def hexToGreen(self, hex):
"""Parses a valid hex color string and returns the Green RGB integer value."""
return int(hex[2:4], 16)
def hexToBlue(self, hex):
"""Parses a valid hex color string and returns the Blue RGB integer value."""
return int(hex[4:6], 16)
def hexToRGB(self, h):
"""Converts a valid hex color string to an RGB array."""
rgb = [self.hexToRed(h), self.hexToGreen(h), self.hexToBlue(h)]
return rgb
def rgbToHex(self, r, g, b):
"""Converts RGB to hex."""
return '%02x%02x%02x' % (r, g, b)
def randomRGBValue(self):
"""Return a random Integer in the range of 0 to 255, representing an RGB color value."""
return random.randrange(0, 256)
def crossProduct(self, p1, p2):
"""Returns the cross product of two XYPoints."""
return (p1.x * p2.y - p1.y * p2.x)
def checkPointInLampsReach(self, p):
"""Check if the provided XYPoint can be recreated by a Hue lamp."""
v1 = XYPoint(self.Lime.x - self.Red.x, self.Lime.y - self.Red.y)
v2 = XYPoint(self.Blue.x - self.Red.x, self.Blue.y - self.Red.y)
q = XYPoint(p.x - self.Red.x, p.y - self.Red.y)
s = self.crossProduct(q, v2) / self.crossProduct(v1, v2)
t = self.crossProduct(v1, q) / self.crossProduct(v1, v2)
return (s >= 0.0) and (t >= 0.0) and (s + t <= 1.0)
def getClosestPointToLine(self, A, B, P):
"""Find the closest point on a line. This point will be reproducible by a Hue lamp."""
AP = XYPoint(P.x - A.x, P.y - A.y)
AB = XYPoint(B.x - A.x, B.y - A.y)
ab2 = AB.x * AB.x + AB.y * AB.y
ap_ab = AP.x * AB.x + AP.y * AB.y
t = ap_ab / ab2
if t < 0.0:
t = 0.0
elif t > 1.0:
t = 1.0
return XYPoint(A.x + AB.x * t, A.y + AB.y * t)
def getClosestPointToPoint(self, xyPoint):
# Color is unreproducible, find the closest point on each line in the CIE 1931 'triangle'.
pAB = self.getClosestPointToLine(self.Red, self.Lime, xyPoint)
pAC = self.getClosestPointToLine(self.Blue, self.Red, xyPoint)
pBC = self.getClosestPointToLine(self.Lime, self.Blue, xyPoint)
# Get the distances per point and see which point is closer to our Point.
dAB = self.getDistanceBetweenTwoPoints(xyPoint, pAB)
dAC = self.getDistanceBetweenTwoPoints(xyPoint, pAC)
dBC = self.getDistanceBetweenTwoPoints(xyPoint, pBC)
lowest = dAB
closestPoint = pAB
if (dAC < lowest):
lowest = dAC
closestPoint = pAC
if (dBC < lowest):
lowest = dBC
closestPoint = pBC
# Change the xy value to a value which is within the reach of the lamp.
cx = closestPoint.x
cy = closestPoint.y
return XYPoint(cx, cy)
def getDistanceBetweenTwoPoints(self, one, two):
"""Returns the distance between two XYPoints."""
dx = one.x - two.x
dy = one.y - two.y
return math.sqrt(dx * dx + dy * dy)
def getXYPointFromRGB(self, red, green, blue):
"""Returns an XYPoint object containing the closest available CIE 1931 coordinates
based on the RGB input values."""
r = ((red + 0.055) / (1.0 + 0.055))**2.4 if (red > 0.04045) else (red / 12.92)
g = ((green + 0.055) / (1.0 + 0.055))**2.4 if (green > 0.04045) else (green / 12.92)
b = ((blue + 0.055) / (1.0 + 0.055))**2.4 if (blue > 0.04045) else (blue / 12.92)
X = r * 0.4360747 + g * 0.3850649 + b * 0.0930804
Y = r * 0.2225045 + g * 0.7168786 + b * 0.0406169
Z = r * 0.0139322 + g * 0.0971045 + b * 0.7141733
if X + Y + Z == 0:
cx = cy = 0
else:
cx = X / (X + Y + Z)
cy = Y / (X + Y + Z)
# Check if the given XY value is within the colourreach of our lamps.
xyPoint = XYPoint(cx, cy)
inReachOfLamps = self.checkPointInLampsReach(xyPoint)
if not inReachOfLamps:
xyPoint = self.getClosestPointToPoint(xyPoint)
return xyPoint
def getRGBFromXYAndBrightness(self, x, y, bri=1):
"""Returns a rgb tuplet for given x, y values. Not actually an inverse of `getXYPointFromRGB`.
Implementation of the instructions found on the Philips Hue iOS SDK docs: http://goo.gl/kWKXKl
"""
xyPoint = XYPoint(x, y)
# Check if the xy value is within the color gamut of the lamp.
# If not continue with step 2, otherwise step 3.
# We do this to calculate the most accurate color the given light can actually do.
if not self.checkPointInLampsReach(xyPoint):
# Calculate the closest point on the color gamut triangle
# and use that as xy value See step 6 of color to xy.
xyPoint = self.getClosestPointToPoint(xyPoint)
# Calculate XYZ values Convert using the following formulas:
Y = bri
X = (Y / xyPoint.y) * xyPoint.x
Z = (Y / xyPoint.y) * (1 - xyPoint.x - xyPoint.y)
# Convert to RGB using Wide RGB D65 conversion.
r = X * 1.612 - Y * 0.203 - Z * 0.302
g = -X * 0.509 + Y * 1.412 + Z * 0.066
b = X * 0.026 - Y * 0.072 + Z * 0.962
# Apply reverse gamma correction.
r, g, b = map(
lambda x: (12.92 * x) if (x <= 0.0031308) else ((1.0 + 0.055) * pow(x, (1.0 / 2.4)) - 0.055),
[r, g, b]
)
# Bring all negative components to zero.
r, g, b = map(lambda x: max(0, x), [r, g, b])
# If one component is greater than 1, weight components by that value.
max_component = max(r, g, b)
if max_component > 1:
r, g, b = map(lambda x: x / max_component, [r, g, b])
r, g, b = map(lambda x: int(x * 255), [r, g, b])
return (r, g, b)
class Converter:
color = ColorHelper()
def hexToCIE1931(self, h):
"""Converts hexadecimal colors represented as a String to approximate CIE 1931 coordinates.
May not produce accurate values."""
rgb = self.color.hexToRGB(h)
return self.rgbToCIE1931(rgb[0], rgb[1], rgb[2])
def rgbToCIE1931(self, red, green, blue):
"""Converts red, green and blue integer values to approximate CIE 1931 x and y coordinates.
Algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=02#text2.
May not produce accurate values.
"""
point = self.color.getXYPointFromRGB(red, green, blue)
return [point.x, point.y]
def getCIEColor(self, hexColor=None):
"""Returns the approximate CIE 1931 x, y coordinates represented by the supplied hexColor parameter,
or of a random color if the parameter is not passed.
The point of this function is to let people set a lamp's color to any random color.
Arguably this should be implemented elsewhere."""
xy = []
if hexColor:
xy = self.hexToCIE1931(hexColor)
else:
r = self.color.randomRGBValue()
g = self.color.randomRGBValue()
b = self.color.randomRGBValue()
xy = self.rgbToCIE1931(r, g, b)
return xy
def CIE1931ToHex(self, x, y, bri=1):
"""Converts CIE 1931 x and y coordinates and brightness value from 0 to 1 to a CSS hex color."""
r, g, b = self.color.getRGBFromXYAndBrightness(x, y, bri)
return self.color.rgbToHex(r, g, b)