Skip to content

Commit

Permalink
Added more examples to colorsynth.srgb().
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie committed Nov 2, 2023
1 parent fe2549f commit 0e6a625
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions colorsynth/_colorsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,66 @@ def srgb(
# Plot the result as an image
plt.figure();
plt.imshow(rgb);
|
Plot the response curves of the :math:`R`, :math:`G`, and :math:`B` to
a constant spectral radiance
.. jupyter-execute::
# Define an evenly-spaced grid of wavelengths
wavelength = np.linspace(380, 780, num=101) * u.nm
spectral_radiance = np.diagflat(np.ones(wavelength.shape))
# Calculate the CIE 1931 tristimulus values from the specdtral radiance
xyz = colorsynth.cie_1931_tristimulus(spectral_radiance, wavelength[..., np.newaxis], axis=0)
# Normalize the tristimulus values based on the max value of the Y parameter
xyz = xyz / xyz.max(axis=1, keepdims=True)
xyz = xyz * np.array([0.9505, 1.0000, 1.0890])[..., np.newaxis]
# Convert the tristimulus values into sRGB
r, g, b = np.clip(colorsynth.srgb(xyz, axis=0), 0, 10)
plt.figure();
plt.plot(wavelength, r, color="red");
plt.plot(wavelength, g, color="green");
plt.plot(wavelength, b, color="blue");
|
Plot the CIE 1931 chromaticity diagram
.. jupyter-execute::
x = np.linspace(0, 1, num=100)[:, np.newaxis]
y = np.linspace(0, 1, num=101)[np.newaxis, :]
x, y = np.broadcast_arrays(x, y)
z = 1 - x - y
Y = 0.5
X = Y * x / y
Z = Y * z / y
XYZ = [
X,
np.broadcast_to(Y, z.shape),
Z,
]
XYZ = np.stack(XYZ, axis=-1)
rgb = colorsynth.srgb(XYZ, axis=-1)
rgb = np.clip(rgb, 0, 1)
rgb[X < 0] = 1
rgb[Z < 0] = 1
rgb[X >= 0.9505] = 1
rgb[Z >= 1.0890] = 1
plt.figure();
plt.pcolormesh(x, y, rgb);
"""
x, y, z = np.moveaxis(tristimulus, axis, 0)

Expand Down

0 comments on commit 0e6a625

Please sign in to comment.