yum/HLSL_OKLAB

HLSL conversion functions between LRGB and OKLAB/OKLCH, a perceptually uniform color space.

git clone https://git.yummers.dev/yum/HLSL_OKLAB

yumFix bug in LRGB <-> XYZ conversion66edcd7

main
3.5 KiB116 linesraw
1/*
2 * MIT License
3 *
4 * Copyright (c) 2023-2024 yum_food
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE
26 * SOFTWARE.
27 */
28
29#ifndef __OKLAB_INC
30#define __OKLAB_INC
31
32// Utilities relating to the OKLAB color space, as defined here:
33//   https://bottosson.github.io/posts/oklab/
34// Code is derived from the samples there, which are in the public domain.
35
36// Weights: https://en.wikipedia.org/wiki/SRGB
37float3 LRGBtoXYZ(float3 c) {
38  float3x3 rgb_to_xyz = float3x3(
39      0.4124, 0.3576, 0.1805,
40      0.2126, 0.7152, 0.0722,
41      0.0193, 0.1192, 0.9505);
42  return saturate(mul(rgb_to_xyz, c));
43}
44
45// Weights: https://en.wikipedia.org/wiki/SRGB
46float3 XYZtoLRGB(float3 c) {
47  float3x3 xyz_to_rgb = float3x3(
48      3.24062548, -1.53720797, -0.4986286,
49      -0.96893071,  1.87575606,  0.04151752,
50      0.05571012, -0.20402105,  1.05699594);
51  return saturate(mul(xyz_to_rgb, c));
52}
53
54// Source: https://bottosson.github.io/posts/oklab/
55float3 XYZtoOKLAB(float3 c) {
56  float3x3 m1 = float3x3(
57      0.8189330101, 0.3618667424, -0.1288597137,
58      0.0329845436, 0.9293118715, 0.0361456387,
59      0.0482003018, 0.2643662691, 0.6338517070);
60  float3x3 m2 = float3x3(
61      0.2104542553, 0.7936177850, -0.0040720468,
62      1.9779984951, -2.4285922050, 0.4505937099,
63      0.0259040371, 0.7827717662, -0.8086757660);
64  c = mul(m1, c);
65  c = pow(c, 0.33333333333);
66  return mul(m2, c);
67}
68
69// Source: https://bottosson.github.io/posts/oklab/
70float3 OKLABtoXYZ(float3 c) {
71  float3x3 m1i = float3x3(
72       1.22701385, -0.55779998,  0.28125615,
73      -0.04058018,  1.11225687, -0.07167668,
74      -0.07638128, -0.42148198,  1.58616322);
75  float3x3 m2i = float3x3(
76      1.00000000,  0.39633779,  0.21580376,
77      1.00000001, -0.10556134, -0.06385417,
78      1.00000005, -0.08948418, -1.29148554);
79  c = mul(m2i, c);
80  c = pow(c, 3);
81  return mul(m1i, c);
82}
83
84// Source: https://bottosson.github.io/posts/oklab/
85float3 OKLABtoOKLCH(float3 c) {
86  float c_ = length(c.yz);
87  float h_ = atan2(c.z, c.y);
88  return float3(c.x, c_, h_);
89}
90
91// Source: https://bottosson.github.io/posts/oklab/
92// Note: hue must be in units of radians.
93float3 OKLCHtoOKLAB(float3 c) {
94  float a = c.y * cos(c.z);
95  float b = c.y * sin(c.z);
96  return float3(c.x, a, b);
97}
98
99float3 LRGBtoOKLAB(float3 c) {
100  return XYZtoOKLAB(LRGBtoXYZ(c));
101}
102
103float3 OKLABtoLRGB(float3 c) {
104  return XYZtoLRGB(OKLABtoXYZ(c));
105}
106
107float3 LRGBtoOKLCH(float3 c) {
108  return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c)));
109}
110
111float3 OKLCHtoLRGB(float3 c) {
112  return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c)));
113}
114
115#endif  // __OKLAB_INC
116