Blender V5.0
node_color_blend.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5color node_mix_blend(float t, color col1, color col2)
6{
7 return mix(col1, col2, t);
8}
9
10color node_mix_add(float t, color col1, color col2)
11{
12 return mix(col1, col1 + col2, t);
13}
14
15color node_mix_mul(float t, color col1, color col2)
16{
17 return mix(col1, col1 * col2, t);
18}
19
20color node_mix_screen(float t, color col1, color col2)
21{
22 float tm = 1.0 - t;
23
24 return color(1.0) - (color(tm) + t * (color(1.0) - col2)) * (color(1.0) - col1);
25}
26
27color node_mix_overlay(float t, color col1, color col2)
28{
29 float tm = 1.0 - t;
30
31 color outcol = col1;
32
33 if (outcol[0] < 0.5) {
34 outcol[0] *= tm + 2.0 * t * col2[0];
35 }
36 else {
37 outcol[0] = 1.0 - (tm + 2.0 * t * (1.0 - col2[0])) * (1.0 - outcol[0]);
38 }
39
40 if (outcol[1] < 0.5) {
41 outcol[1] *= tm + 2.0 * t * col2[1];
42 }
43 else {
44 outcol[1] = 1.0 - (tm + 2.0 * t * (1.0 - col2[1])) * (1.0 - outcol[1]);
45 }
46
47 if (outcol[2] < 0.5) {
48 outcol[2] *= tm + 2.0 * t * col2[2];
49 }
50 else {
51 outcol[2] = 1.0 - (tm + 2.0 * t * (1.0 - col2[2])) * (1.0 - outcol[2]);
52 }
53
54 return outcol;
55}
56
57color node_mix_sub(float t, color col1, color col2)
58{
59 return mix(col1, col1 - col2, t);
60}
61
62color node_mix_div(float t, color col1, color col2)
63{
64 float tm = 1.0 - t;
65
66 color outcol = col1;
67
68 if (col2[0] != 0.0) {
69 outcol[0] = tm * outcol[0] + t * outcol[0] / col2[0];
70 }
71 if (col2[1] != 0.0) {
72 outcol[1] = tm * outcol[1] + t * outcol[1] / col2[1];
73 }
74 if (col2[2] != 0.0) {
75 outcol[2] = tm * outcol[2] + t * outcol[2] / col2[2];
76 }
77
78 return outcol;
79}
80
81color node_mix_diff(float t, color col1, color col2)
82{
83 return mix(col1, abs(col1 - col2), t);
84}
85
86color node_mix_exclusion(float t, color col1, color col2)
87{
88 return max(mix(col1, col1 + col2 - 2.0 * col1 * col2, t), 0.0);
89}
90
91color node_mix_dark(float t, color col1, color col2)
92{
93 return mix(col1, min(col1, col2), t);
94}
95
96color node_mix_light(float t, color col1, color col2)
97{
98 return mix(col1, max(col1, col2), t);
99}
100
101color node_mix_dodge(float t, color col1, color col2)
102{
103 color outcol = col1;
104
105 if (outcol[0] != 0.0) {
106 float tmp = 1.0 - t * col2[0];
107 if (tmp <= 0.0) {
108 outcol[0] = 1.0;
109 }
110 else if ((tmp = outcol[0] / tmp) > 1.0) {
111 outcol[0] = 1.0;
112 }
113 else {
114 outcol[0] = tmp;
115 }
116 }
117 if (outcol[1] != 0.0) {
118 float tmp = 1.0 - t * col2[1];
119 if (tmp <= 0.0) {
120 outcol[1] = 1.0;
121 }
122 else if ((tmp = outcol[1] / tmp) > 1.0) {
123 outcol[1] = 1.0;
124 }
125 else {
126 outcol[1] = tmp;
127 }
128 }
129 if (outcol[2] != 0.0) {
130 float tmp = 1.0 - t * col2[2];
131 if (tmp <= 0.0) {
132 outcol[2] = 1.0;
133 }
134 else if ((tmp = outcol[2] / tmp) > 1.0) {
135 outcol[2] = 1.0;
136 }
137 else {
138 outcol[2] = tmp;
139 }
140 }
141
142 return outcol;
143}
144
145color node_mix_burn(float t, color col1, color col2)
146{
147 float tmp, tm = 1.0 - t;
148
149 color outcol = col1;
150
151 tmp = tm + t * col2[0];
152 if (tmp <= 0.0) {
153 outcol[0] = 0.0;
154 }
155 else if ((tmp = (1.0 - (1.0 - outcol[0]) / tmp)) < 0.0) {
156 outcol[0] = 0.0;
157 }
158 else if (tmp > 1.0) {
159 outcol[0] = 1.0;
160 }
161 else {
162 outcol[0] = tmp;
163 }
164
165 tmp = tm + t * col2[1];
166 if (tmp <= 0.0) {
167 outcol[1] = 0.0;
168 }
169 else if ((tmp = (1.0 - (1.0 - outcol[1]) / tmp)) < 0.0) {
170 outcol[1] = 0.0;
171 }
172 else if (tmp > 1.0) {
173 outcol[1] = 1.0;
174 }
175 else {
176 outcol[1] = tmp;
177 }
178
179 tmp = tm + t * col2[2];
180 if (tmp <= 0.0) {
181 outcol[2] = 0.0;
182 }
183 else if ((tmp = (1.0 - (1.0 - outcol[2]) / tmp)) < 0.0) {
184 outcol[2] = 0.0;
185 }
186 else if (tmp > 1.0) {
187 outcol[2] = 1.0;
188 }
189 else {
190 outcol[2] = tmp;
191 }
192
193 return outcol;
194}
195
196color node_mix_hue(float t, color col1, color col2)
197{
198 color outcol = col1;
199 color hsv2 = rgb_to_hsv(col2);
200
201 if (hsv2[1] != 0.0) {
202 color hsv = rgb_to_hsv(outcol);
203 hsv[0] = hsv2[0];
204 color tmp = hsv_to_rgb(hsv);
205
206 outcol = mix(outcol, tmp, t);
207 }
208
209 return outcol;
210}
211
212color node_mix_sat(float t, color col1, color col2)
213{
214 float tm = 1.0 - t;
215
216 color outcol = col1;
217
218 color hsv = rgb_to_hsv(outcol);
219
220 if (hsv[1] != 0.0) {
221 color hsv2 = rgb_to_hsv(col2);
222
223 hsv[1] = tm * hsv[1] + t * hsv2[1];
224 outcol = hsv_to_rgb(hsv);
225 }
226
227 return outcol;
228}
229
230color node_mix_val(float t, color col1, color col2)
231{
232 float tm = 1.0 - t;
233
234 color hsv = rgb_to_hsv(col1);
235 color hsv2 = rgb_to_hsv(col2);
236
237 hsv[2] = tm * hsv[2] + t * hsv2[2];
238
239 return hsv_to_rgb(hsv);
240}
241
242color node_mix_color(float t, color col1, color col2)
243{
244 color outcol = col1;
245 color hsv2 = rgb_to_hsv(col2);
246
247 if (hsv2[1] != 0.0) {
248 color hsv = rgb_to_hsv(outcol);
249 hsv[0] = hsv2[0];
250 hsv[1] = hsv2[1];
251 color tmp = hsv_to_rgb(hsv);
252
253 outcol = mix(outcol, tmp, t);
254 }
255
256 return outcol;
257}
258
259color node_mix_soft(float t, color col1, color col2)
260{
261 float tm = 1.0 - t;
262
263 color one = color(1.0);
264 color scr = one - (one - col2) * (one - col1);
265
266 return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr);
267}
268
269color node_mix_linear(float t, color col1, color col2)
270{
271 color outcol = col1;
272
273 if (col2[0] > 0.5) {
274 outcol[0] = col1[0] + t * (2.0 * (col2[0] - 0.5));
275 }
276 else {
277 outcol[0] = col1[0] + t * (2.0 * (col2[0]) - 1.0);
278 }
279
280 if (col2[1] > 0.5) {
281 outcol[1] = col1[1] + t * (2.0 * (col2[1] - 0.5));
282 }
283 else {
284 outcol[1] = col1[1] + t * (2.0 * (col2[1]) - 1.0);
285 }
286
287 if (col2[2] > 0.5) {
288 outcol[2] = col1[2] + t * (2.0 * (col2[2] - 0.5));
289 }
290 else {
291 outcol[2] = col1[2] + t * (2.0 * (col2[2]) - 1.0);
292 }
293
294 return outcol;
295}
296
297color node_mix_clamp(color col)
298{
299 color outcol = col;
300
301 outcol[0] = clamp(col[0], 0.0, 1.0);
302 outcol[1] = clamp(col[1], 0.0, 1.0);
303 outcol[2] = clamp(col[2], 0.0, 1.0);
304
305 return outcol;
306}
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:21
uint col
#define abs
constexpr T clamp(T, U, U) RET
color node_mix_add(float t, color col1, color col2)
color node_mix_dark(float t, color col1, color col2)
color node_mix_clamp(color col)
color node_mix_div(float t, color col1, color col2)
color node_mix_sat(float t, color col1, color col2)
color node_mix_linear(float t, color col1, color col2)
color node_mix_hue(float t, color col1, color col2)
color node_mix_exclusion(float t, color col1, color col2)
color node_mix_color(float t, color col1, color col2)
color node_mix_screen(float t, color col1, color col2)
color node_mix_soft(float t, color col1, color col2)
color node_mix_val(float t, color col1, color col2)
color node_mix_light(float t, color col1, color col2)
color node_mix_overlay(float t, color col1, color col2)
color node_mix_mul(float t, color col1, color col2)
color node_mix_burn(float t, color col1, color col2)
color node_mix_sub(float t, color col1, color col2)
color node_mix_dodge(float t, color col1, color col2)
color node_mix_diff(float t, color col1, color col2)
color node_mix_blend(float t, color col1, color col2)
#define mix
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251