Is there a package that supports gamut calculations for the xyY color space?

Is there a Julia package that allows checking if a color in XYZ or xyY space is valid, and if not, allow finding a nearest point (eg by bisecting the ray from the white point)?

I believe that Colors.jl is your best bet. I didn’t check the source, but I remember they rely on ColorVectorSpace.jl for linear algebra.

Maybe you need to add an extra method that is missing?

Colors.jl is the first place I looked (should have said).

AFAIK it needs an extra table for the spectral locus, too. But if no other package has this, then that’s indeed the best place for a PR.

@Tamas_Papp, please check if the following makes sense to you:

Code
using Colors, PolygonOps, StaticArrays, Plots

xyz = colormatch.(360:830)   # sweep wavelengths of interest
x = getproperty.(xyz, :x)
y = getproperty.(xyz, :y)
z = getproperty.(xyz, :z)

# CIE 1976 UCS diagram:
ucs(x, y, z) = (4x/(x + 15y + 3z), 9y/(x + 15y + 3z))

p = ucs.(x, y, z)
polygon = SVector.([p; p[1]])
plot(p; seriestype=:shape, fc=:blues, fa=0.5, ratio=1)

# test if points p1 and p2 are in gamut:
x1, y1, z1 = 0.5, 0.5, 0.5
p1 = ucs(x1, y1, z1)
x2, y2, z2 = 0.0, 0.2, 0.5
p2 = ucs(x2, y2, z2)

bool1 = inpolygon(p1, polygon)      # true
bool2 = inpolygon(p2, polygon)      # false

scatter!(p1; c=:green, label="In gamut = $(Bool(bool1))")
scatter!(p2; c=:red, label="In gamut = $(Bool(bool2))")
1 Like

Thanks, this is very helpful.

1 Like