rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/tukan/color.sc -rw-r--r-- 2.7 KiB View raw Log this file
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
                                                                                
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
using import glm

fn hue2rgb (hue)
    "vec3 <- (f32)"
    clamp
        (abs ((((hue * 6.0) + (vec3 0 4 2)) % 6.0) - 3.0)) - 1.0
        \ 0.0 1.0

# maps n=0 to blue, n=1 to red, n=0.5 to green
fn normhue (n)
    "vec3 <- (f32)"
    hue2rgb ((2.0 / 3.0) * (1.0 - (clamp n 0.0 1.0)))

fn hsl2rgb (c)
    "vec3 <- (vec3)"
    + c.z
        * c.y
            (hue2rgb c.x) - 0.5
            1.0 - (abs (2.0 * c.z - 1.0))

fn hsv2rgb (c)
    "vec3 <- (vec3)"
    c.z * (mix (vec3 1) (hue2rgb c.x) c.y)

fn smooth-hsv2rgb (c)
    "vec3 <- (vec3)"
    let rgb = (hue2rgb c.x)
    # rgb = smoothstep(vec3(0.0),vec3(1.0),rgb);
    let rgb =
        * rgb rgb
            3.0 - 2.0 * rgb
    c.z * (mix (vec3 1) rgb c.y)

# ratio: 3 = neon, 4 = refracted, 5+ = approximate white
fn physhue2rgb (hue ratio)
    "vec3 <- (f32 f32)"
    smoothstep
        vec3 0
        vec3 1
        abs ((2.0 * ((hue + (vec3 0 1 2) * (1.0 / ratio)) % 1.0)) - 1.0)

# linear white point
let W = 11.2

# Filmic Reinhard, a simpler tonemapping operator with a single coefficient
    regulating the toe size.
    see also https://www.shadertoy.com/view/ldcSRN

# The operator ensures that f(0.5) = 0.5
# T = 0: no toe, classic Reinhard
let T = 0.01

fn filmic_reinhard_curve (x)
    let q = ((T + 1.0) * x * x)
    q / (q + x + T)

fn inverse_filmic_reinhard_curve (x)
    let q = (-2.0 * (T + 1.0) * (x - 1.0))
    (x + (sqrt (x * (x + 2.0 * T * q)))) / q

fn filmic_reinhard (x)
    "vec3 <- vec3"
    let w = (filmic_reinhard_curve W)
    /
        vec3
            filmic_reinhard_curve x.r
            filmic_reinhard_curve x.g
            filmic_reinhard_curve x.b
        w

fn inverse_filmic_reinhard (x)
    "vec3 <- vec3"
    let x = (x * (filmic_reinhard_curve W))
    vec3
        inverse_filmic_reinhard_curve x.r
        inverse_filmic_reinhard_curve x.g
        inverse_filmic_reinhard_curve x.b

let tonemap = filmic_reinhard
let inverse_tonemap = inverse_filmic_reinhard

fn... linear_srgb
    (x : f32)
        mix
            1.055 * (x ** (1.0 / 2.4)) - 0.055
            12.92 * x
            step x 0.0031308
    (x : vec3)
        mix
            1.055 * (x ** (vec3 (1.0 / 2.4))) - 0.055
            12.92 * x
            step x (vec3 0.0031308)

fn... srgb_linear
    (x : f32)
        mix
            ((x + 0.055) / 1.055) ** 2.4
            x / 12.92
            step x 0.04045
    (x : vec3)
        mix
            ((x + 0.055) / 1.055) ** (vec3 2.4)
            x / 12.92
            step x (vec3 0.04045)

do
    let
        hue2rgb
        normhue
        hsl2rgb
        hsv2rgb
        smooth-hsv2rgb
        physhue2rgb
        tonemap
        inverse_tonemap
        linear_srgb
        srgb_linear

    locals;