import numpy as np
# Step size and wavelengths (all aligned)
wavelengths = np.arange(380, 781, 10)
# M02 magnesium oxide reflectance - approximate NIST data
R_M02 = np.array([
0.978, 0.982, 0.985, 0.988, 0.990, 0.992, 0.990, 0.991,
0.990, 0.992, 0.993, 0.994, 0.995, 0.995, 0.996, 0.995,
0.994, 0.995, 0.993, 0.991, 0.987, 0.984, 0.980, 0.978,
0.973, 0.969, 0.965, 0.961, 0.957, 0.953, 0.949, 0.945,
0.941, 0.938, 0.934, 0.930, 0.926, 0.922, 0.918, 0.915,
0.911
])
# CIE D65 SPD (relative energy)
E_D65 = np.array([
49.9755, 52.3118, 54.6482, 68.7015, 82.7549, 87.1204, 91.486,
92.4589, 93.4318, 90.057, 86.6823, 95.7736, 104.865, 110.936,
117.008, 117.41, 117.812, 116.336, 114.861, 115.392, 115.923,
112.367, 108.811, 109.082, 109.354, 108.578, 107.802, 106.296,
104.79, 106.239, 107.689, 106.047, 104.405, 104.225, 104.046,
102.023, 100.0, 98.1671, 96.3342, 96.0611, 95.788
])
# CIE 1931 2° Standard Observer color matching functions (x_bar, y_bar, z_bar)
x_bar = np.array([
0.0014, 0.0042, 0.0143, 0.0435, 0.1344, 0.2839, 0.3483,
0.3362, 0.2908, 0.1954, 0.0956, 0.032, 0.0049, 0.0093,
0.0633, 0.1655, 0.2904, 0.4334, 0.5945, 0.7621, 0.9163,
1.0263, 1.0622, 1.0026, 0.8544, 0.6424, 0.4479, 0.2835,
0.1649, 0.0874, 0.0468, 0.0227, 0.0114, 0.0058, 0.0029,
0.0014, 0.0007, 0.0003, 0.0002, 0.0001, 0
])
y_bar = np.array([
0.0000, 0.0001, 0.0004, 0.0012, 0.004, 0.0116, 0.023,
0.038, 0.06, 0.091, 0.139, 0.208, 0.323, 0.503, 0.71,
0.862, 0.954, 0.9949, 0.995, 0.952, 0.87,
0.757, 0.631, 0.503, 0.381, 0.265, 0.175, 0.107,
0.061, 0.032, 0.017, 0.0082, 0.0041, 0.0021, 0.001,
0.0005, 0.0002, 0.0001, 0.0001, 0, 0
])
z_bar = np.array([
0.0065, 0.0201, 0.0679, 0.2074, 0.6456, 1.3856, 1.7471,
1.7721, 1.6692, 1.2876, 0.813, 0.4652, 0.272, 0.1582,
0.0782, 0.0422, 0.0203, 0.0087, 0.0039, 0.0021, 0.0017,
0.0011, 0.0008, 0.0003, 0.0002, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
])
# Step size in nm
delta_lambda = 10
# Normalize factor k (for illuminant)
k = 100 / np.sum(E_D65 * y_bar * delta_lambda)
# Calculate MgO white point (XYZ)
X_MgO = k * np.sum(R_M02 * E_D65 * x_bar * delta_lambda)
Y_MgO = k * np.sum(R_M02 * E_D65 * y_bar * delta_lambda)
Z_MgO = k * np.sum(R_M02 * E_D65 * z_bar * delta_lambda)
# Chromaticity coordinates of MgO white point
x_MgO = X_MgO / (X_MgO + Y_MgO + Z_MgO)
y_MgO = Y_MgO / (X_MgO + Y_MgO + Z_MgO)
print(f"Calculated MgO (M02) white point under D65 illuminant:")
print(f"X = {X_MgO:.4f}, Y = {Y_MgO:.4f}, Z = {Z_MgO:.4f}")
print(f"x = {x_MgO:.4f}, y = {y_MgO:.4f}")
# === Conversion functions ===
def xyY_to_XYZ(x, y, Y):
"""Convert from xyY to XYZ."""
if y == 0:
return 0.0, 0.0, 0.0
X = (x / y) * Y
Z = ((1 - x - y) / y) * Y
return X, Y, Z
def bradford_CAT_matrix():
"""Returns Bradford CAT matrix and its inverse."""
M = np.array([[ 0.8951, 0.2664, -0.1614],
[-0.7502, 1.7135, 0.0367],
[ 0.0389, -0.0685, 1.0296]])
M_inv = np.linalg.inv(M)
return M, M_inv
def chromatic_adaptation_Bradford(XYZ_source, XYZ_source_white, XYZ_target_white):
"""
Adapt source XYZ to target white using Bradford chromatic adaptation.
"""
M, M_inv = bradford_CAT_matrix()
LMS_source = M @ XYZ_source
LMS_source_white = M @ XYZ_source_white
LMS_target_white = M @ XYZ_target_white
D = np.diag(LMS_target_white / LMS_source_white)
LMS_adapted = D @ LMS_source
XYZ_adapted = M_inv @ LMS_adapted
return XYZ_adapted
# Example: convert xyY (MgO-based) to D65-adapted XYZ
# Suppose input xyY (measured relative to MgO, source white point)
# Example values (your input): (you can replace with your data)
x_in = 0.3 # example x
y_in = 0.32 # example y
Y_in = 50 # example luminance / reflectance Y
# Convert input xyY -> XYZ (MgO white reference)
XYZ_in = np.array(xyY_to_XYZ(x_in, y_in, Y_in))
# Source white point (MgO)
XYZ_white_source = np.array([X_MgO, Y_MgO, Z_MgO])
# Target D65 white point (standard)
XYZ_white_D65 = np.array([0.95047, 1.00000, 1.08883]) # CIE 1931 2° D65 white
# Perform chromatic adaptation Bradford transform
XYZ_adapted = chromatic_adaptation_Bradford(XYZ_in, XYZ_white_source, XYZ_white_D65)
print("\nInput xyY converted to XYZ (MgO reference):")
print(f"X = {XYZ_in[0]:.4f}, Y = {XYZ_in[1]:.4f}, Z = {XYZ_in[2]:.4f}")
print("\nChromatically adapted XYZ to D65:")
print(f"X = {XYZ_adapted[0]:.4f}, Y = {XYZ_adapted[1]:.4f}, Z = {XYZ_adapted[2]:.4f}")