summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-09-20 18:19:39 -0700
committeryum <yum.food.vr@gmail.com>2024-09-20 18:19:39 -0700
commit66edcd749715cd1fa582c4c608b556d78c8c3f76 (patch)
treefa6b67275080b36cfe8686efc3c8b9a485d2dfac
parent325497faaca27356980fc9e5b9106a60c801fad6 (diff)
Fix bug in LRGB <-> XYZ conversion
Colors need to be clipped after the conversion. Also update license and fix slight rounding issues with weights.
-rw-r--r--oklab.cginc40
1 files changed, 17 insertions, 23 deletions
diff --git a/oklab.cginc b/oklab.cginc
index f6b788c..ca796de 100644
--- a/oklab.cginc
+++ b/oklab.cginc
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2023 yum_food
+ * Copyright (c) 2023-2024 yum_food
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
@@ -38,9 +38,8 @@ float3 LRGBtoXYZ(float3 c) {
float3x3 rgb_to_xyz = float3x3(
0.4124, 0.3576, 0.1805,
0.2126, 0.7152, 0.0722,
- 0.0193, 0.1192, 0.9505
- );
- return mul(rgb_to_xyz, c);
+ 0.0193, 0.1192, 0.9505);
+ return saturate(mul(rgb_to_xyz, c));
}
// Weights: https://en.wikipedia.org/wiki/SRGB
@@ -48,23 +47,20 @@ float3 XYZtoLRGB(float3 c) {
float3x3 xyz_to_rgb = float3x3(
3.24062548, -1.53720797, -0.4986286,
-0.96893071, 1.87575606, 0.04151752,
- 0.05571012, -0.20402105, 1.05699594
- );
- return mul(xyz_to_rgb, c);
+ 0.05571012, -0.20402105, 1.05699594);
+ return saturate(mul(xyz_to_rgb, c));
}
// Source: https://bottosson.github.io/posts/oklab/
float3 XYZtoOKLAB(float3 c) {
float3x3 m1 = float3x3(
- 0.8189, 0.3618, -0.1288,
- 0.0329, 0.9293, 0.0361,
- 0.0482, 0.2643, 0.6338
- );
+ 0.8189330101, 0.3618667424, -0.1288597137,
+ 0.0329845436, 0.9293118715, 0.0361456387,
+ 0.0482003018, 0.2643662691, 0.6338517070);
float3x3 m2 = float3x3(
- 0.2104, 0.7936, -0.0040,
- 1.9779, -2.4285, 0.4505,
- 0.0259, 0.7827, -0.8086
- );
+ 0.2104542553, 0.7936177850, -0.0040720468,
+ 1.9779984951, -2.4285922050, 0.4505937099,
+ 0.0259040371, 0.7827717662, -0.8086757660);
c = mul(m1, c);
c = pow(c, 0.33333333333);
return mul(m2, c);
@@ -73,15 +69,13 @@ float3 XYZtoOKLAB(float3 c) {
// Source: https://bottosson.github.io/posts/oklab/
float3 OKLABtoXYZ(float3 c) {
float3x3 m1i = float3x3(
- 1.22700842, -0.5576564 , 0.28111404,
- -0.04047048, 1.11219073, -0.07157255,
- -0.07643651, -0.42138367, 1.58625265
- );
+ 1.22701385, -0.55779998, 0.28125615,
+ -0.04058018, 1.11225687, -0.07167668,
+ -0.07638128, -0.42148198, 1.58616322);
float3x3 m2i = float3x3(
- 1.00003964, 0.39638005, 0.21589049,
- 0.99998945, -0.10553958, -0.06374665,
- 0.99999105, -0.08946276, -1.291495
- );
+ 1.00000000, 0.39633779, 0.21580376,
+ 1.00000001, -0.10556134, -0.06385417,
+ 1.00000005, -0.08948418, -1.29148554);
c = mul(m2i, c);
c = pow(c, 3);
return mul(m1i, c);