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

**URL:** <https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128>\
**Category:** Visualization\
**Tags:** question, colors\
**Created:** [December 28, 2023, 1:09pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128 "2023-12-28T13:09:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 28, 2023, 1:09pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128/1 "2023-12-28T13:09:10Z")

</div>

Is there a Julia package that allows checking if a color in [XYZ or xyY](https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space) space is valid, and if not, allow finding a nearest point (eg by bisecting the ray from the white point)?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 28, 2023, 2:04pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128/2 "2023-12-28T14:04:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 28, 2023, 2:30pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128/3 "2023-12-28T14:30:41Z")

</div>

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

> [@juliohm](#):
>
> Maybe you need to add an extra method that is missing?

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 28, 2023, 8:27pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128/4 "2023-12-28T20:27:53Z")

</div>

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

 ![CIE_Colors_Spectral_Locus](https://global.discourse-cdn.com/julialang/original/3X/f/d/fdb36f407597e6a3a2cdcc39829b2543d28afe14.png)

> **Code**
>
> ```julia
> 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))")
> 
> ```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 3, 2024, 1:52pm UTC](https://discourse.julialang.org/t/is-there-a-package-that-supports-gamut-calculations-for-the-xyy-color-space/108128/5 "2024-01-03T13:52:48Z")

</div>

Thanks, this is very helpful.
