Skip to content

Commit 9f4debb

Browse files
committed
Small refactor of the CIE diagram tool
1 parent f1b70af commit 9f4debb

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

tools/cie_diagrams.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ class Color(ColorAll):
7474
"""Custom class for Pointer conversion."""
7575

7676

77-
def get_spline(x, y, steps=100):
78-
"""Get spline."""
79-
80-
return tuple([*i] for i in zip(*alg.interpolate([*zip(x, y)], method='catrom').steps(steps)))
81-
82-
8377
def convert_chromaticity(xy, opt):
8478
"""Convert chromaticities."""
8579

@@ -148,7 +142,7 @@ def get_spectral_locus_labels(opt):
148142
return annotations
149143

150144

151-
class Polygon2D:
145+
class Polygon:
152146
"""2D Polygon."""
153147

154148
def __init__(self, x, y):
@@ -181,23 +175,33 @@ def contains(self, p):
181175
"""
182176

183177
px, py = p
178+
184179
# If point is outside the the bounding box, it is not in the 2D polygon.
185180
if px > self.xmax or px < self.xmin or py > self.ymax or py < self.ymin:
186181
return False
187182

188183
winding = 0
184+
# Get the previous vertex
185+
ax = self.x[0]
186+
ay = self.y[0]
189187
for i in range(1, self.length):
190-
# Get the previous and and current vertex
191-
j = i - 1
192-
ax, ay = self.x[j], self.y[j]
193-
bx, by = self.x[i], self.y[i]
188+
# Get the current vertex
189+
bx = self.x[i]
190+
by = self.y[i]
194191

192+
# Check if crossing
195193
if ay <= py:
196194
if by > py and ((bx - ax) * (py - ay) - (px - ax) * (by - ay)) > 0:
197195
winding += 1
198196
elif by <= py and ((bx - ax) * (py - ay) - (px - ax) * (by - ay)) < 0:
199-
winding -= 1
200-
return winding != 0
197+
winding += 1
198+
199+
# Current is now previous
200+
ax = bx
201+
ay = by
202+
203+
# If odd, we are inside
204+
return bool(winding & 1)
201205

202206

203207
class DiagramOptions:
@@ -302,7 +306,7 @@ class Color(ColorAll):
302306
annotations = []
303307

304308
# Get points for the spectral locus
305-
for r in alg.linspace(360, 780, int(len(opt.observer) * 1.5)):
309+
for r in alg.linspace(360, 780, len(opt.observer)):
306310
xy = opt.observer.xy(r)
307311
x, y = Color.convert_chromaticity('xy-1931', opt.chromaticity, xy)[:-1]
308312
xs.append(x)
@@ -353,7 +357,7 @@ class Color(ColorAll):
353357
sy,
354358
color,
355359
label,
356-
Polygon2D(sx, sy),
360+
Polygon(sx, sy),
357361
_x,
358362
_y
359363
)
@@ -387,7 +391,7 @@ class Color(ColorAll):
387391
sy,
388392
color,
389393
label,
390-
Polygon2D(sx, sy),
394+
Polygon(sx, sy),
391395
_x,
392396
_y
393397
)
@@ -414,7 +418,7 @@ class Color(ColorAll):
414418
sy,
415419
color,
416420
space,
417-
Polygon2D(sx, sy),
421+
Polygon(sx, sy),
418422
xy[0],
419423
xy[1]
420424
)
@@ -428,7 +432,7 @@ class Color(ColorAll):
428432
cx = []
429433
cy = []
430434
cc = []
431-
poly = Polygon2D(xs, ys)
435+
poly = Polygon(xs, ys)
432436
min_range_x = float('inf')
433437
min_range_y = float('inf')
434438
max_range_x = float('-inf')
@@ -766,7 +770,9 @@ class Color(ColorAll):
766770
opacity=0.5
767771
))
768772

769-
uaxis, vaxis = get_spline(uaxis, vaxis, len(uaxis) * 3)
773+
uaxis, vaxis = zip(
774+
*alg.interpolate([[i, j] for i, j in zip(uaxis, vaxis)], method='sprague').steps(len(uaxis) * 3)
775+
)
770776
fig.add_traces(data=go.Scatter(
771777
x=uaxis,
772778
y=vaxis,

0 commit comments

Comments
 (0)