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