Blender V5.3
gpu_shader_common_color_utils.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9[[node]]
10void rgb_to_hsv(float4 rgb, float4 &outcol)
11{
12 float cmax, cmin, h, s, v, cdelta;
13 float3 c;
14
15 cmax = max(rgb[0], max(rgb[1], rgb[2]));
16 cmin = min(rgb[0], min(rgb[1], rgb[2]));
17 cdelta = cmax - cmin;
18
19 v = cmax;
20 if (cmax != 0.0f) {
21 s = cdelta / cmax;
22 }
23 else {
24 s = 0.0f;
25 h = 0.0f;
26 }
27
28 if (s == 0.0f) {
29 h = 0.0f;
30 }
31 else {
32 c = (float3(cmax) - rgb.xyz) / cdelta;
33
34 if (rgb.x == cmax) {
35 h = c[2] - c[1];
36 }
37 else if (rgb.y == cmax) {
38 h = 2.0f + c[0] - c[2];
39 }
40 else {
41 h = 4.0f + c[1] - c[0];
42 }
43
44 h /= 6.0f;
45
46 if (h < 0.0f) {
47 h += 1.0f;
48 }
49 }
50
51 outcol = float4(h, s, v, rgb.w);
52}
53
54[[node]]
55void hsv_to_rgb(float4 hsv, float4 &outcol)
56{
57 float i, f, p, q, t, h, s, v;
58 float3 rgb;
59
60 h = hsv[0];
61 s = hsv[1];
62 v = hsv[2];
63
64 if (s == 0.0f) {
65 rgb = float3(v, v, v);
66 }
67 else {
68 if (h == 1.0f) {
69 h = 0.0f;
70 }
71
72 h *= 6.0f;
73 i = floor(h);
74 f = h - i;
75 rgb = float3(f, f, f);
76 p = v * (1.0f - s);
77 q = v * (1.0f - (s * f));
78 t = v * (1.0f - (s * (1.0f - f)));
79
80 if (i == 0.0f) {
81 rgb = float3(v, t, p);
82 }
83 else if (i == 1.0f) {
84 rgb = float3(q, v, p);
85 }
86 else if (i == 2.0f) {
87 rgb = float3(p, v, t);
88 }
89 else if (i == 3.0f) {
90 rgb = float3(p, q, v);
91 }
92 else if (i == 4.0f) {
93 rgb = float3(t, p, v);
94 }
95 else {
96 rgb = float3(v, p, q);
97 }
98 }
99
100 outcol = float4(rgb, hsv.w);
101}
102
103[[node]]
104void rgb_to_hsl(float4 rgb, float4 &outcol)
105{
106 float cmax, cmin, h, s, l;
107
108 cmax = max(rgb[0], max(rgb[1], rgb[2]));
109 cmin = min(rgb[0], min(rgb[1], rgb[2]));
110 l = min(1.0f, (cmax + cmin) / 2.0f);
111
112 if (cmax == cmin) {
113 h = s = 0.0f; /* achromatic */
114 }
115 else {
116 float cdelta = cmax - cmin;
117 s = l > 0.5f ? cdelta / (2.0f - cmax - cmin) : cdelta / (cmax + cmin);
118 if (cmax == rgb[0]) {
119 h = (rgb[1] - rgb[2]) / cdelta + (rgb[1] < rgb[2] ? 6.0f : 0.0f);
120 }
121 else if (cmax == rgb[1]) {
122 h = (rgb[2] - rgb[0]) / cdelta + 2.0f;
123 }
124 else {
125 h = (rgb[0] - rgb[1]) / cdelta + 4.0f;
126 }
127 }
128 h /= 6.0f;
129
130 outcol = float4(h, s, l, rgb.w);
131}
132
133[[node]]
134void hsl_to_rgb(float4 hsl, float4 &outcol)
135{
136 float nr, ng, nb, chroma, h, s, l;
137
138 h = hsl[0];
139 s = hsl[1];
140 l = hsl[2];
141
142 nr = abs(h * 6.0f - 3.0f) - 1.0f;
143 ng = 2.0f - abs(h * 6.0f - 2.0f);
144 nb = 2.0f - abs(h * 6.0f - 4.0f);
145
146 nr = clamp(nr, 0.0f, 1.0f);
147 nb = clamp(nb, 0.0f, 1.0f);
148 ng = clamp(ng, 0.0f, 1.0f);
149
150 chroma = (1.0f - abs(2.0f * l - 1.0f)) * s;
151
152 outcol = float4(
153 (nr - 0.5f) * chroma + l, (ng - 0.5f) * chroma + l, (nb - 0.5f) * chroma + l, hsl.w);
154}
155
156/* ** YCCA to RGBA ** */
157
158[[node]]
160{
161 ycca.xyz *= 255.0f;
162 ycca.xyz -= float3(16.0f, 128.0f, 128.0f);
163 color.rgb = float3x3(1.164f, 1.164f, 1.164f, 0.0f, -0.392f, 2.017f, 1.596f, -0.813f, 0.0f) *
164 ycca.xyz;
165 color.rgb /= 255.0f;
166 color.a = ycca.a;
167}
168
169[[node]]
171{
172 ycca.xyz *= 255.0f;
173 ycca.xyz -= float3(16.0f, 128.0f, 128.0f);
174 color.rgb = float3x3(1.164f, 1.164f, 1.164f, 0.0f, -0.213f, 2.115f, 1.793f, -0.534f, 0.0f) *
175 ycca.xyz;
176 color.rgb /= 255.0f;
177 color.a = ycca.a;
178}
179
180[[node]]
182{
183 ycca.xyz *= 255.0f;
184 color.rgb = float3x3(1.0f, 1.0f, 1.0f, 0.0f, -0.34414f, 1.772f, 1.402f, -0.71414f, 0.0f) *
185 ycca.xyz;
186 color.rgb += float3(-179.456f, 135.45984f, -226.816f);
187 color.rgb /= 255.0f;
188 color.a = ycca.a;
189}
190
191/* ** RGBA to YCCA ** */
192
193[[node]]
195{
196 rgba.rgb *= 255.0f;
197 ycca.xyz = float3x3(0.257f, -0.148f, 0.439f, 0.504f, -0.291f, -0.368f, 0.098f, 0.439f, -0.071f) *
198 rgba.rgb;
199 ycca.xyz += float3(16.0f, 128.0f, 128.0f);
200 ycca.xyz /= 255.0f;
201 ycca.a = rgba.a;
202}
203
204[[node]]
206{
207 rgba.rgb *= 255.0f;
208 ycca.xyz = float3x3(0.183f, -0.101f, 0.439f, 0.614f, -0.338f, -0.399f, 0.062f, 0.439f, -0.040f) *
209 rgba.rgb;
210 ycca.xyz += float3(16.0f, 128.0f, 128.0f);
211 ycca.xyz /= 255.0f;
212 ycca.a = rgba.a;
213}
214
215[[node]]
217{
218 rgba.rgb *= 255.0f;
219 ycca.xyz = float3x3(
220 0.299f, -0.16874f, 0.5f, 0.587f, -0.33126f, -0.41869f, 0.114f, 0.5f, -0.08131f) *
221 rgba.rgb;
222 ycca.xyz += float3(0.0f, 128.0f, 128.0f);
223 ycca.xyz /= 255.0f;
224 ycca.a = rgba.a;
225}
226
227/* ** YUVA to RGBA ** */
228
229[[node]]
231{
232 color.rgb = float3x3(1.0f, 1.0f, 1.0f, 0.0f, -0.21482f, 2.12798f, 1.28033f, -0.38059f, 0.0f) *
233 yuva.xyz;
234 color.a = yuva.a;
235}
236
237/* ** RGBA to YUVA ** */
238
239[[node]]
241{
242 yuva.xyz =
243 float3x3(
244 0.2126f, -0.09991f, 0.615f, 0.7152f, -0.33609f, -0.55861f, 0.0722f, 0.436f, -0.05639f) *
245 rgba.rgb;
246 yuva.a = rgba.a;
247}
248
249/* ** Alpha Handling ** */
250
251[[node]]
252void color_alpha_clear(float4 color, float4 &result)
253{
254 result = float4(color.rgb, 1.0f);
255}
256
257[[node]]
259{
260 result = float4(color.rgb * color.a, color.a);
261}
262
263[[node]]
265{
266 if (color.a == 0.0f || color.a == 1.0f) {
267 result = color;
268 }
269 else {
270 result = float4(color.rgb / color.a, color.a);
271 }
272}
273
274float linear_rgb_to_srgb(float color)
275{
276 if (color < 0.0031308f) {
277 return (color < 0.0f) ? 0.0f : color * 12.92f;
278 }
279
280 return 1.055f * pow(color, 1.0f / 2.4f) - 0.055f;
281}
282
284{
285 return float3(
286 linear_rgb_to_srgb(color.r), linear_rgb_to_srgb(color.g), linear_rgb_to_srgb(color.b));
287}
288
289float srgb_to_linear_rgb(float color)
290{
291 if (color < 0.04045f) {
292 return (color < 0.0f) ? 0.0f : color * (1.0f / 12.92f);
293 }
294
295 return pow((color + 0.055f) * (1.0f / 1.055f), 2.4f);
296}
297
299{
300 return float3(
301 srgb_to_linear_rgb(color.r), srgb_to_linear_rgb(color.g), srgb_to_linear_rgb(color.b));
302}
303
304float get_luminance(float3 color, float3 luminance_coefficients)
305{
306 return dot(color, luminance_coefficients);
307}
btScalar max
void color_alpha_unpremultiply(float4 color, float4 &result)
float linear_rgb_to_srgb(float color)
void hsl_to_rgb(float4 hsl, float4 &outcol)
float srgb_to_linear_rgb(float color)
void ycca_to_rgba_jpeg(float4 ycca, float4 &color)
void rgba_to_ycca_itu_601(float4 rgba, float4 &ycca)
void rgb_to_hsv(float4 rgb, float4 &outcol)
void rgba_to_yuva_itu_709(float4 rgba, float4 &yuva)
void color_alpha_clear(float4 color, float4 &result)
void rgba_to_ycca_itu_709(float4 rgba, float4 &ycca)
float get_luminance(float3 color, float3 luminance_coefficients)
void hsv_to_rgb(float4 hsv, float4 &outcol)
void ycca_to_rgba_itu_601(float4 ycca, float4 &color)
void rgba_to_ycca_jpeg(float4 rgba, float4 &ycca)
void ycca_to_rgba_itu_709(float4 ycca, float4 &color)
void color_alpha_premultiply(float4 color, float4 &result)
void rgb_to_hsl(float4 rgb, float4 &outcol)
void yuva_to_rgba_itu_709(float4 yuva, float4 &color)
#define pow
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
constexpr T clamp(T, U, U) RET
#define min(a, b)
Definition sort.cc:38
float y
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225