Debian Codegraph @

DisplayCAL/argyll_rgb2xyz.py

raw
1 """Functions and data for RGB/XYZ color space conversions.
2
3 It defines ink tables, normalization factors, and channel mappings used in the
4 conversion process.
5 """
6
7 import math
8
9 from DisplayCAL import colormath
10
11 # from xcolorants.c
12 ICX_INK_TABLE = {
13 "C": [[0.12, 0.18, 0.48], [0.12, 0.18, 0.48]],
14 "M": [[0.38, 0.19, 0.20], [0.38, 0.19, 0.20]],
15 "Y": [[0.76, 0.81, 0.11], [0.76, 0.81, 0.11]],
16 "K": [[0.01, 0.01, 0.01], [0.04, 0.04, 0.04]],
17 "O": [[0.59, 0.41, 0.03], [0.59, 0.41, 0.05]],
18 "R": [[0.412414, 0.212642, 0.019325], [0.40, 0.21, 0.05]],
19 "G": [[0.357618, 0.715136, 0.119207], [0.11, 0.27, 0.21]],
20 "B": [[0.180511, 0.072193, 0.950770], [0.11, 0.27, 0.47]],
21 "W": [
22 [0.950543, 1.0, 1.089303], # D65 ?
23 colormath.get_standard_illuminant("D50"),
24 ], # D50
25 "LC": [[0.76, 0.89, 1.08], [0.76, 0.89, 1.08]],
26 "LM": [[0.83, 0.74, 1.02], [0.83, 0.74, 1.02]],
27 "LY": [[0.88, 0.97, 0.72], [0.88, 0.97, 0.72]],
28 "LK": [[0.56, 0.60, 0.65], [0.56, 0.60, 0.65]],
29 "MC": [[0.61, 0.81, 1.07], [0.61, 0.81, 1.07]],
30 "MM": [[0.74, 0.53, 0.97], [0.74, 0.53, 0.97]],
31 "MY": [[0.82, 0.93, 0.40], [0.82, 0.93, 0.40]],
32 "MK": [[0.27, 0.29, 0.31], [0.27, 0.29, 0.31]],
33 "LLK": [
34 [0.76, 0.72, 0.65], # Very rough - should substitute real numbers
35 [0.76, 0.72, 0.65],
36 ],
37 "": [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
38 }
39
40 NORMALIZATION_FACTOR = {"Ynorm": 0.0}
41 IIX_CHANNEL_MAPPING = {"iix": {0: "R", 1: "G", 2: "B"}}
42
43 for e in range(3):
44 NORMALIZATION_FACTOR["Ynorm"] += ICX_INK_TABLE[IIX_CHANNEL_MAPPING["iix"][e]][0][1]
45 NORMALIZATION_FACTOR["Ynorm"] = 1.0 / NORMALIZATION_FACTOR["Ynorm"]
46
47
48 def xyz_denormalize_remove_glare(x: float, y: float, z: float) -> tuple:
49 """Convert XYZ to RGB using the inverse of the RGB to XYZ conversion.
50
51 Args:
52 x (float): The X component of the XYZ color.
53 y (float): The Y component of the XYZ color.
54 z (float): The Z component of the XYZ color.
55
56 Returns:
57 tuple: A tuple containing the X, Y, and Z components of the XYZ color.
58 """
59 xyz = [x, y, z]
60 # De-Normalize Y from 1.0, & remove black glare
61 for j in range(3):
62 xyz[j] = (xyz[j] - ICX_INK_TABLE["K"][0][j]) / (1.0 - ICX_INK_TABLE["K"][0][j])
63 xyz[j] /= NORMALIZATION_FACTOR["Ynorm"]
64 return tuple(xyz)
65
66
67 def xyz_normalize_add_glare(x: float, y: float, z: float) -> tuple:
68 """Convert XYZ to RGB using the inverse of the RGB to XYZ conversion.
69
70 Args:
71 x (float): The X component of the XYZ color.
72 y (float): The Y component of the XYZ color.
73 z (float): The Z component of the XYZ color.
74
75 Returns:
76 tuple: A tuple containing the X, Y, and Z components of the XYZ color.
77 """
78 xyz = [x, y, z]
79 # Normalize Y to 1.0, & add black glare
80 for j in range(3):
81 xyz[j] *= NORMALIZATION_FACTOR["Ynorm"]
82 xyz[j] = xyz[j] * (1.0 - ICX_INK_TABLE["K"][0][j]) + ICX_INK_TABLE["K"][0][j]
83 return tuple(xyz)
84
85
86 def rgb2xyz(r: float, g: float, b: float) -> tuple:
87 """Convert RGB to XYZ using the RGB to XYZ conversion.
88
89 from xcolorants.c -> icxColorantLu_to_XYZ
90
91 Args:
92 r (float): The red component of the RGB color.
93 g (float): The green component of the RGB color.
94 b (float): The blue component of the RGB color.
95
96 Returns:
97 tuple: A tuple containing the X, Y, and Z components of the XYZ color.
98 """
99 d = (r, g, b)
100 # We assume a simple additive model with gamma
101 xyz = [0.0, 0.0, 0.0]
102 for e in range(3):
103 v = d[e]
104 if v < 0.0:
105 v = 0.0
106 elif v > 1.0:
107 v = 1.0
108 if v <= 0.03928:
109 v /= 12.92
110 else:
111 v = math.pow((0.055 + v) / 1.055, 2.4) # Gamma
112 for j in range(3):
113 xyz[j] += v * ICX_INK_TABLE[IIX_CHANNEL_MAPPING["iix"][e]][0][j]
114 return xyz_normalize_add_glare(*xyz)
115
116
117 def xyz2rgb(x: float, y: float, z: float) -> tuple:
118 """Convert XYZ to RGB using the inverse of the RGB to XYZ conversion.
119
120 Args:
121 x (float): The X component of the XYZ color.
122 y (float): The Y component of the XYZ color.
123 z (float): The Z component of the XYZ color.
124
125 Returns:
126 tuple: A tuple containing the R, G, and B components of the RGB color.
127 """
128 return colormath.XYZ2RGB(*xyz_denormalize_remove_glare(x, y, z))