1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Legacy colour conversion helper functions for CIE 1931 xy-based colourspaces
//!
//! **DEPRECATED**: These colourspaces are based on CIE 1931 xy chromaticity coordinates and are permanently tied to the 1931 Standard Observer. Use spectral colourspaces (VSF RGB, Rec.2020) instead for future-proof colour workflows.
// ==================== BT.709/Rec.709 CONSTANTS AND FUNCTIONS ====================
const BT709_LINEAR_THRESHOLD: f32 = 0.018;
const BT709_LINEAR_COEFF: f32 = 4.5;
const BT709_GAMMA_A: f32 = 0.099;
const BT709_GAMMA_DIVISOR: f32 = 1.099;
const BT709_GAMMA_EXPONENT: f32 = 0.45; // Inverse is ~2.222
/// Linearize a BT.709-encoded value (0-1 range)
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// BT.709 OETF (encoding, camera):
/// - Linear: V = 4.5 * L for L < 0.018
/// - Gamma: V = 1.099 * L^0.45 - 0.099 for L >= 0.018
/// Encode a linear value to BT.709
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
// ==================== sRGB CONSTANTS AND FUNCTIONS ====================
const SRGB_LINEAR_THRESHOLD: f32 = 0.0031308;
const SRGB_LINEAR_COEFF: f32 = 12.92;
const SRGB_GAMMA_A: f32 = 0.055;
const SRGB_GAMMA_DIVISOR: f32 = 1.055;
const SRGB_GAMMA_EXPONENT: f32 = 2.4;
/// Linearize an sRGB-encoded value (0-1 range) using the specification piecewise function
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// sRGB transfer function:
/// - Linear: `C_srgb / 12.92` for `C_srgb <= 0.04045`
/// - Gamma: `((C_srgb + 0.055) / 1.055)^2.4` for `C_srgb > 0.04045`
///
/// Note: The threshold 0.04045 is the encoded value; the linear threshold is 0.0031308
/// Delinearize a linear value to sRGB-encoded (0-1 range) using the specification piecewise function
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Inverse sRGB transfer function:
/// - Linear: `C_linear * 12.92` for `C_linear <= 0.0031308`
/// - Gamma: `1.055 * C_linear^(1/2.4) - 0.055` for `C_linear > 0.0031308`
// ==================== sRGB u8/u16 QUANTIZATION FUNCTIONS ====================
/// Linearize an 8-bit sRGB-encoded value (spec-compliant, asymmetric)
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts 0-255 sRGB to linear 0-1 using the sRGB specification.
///
/// **WARNING: This follows the sRGB spec which is asymmetric:**
/// - Encoding uses ×255 + round (bucket centers)
/// - Decoding uses ÷255 (bucket bottoms)
///
/// This asymmetry means encode→decode roundtrip has inherent error.
/// Delinearize a linear value to 8-bit sRGB (spec-compliant, asymmetric)
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts linear 0-1 to 0-255 sRGB using the sRGB specification.
///
/// **WARNING: This follows the sRGB spec which is asymmetric:**
/// - Encoding uses ×255 + round (bucket centers) ← YOU ARE HERE
/// - Decoding uses ÷255 (bucket bottoms)
///
/// Note the expensive `.round()` call which costs 5-30 cycles.
/// Linearize a 16-bit sRGB-encoded value (spec-compliant, asymmetric)
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts 0-65535 sRGB to linear 0-1 using the sRGB specification.
///
/// **WARNING: This follows the sRGB spec which is slow and asymmetric:**
/// - Encoding uses ×65535 + round (bucket centers)
/// - Decoding uses ÷65535 (bucket bottoms, actual division!) ← YOU ARE HERE
/// Delinearize a linear value to 16-bit sRGB (spec-compliant, asymmetric)
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts linear 0-1 to 0-65535 sRGB using the sRGB specification.
///
/// **WARNING: This follows the sRGB spec which is asymmetric:**
/// - Encoding uses ×65535 + round (bucket centers) ← YOU ARE HERE
/// - Decoding uses ÷65535 (bucket bottoms)
///
/// Note the expensive `.round()` call which costs 5-30 cycles.
// ==================== BT.709/BT.2020 STUDIO RANGE FUNCTIONS ====================
/// Linearize an 8-bit BT.709-encoded value (studio range: 16-235)
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts studio range 16-235 to linear 0-1 using BT.709 transfer function.
///
/// **Studio range mapping:**
/// - 16 (video black) → 0.0 linear
/// - 235 (video white) → 1.0 linear
/// - Values outside [16, 235] are clamped
/// Encode a linear value to 8-bit BT.709 (studio range: 16-235)
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts linear 0-1 to studio range 16-235 using BT.709 transfer function.
///
/// **Studio range mapping:**
/// - 0.0 linear → 16 (video black)
/// - 1.0 linear → 235 (video white)
/// - Output clamped to [16, 235]
/// Linearize a 16-bit BT.709-encoded value (studio range: 4096-60160)
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts studio range 4096-60160 to linear 0-1 using BT.709 transfer function.
///
/// **Studio range mapping:**
/// - 4096 (video black) → 0.0 linear
/// - 60160 (video white) → 1.0 linear
/// - Values outside [4096, 60160] are clamped
/// Encode a linear value to 16-bit BT.709 (studio range: 4096-60160)
///
/// **DEPRECATED**: Rec.709 primaries are based on CIE 1931 xy chromaticity (legacy).
///
/// Converts linear 0-1 to studio range 4096-60160 using BT.709 transfer function.
///
/// **Studio range mapping:**
/// - 0.0 linear → 4096 (video black)
/// - 1.0 linear → 60160 (video white)
/// - Output clamped to [4096, 60160]
// ==================== sRGB RGB TRIPLE HELPERS ====================
/// Linearize an sRGB RGB triple (8-bit per channel)
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).
/// Delinearize a linear RGB triple to 8-bit sRGB
///
/// **DEPRECATED**: sRGB primaries are based on CIE 1931 xy chromaticity (legacy).