Skip to content

Commit 742e4eb

Browse files
Update jedypod
using oklab, a little better than cie-lab, but still not mathematically correct.
1 parent c687f19 commit 742e4eb

File tree

1 file changed

+36
-57
lines changed

1 file changed

+36
-57
lines changed

gamut-mapping/jedypod.glsl

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -126,84 +126,63 @@ vec3 XYZ_to_RGB(vec3 XYZ) {
126126
return XYZ * M;
127127
}
128128

129-
vec3 XYZD65_to_XYZD50(vec3 XYZ) {
129+
vec3 XYZ_to_LMS(vec3 XYZ) {
130130
mat3 M = mat3(
131-
1.0479298208405488, 0.022946793341019088, -0.05019222954313557,
132-
0.029627815688159344, 0.990434484573249, -0.01707382502938514,
133-
-0.009243058152591178, 0.015055144896577895, 0.7518742899580008);
131+
0.8190224432164319, 0.3619062562801221, -0.12887378261216414,
132+
0.0329836671980271, 0.9292868468965546, 0.03614466816999844,
133+
0.048177199566046255, 0.26423952494422764, 0.6335478258136937);
134134
return XYZ * M;
135135
}
136136

137-
vec3 XYZD50_to_XYZD65(vec3 XYZ) {
137+
vec3 LMS_to_XYZ(vec3 LMS) {
138138
mat3 M = mat3(
139-
0.9554734527042182, -0.023098536874261423, 0.0632593086610217,
140-
-0.028369706963208136, 1.0099954580058226, 0.021041398966943008,
141-
0.012314001688319899, -0.020507696433477912, 1.3303659366080753);
142-
return XYZ * M;
143-
}
144-
145-
float delta = 6.0 / 29.0;
146-
float deltac = delta * 2.0 / 3.0;
147-
148-
float f1(float x, float delta) {
149-
return x > pow(delta, 3.0) ?
150-
cbrt(x) :
151-
deltac + x / (3.0 * pow(delta, 2.0));
139+
1.2268798733741557, -0.5578149965554813, 0.28139105017721583,
140+
-0.04057576262431372, 1.1122868293970594, -0.07171106666151701,
141+
-0.07637294974672142, -0.4214933239627914, 1.5869240244272418);
142+
return LMS * M;
152143
}
153144

154-
float f2(float x, float delta) {
155-
return x > delta ?
156-
pow(x, 3.0) :
157-
(x - deltac) * (3.0 * pow(delta, 2.0));
158-
}
159-
160-
vec3 XYZ_ref = RGB_to_XYZ(vec3(L_sdr));
161-
162-
vec3 XYZ_to_Lab(vec3 XYZ) {
163-
float X = XYZ.x;
164-
float Y = XYZ.y;
165-
float Z = XYZ.z;
166-
167-
X = f1(X / XYZ_ref.x, delta);
168-
Y = f1(Y / XYZ_ref.y, delta);
169-
Z = f1(Z / XYZ_ref.z, delta);
170-
171-
float L = 116.0 * Y - 16.0;
172-
float a = 500.0 * (X - Y);
173-
float b = 200.0 * (Y - Z);
145+
vec3 LMS_to_Lab(vec3 LMS) {
146+
mat3 M = mat3(
147+
0.2104542553, 0.7936177850, -0.0040720468,
148+
1.9779984951, -2.4285922050, 0.4505937099,
149+
0.0259040371, 0.7827717662, -0.8086757660);
150+
151+
LMS = vec3(
152+
cbrt(LMS.x),
153+
cbrt(LMS.y),
154+
cbrt(LMS.z)
155+
);
174156

175-
return vec3(L, a, b);
157+
return LMS * M;
176158
}
177159

178-
vec3 Lab_to_XYZ(vec3 Lab) {
179-
float L = Lab.x;
180-
float a = Lab.y;
181-
float b = Lab.z;
182-
183-
float Y = (L + 16.0) / 116.0;
184-
float X = Y + a / 500.0;
185-
float Z = Y - b / 200.0;
160+
vec3 Lab_to_LMS(vec3 Lab) {
161+
mat3 M = mat3(
162+
0.99999999845051981432, 0.39633779217376785678, 0.21580375806075880339,
163+
1.0000000088817607767, -0.1055613423236563494, -0.063854174771705903402,
164+
1.0000000546724109177, -0.089484182094965759684, -1.2914855378640917399);
186165

187-
X = f2(X, delta) * XYZ_ref.x;
188-
Y = f2(Y, delta) * XYZ_ref.y;
189-
Z = f2(Z, delta) * XYZ_ref.z;
166+
Lab = Lab * M;
190167

191-
return vec3(X, Y, Z);
168+
return vec3(
169+
pow(Lab.x, 3.0),
170+
pow(Lab.y, 3.0),
171+
pow(Lab.z, 3.0)
172+
);
192173
}
193174

194175
vec3 RGB_to_Lab(vec3 color) {
195-
color *= L_sdr;
196176
color = RGB_to_XYZ(color);
197-
color = XYZD65_to_XYZD50(color);
198-
color = XYZ_to_Lab(color);
177+
color = XYZ_to_LMS(color);
178+
color = LMS_to_Lab(color);
199179
return color;
200180
}
201181

202182
vec3 Lab_to_RGB(vec3 color) {
203-
color = Lab_to_XYZ(color);
204-
color = XYZD50_to_XYZD65(color);
183+
color = Lab_to_LMS(color);
184+
color = LMS_to_XYZ(color);
205185
color = XYZ_to_RGB(color);
206-
color /= L_sdr;
207186
return color;
208187
}
209188

0 commit comments

Comments
 (0)