Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _plot_color_constancy_data(
data_xyz100, wp_xyz100, colorspace, approximate_colors_in_srgb=False
):
# k0 is the coordinate that corresponds to "lightness"
k0 = colorspace.k0
k1, k2 = [k for k in [0, 1, 2] if k != k0]
wp = colorspace.from_xyz100(wp_xyz100)[[k1, k2]]
srgb = SrgbLinear()
for xyz in data_xyz100:
d = colorspace.from_xyz100(xyz)[[k1, k2]]
# There are numerous possibilities of defining the "best" approximating line for
# a bunch of points (x_i, y_i). For example, one could try and minimize the
# expression
# sum_i (-numpy.sin(theta) * x_i + numpy.cos(theta) * y_i) ** 2
# over theta, which means to minimize the orthogonal component of (x_i, y_i) to
# (cos(theta), sin(theta)).
#
# A more simple and effective approach is to use the average of all points,
# theta = arctan(sum(y_i) / sum(x_i)).
# This also fits in nicely with minimization problems which move around the
# points to minimize the difference from the average,
#
# sum_j (y_j / x_j - bar{y} / bar{x}) ** 2 -> min,
def plot_munsell(self, V):
_, v, _, xyy = get_munsell_data()
# pick the data from the given munsell level
xyy = xyy[:, v == V]
x, y, Y = xyy
xyz100 = numpy.array([Y / y * x, Y, Y / y * (1 - x - y)])
vals = self.from_xyz100(xyz100)
srgb = SrgbLinear()
rgb = srgb.from_xyz100(xyz100)
is_legal_srgb = numpy.all((0 <= rgb) & (rgb <= 1), axis=0)
idx = [0, 1, 2]
k1, k2 = idx[: self.k0] + idx[self.k0 + 1 :]
# plot the ones that cannot be represented in SRGB
plt.plot(
vals[k1, ~is_legal_srgb],
vals[k2, ~is_legal_srgb],
"o",
color="white",
markeredgecolor="black",
)
# plot the srgb dots
for val, rgb_ in zip(vals[:, is_legal_srgb].T, rgb[:, is_legal_srgb].T):
def _plot_rgb_triangle(xy_to_2d, bright=True):
# plot sRGB triangle
# discretization points
n = 50
# Get all RGB values that sum up to 1.
rgb_linear, _ = meshzoo.triangle(n)
if bright:
# For the x-y-diagram, it doesn't matter if the values are scaled in any way.
# After all, the tranlation to XYZ is linear, and then to xyY it's (X/(X+Y+Z),
# Y/(X+Y+Z), Y), so the factor will only be present in the last component which
# is discarded. To make the plot a bit brighter, scale the colors up as much as
# possible.
rgb_linear /= numpy.max(rgb_linear, axis=0)
srgb_linear = SrgbLinear()
xyz = srgb_linear.to_xyz100(rgb_linear)
xyy_vals = xy_to_2d(_xyy_from_xyz100(xyz)[:2])
# Unfortunately, one cannot use tripcolors with explicit RGB specification
# (see ). As a
# workaround, associate range(n) data with the points and create a colormap
# that associates the integer values with the respective RGBs.
z = numpy.arange(xyy_vals.shape[1])
rgb = srgb_linear.to_srgb1(rgb_linear)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
"gamut", rgb.T, N=len(rgb.T)
)
triang = matplotlib.tri.Triangulation(xyy_vals[0], xyy_vals[1])
plt.tripcolor(triang, z, shading="gouraud", cmap=cmap)
return
def _plot_srgb_gamut(self, k0, level, bright=False):
import meshzoo
# Get all RGB values that sum up to 1.
bary, triangles = meshzoo.triangle(n=50)
corners = numpy.array([[0, 0], [1, 0], [0, 1]]).T
srgb_vals = numpy.dot(corners, bary).T
srgb_vals = numpy.column_stack([srgb_vals, 1.0 - numpy.sum(srgb_vals, axis=1)])
# matplotlib is sensitive when it comes to srgb values, so take good care here
assert numpy.all(srgb_vals > -1.0e-10)
srgb_vals[srgb_vals < 0.0] = 0.0
# Use bisection to
srgb_linear = SrgbLinear()
tol = 1.0e-5
# Use zeros() instead of empty() here to avoid invalid values when setting up
# the cmap below.
self_vals = numpy.zeros((srgb_vals.shape[0], 3))
srgb_linear_vals = numpy.zeros((srgb_vals.shape[0], 3))
mask = numpy.ones(srgb_vals.shape[0], dtype=bool)
for k, val in enumerate(srgb_vals):
alpha_min = 0.0
xyz100 = srgb_linear.to_xyz100(val * alpha_min)
self_val_min = self.from_xyz100(xyz100)
if self_val_min[k0] > level:
mask[k] = False
continue
alpha_max = 1.0 / numpy.max(val)