Determining if XYZ Tristimulus Values Fall Within a Color Gamut
To determine if given XYZ tristimulus values fall within a color gamut defined by four CIE xy coordinates (red, green, blue, and white), we need to follow these steps:
- Convert the given XYZ tristimulus values to xy coordinates: This is necessary because the gamut is defined in xy coordinates.
- Convert the given RGBW xy coordinates to XYZ: We need the XYZ coordinates of the gamut's vertices to perform the gamut check.
- Perform a gamut check using the xy coordinates: We will use a method to check if a point (the given xy coordinates) lies within a polygon defined by the xy coordinates of the gamut's vertices (R, G, B).
Step 1: Convert XYZ to xy
The conversion from XYZ to xy is given by:
[ x = \frac{X}{X + Y + Z} ]
[ y = \frac{Y}{X + Y + Z} ]
Step 2: Gamut Check
We'll use a simple method to check if a point is inside a triangle (since our gamut is defined by RGB, we can divide it into three triangles: RGB, GRW, and BRW, and check if the point lies within any of these). However, a more straightforward approach for a quadrilateral gamut involves checking if the point lies within the convex hull defined by the RGBW points.
Python Code
import numpy as np
def xyz_to_xy(X, Y, Z):
"""Convert XYZ to xy."""
sum_xyz = X + Y + Z
if sum_xyz == 0:
return 0, 0
x = X / sum_xyz
y = Y / sum_xyz
return x, y
def point_inside_polygon(point, polygon):
"""Check if a point is inside a polygon using the ray casting algorithm."""
x, y = point
n = len(polygon)
inside = False
p1x, p1y = polygon[0]
for i in range(n+1):
p2x, p2y = polygon[i % n]
if min(p1y, p2y) < y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y
return inside
def check_gamut(X, Y, Z, xr, yr, xg, yg, xb, yb, xw, yw):
"""Check if XYZ falls within the gamut defined by xr, yr, xg, yg, xb, yb, xw, yw."""
# Convert XYZ to xy
x, y = xyz_to_xy(X, Y, Z)
# Define the gamut polygon in xy coordinates
gamut_polygon = [(xr, yr), (xg, yg), (xb, yb), (xw, yw)]
# Check if the point is inside the gamut polygon
return point_inside_polygon((x, y), gamut_polygon)
# Example usage
X, Y, Z = 10, 20, 30 # Example XYZ values
xr, yr = 0.6400, 0.3300 # Red
xg, yg = 0.2900, 0.6000 # Green
xb, yb = 0.1500, 0.0600 # Blue
xw, yw = 0.3127, 0.3290 # White
inside_gamut = check_gamut(X, Y, Z, xr, yr, xg, yg, xb, yb, xw, yw)
print(f"XYZ ({X}, {Y}, {Z}) is {'inside' if inside_gamut else 'outside'} the gamut.")
This code defines a function check_gamut that takes the XYZ values and the xy coordinates of the gamut's vertices as input and returns a boolean indicating whether the XYZ values fall within the gamut. The xyz_to_xy function converts XYZ to xy coordinates, and the point_inside_polygon function checks if a point is inside a given polygon using the ray casting algorithm.