Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_observers(observer):
lmbda, data = observer
# For plot colors, take the SRGB approximation of the color that the
# observer would perceive if a light spectrum hits its eye that corresponds
# to the sensitivity spectrum.
colors = []
for k in range(3):
out = colorio.illuminants.spectrum_to_xyz100(
(lmbda, data[k]), observer=observer
)
out *= 100 / out[1]
srgb = colorio.SrgbLinear()
rgb_vals = srgb.from_xyz100(out)
rgb_vals[rgb_vals < 0] = 0
# project down to proper rgb
rgb_vals /= max(rgb_vals)
colors.append(srgb.to_srgb1(rgb_vals))
plt.plot(lmbda, data[0], color=colors[0])
plt.plot(lmbda, data[1], color=colors[1])
plt.plot(lmbda, data[2], color=colors[2])
plt.xlabel("wavelength (nm)")
plt.grid()
def test_spectrum_to_xyz100():
spectrum = colorio.illuminants.d65()
observer = colorio.observers.cie_1931_2()
colorio.illuminants.spectrum_to_xyz100(spectrum, observer)
return
def _plot_monochromatic(observer, xy_to_2d, fill_horseshoe=True):
# draw outline of monochromatic spectra
lmbda = 1.0e-9 * numpy.arange(380, 701)
values = []
# TODO vectorize (see )
for k, _ in enumerate(lmbda):
data = numpy.zeros(len(lmbda))
data[k] = 1.0
values.append(_xyy_from_xyz100(spectrum_to_xyz100((lmbda, data), observer))[:2])
values = numpy.array(values)
# Add the values between the first and the last point of the horseshoe
t = numpy.linspace(0.0, 1.0, 101)
connect = xy_to_2d(numpy.outer(values[0], t) + numpy.outer(values[-1], 1 - t))
values = xy_to_2d(values.T).T
full = numpy.concatenate([values, connect.T])
# fill horseshoe area
if fill_horseshoe:
plt.fill(*full.T, color=[0.8, 0.8, 0.8], zorder=0)
# plot horseshoe outline
plt.plot(
values[:, 0],
values[:, 1],
"-k",
def _plot_planckian_locus(observer, xy_to_2d):
# plot planckian locus
values = []
for temp in numpy.arange(1000, 20001, 100):
xyy_vals = xy_to_2d(
_xyy_from_xyz100(spectrum_to_xyz100(planckian_radiator(temp), observer))
)
values.append(xyy_vals)
values = numpy.array(values)
plt.plot(values[:, 0], values[:, 1], ":k", label="Planckian locus")
return
def xy_gamut_mesh(lcar):
import optimesh
import pygmsh
observer = observers.cie_1931_2()
# Gather all points on the horseshoe outline
lmbda = 1.0e-9 * numpy.arange(380, 701)
all_points = numpy.empty((len(lmbda), 2))
for k in range(len(lmbda)):
data = numpy.zeros(len(lmbda))
data[k] = 1.0
all_points[k] = _xyy_from_xyz100(spectrum_to_xyz100((lmbda, data), observer))[
:2
]
# Generate gmsh geometry: spline + straight line
all_points = numpy.column_stack([all_points, numpy.zeros(len(all_points))])
geom = pygmsh.built_in.Geometry()
gmsh_points = [geom.add_point(pt, lcar) for pt in all_points]
s1 = geom.add_spline(gmsh_points)
s2 = geom.add_line(gmsh_points[-1], gmsh_points[0])
ll = geom.add_line_loop([s1, s2])
geom.add_plane_surface(ll)
mesh = pygmsh.generate_mesh(geom)
points, cells = optimesh.cvt.quasi_newton_uniform_lloyd(
mesh.points, mesh.get_cells_type("triangle"), 1.0e-2, 100, omega=2.0
)
def get_mono_outline_xy(observer, max_stepsize, max_angle=None):
"""Monochromatic light of different frequencies form a horseshoe-like shape in
xy-space. Get the outline of that space.
"""
lmbda, data = observer
m = lmbda.shape[0]
mono = numpy.zeros(m)
# first the straight connector at the bottom
mono[:] = 0.0
mono[-1] = 1.0
first = _xyy_from_xyz100(spectrum_to_xyz100((lmbda, mono), observer))[:2]
mono[:] = 0.0
mono[0] = 1.0
last = _xyy_from_xyz100(spectrum_to_xyz100((lmbda, mono), observer))[:2]
#
diff = first - last
dist = numpy.sqrt(numpy.sum(diff ** 2))
num_steps = dist / max_stepsize
num_steps = int(num_steps) + 2
# connection between lowest and highest frequencies
vals_conn = numpy.array(
[first * (1 - t) + last * t for t in numpy.linspace(0, 1, num_steps)]
)
vals_mono = [vals_conn[-1]]
for k in range(1, m):
mono[:] = 0.0
last = _xyy_from_xyz100(spectrum_to_xyz100((lmbda, mono), observer))[:2]
#
diff = first - last
dist = numpy.sqrt(numpy.sum(diff ** 2))
num_steps = dist / max_stepsize
num_steps = int(num_steps) + 2
# connection between lowest and highest frequencies
vals_conn = numpy.array(
[first * (1 - t) + last * t for t in numpy.linspace(0, 1, num_steps)]
)
vals_mono = [vals_conn[-1]]
for k in range(1, m):
mono[:] = 0.0
mono[k] = 1.0
val = _xyy_from_xyz100(spectrum_to_xyz100((lmbda, mono), observer))[:2]
diff = vals_mono[-1] - val
dist = numpy.sqrt(numpy.dot(diff, diff))
if dist > max_stepsize:
vals_mono.append(val)
vals_mono.append(vals_conn[0])
vals_mono = numpy.array(vals_mono)
return vals_mono, vals_conn