Repository navigation
Expand file tree
/
Copy pathHEQ.hlsl
More file actions
210 lines (174 loc) · 4.48 KB
/
Copy pathHEQ.hlsl
File metadata and controls
210 lines (174 loc) · 4.48 KB
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
//!MAGPIE EFFECT
//!VERSION 4
//!PARAMETER
//!LABEL Brightness
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float brightness;
//!PARAMETER
//!LABEL Contrast
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float contrast;
//!PARAMETER
//!LABEL Saturation
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float saturation;
//!PARAMETER
//!LABEL Hue
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float hue;
//!PARAMETER
//!LABEL Gamma
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float gamma;
//!PARAMETER
//!LABEL Temperature
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float temperature;
//!PARAMETER
//!LABEL Channel Red
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float red;
//!PARAMETER
//!LABEL Channel Green
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float green;
//!PARAMETER
//!LABEL Channel Blue
//!DEFAULT 0
//!MIN -100
//!MAX 100
//!STEP 0.5
float blue;
//!PARAMETER
//!LABEL Invert
//!DEFAULT 0
//!MIN 0
//!MAX 1
//!STEP 1
int invertColors;
//!TEXTURE
Texture2D INPUT;
//!TEXTURE
//!WIDTH INPUT_WIDTH
//!HEIGHT INPUT_HEIGHT
Texture2D OUTPUT;
//!SAMPLER
//!FILTER POINT
SamplerState sam;
//!PASS 1
//!DESC HEQ color adjustment
//!STYLE PS
//!IN INPUT
//!OUT OUTPUT
float3 SrgbToLinear(float3 color) {
float3 low = color / 12.92;
float3 high = pow(max((color + 0.055) / 1.055, 0.0), 2.4);
return lerp(low, high, step(float3(0.04045, 0.04045, 0.04045), color));
}
float3 LinearToSrgb(float3 color) {
color = max(color, 0.0);
float3 low = color * 12.92;
float3 high = 1.055 * pow(color, 1.0 / 2.4) - 0.055;
return lerp(low, high, step(float3(0.0031308, 0.0031308, 0.0031308), color));
}
float3 LinearToOklab(float3 color) {
float l = 0.4122214708 * color.r + 0.5363325363 * color.g + 0.0514459929 * color.b;
float m = 0.2119034982 * color.r + 0.6806995451 * color.g + 0.1073969566 * color.b;
float s = 0.0883024619 * color.r + 0.2817188376 * color.g + 0.6299787005 * color.b;
l = pow(max(l, 0.0), 1.0 / 3.0);
m = pow(max(m, 0.0), 1.0 / 3.0);
s = pow(max(s, 0.0), 1.0 / 3.0);
return float3(
0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s,
0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
);
}
float3 OklabToLinear(float3 color) {
float l = color.x + 0.3963377774 * color.y + 0.2158037573 * color.z;
float m = color.x - 0.1055613458 * color.y - 0.0638541728 * color.z;
float s = color.x - 0.0894841775 * color.y - 1.2914855480 * color.z;
l = l * l * l;
m = m * m * m;
s = s * s * s;
return float3(
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
);
}
float4 Pass1(float2 pos) {
float4 source = INPUT.SampleLevel(sam, pos, 0);
bool isNeutral = brightness == 0.0 &&
contrast == 0.0 &&
saturation == 0.0 &&
hue == 0.0 &&
gamma == 0.0 &&
temperature == 0.0 &&
red == 0.0 &&
green == 0.0 &&
blue == 0.0;
if (isNeutral) {
float3 neutral = invertColors != 0 ? 1.0 - source.rgb : source.rgb;
return float4(neutral, source.a);
}
float3 color = SrgbToLinear(source.rgb);
float temperatureAmount = temperature * 0.01;
float3 temperatureGain = exp2(temperatureAmount * float3(0.5, 0.0, -0.5));
temperatureGain /= dot(temperatureGain, float3(0.2126, 0.7152, 0.0722));
float3 channelGain = max(1.0 + float3(red, green, blue) * 0.01, 0.0);
color *= temperatureGain * channelGain;
float3 lab = LinearToOklab(color);
float contrastFactor = max(1.0 + contrast * 0.01, 0.0);
lab.x = (lab.x - 0.5) * contrastFactor + 0.5;
float brightnessAmount = brightness * 0.01;
if (brightness <= -100.0) {
lab = 0.0;
} else if (brightness >= 100.0) {
lab = float3(1.0, 0.0, 0.0);
} else if (brightnessAmount < 0.0) {
lab *= 1.0 + brightnessAmount;
} else {
lab = lerp(lab, float3(1.0, 0.0, 0.0), brightnessAmount);
}
float hueAngle = hue * 0.031415926535897932;
float hueSin;
float hueCos;
sincos(hueAngle, hueSin, hueCos);
lab.yz = float2(
hueCos * lab.y - hueSin * lab.z,
hueSin * lab.y + hueCos * lab.z
);
lab.yz *= max(1.0 + saturation * 0.01, 0.0);
color = LinearToSrgb(OklabToLinear(lab));
color = pow(max(color, 0.0), exp2(gamma * 0.02));
color = saturate(color);
if (invertColors != 0) {
color = 1.0 - color;
}
return float4(color, source.a);
}