Blender V5.0
node_color.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/* TODO(lukas): Fix colors in OSL. */
6
8{
9 if (c < 0.04045) {
10 return (c < 0.0) ? 0.0 : c * (1.0 / 12.92);
11 }
12 else {
13 return pow((c + 0.055) * (1.0 / 1.055), 2.4);
14 }
15}
16
18{
19 if (c < 0.0031308) {
20 return (c < 0.0) ? 0.0 : c * 12.92;
21 }
22 else {
23 return 1.055 * pow(c, 1.0 / 2.4) - 0.055;
24 }
25}
26
33
40
41color color_unpremultiply(color c, float alpha)
42{
43 if (alpha != 1.0 && alpha != 0.0) {
44 return c / alpha;
45 }
46
47 return c;
48}
49
50/* Color Operations */
51
52color xyY_to_xyz(float x, float y, float Y)
53{
54 float X, Z;
55
56 if (y != 0.0) {
57 X = (x / y) * Y;
58 }
59 else {
60 X = 0.0;
61 }
62
63 if (y != 0.0 && Y != 0.0) {
64 Z = ((1.0 - x - y) / y) * Y;
65 }
66 else {
67 Z = 0.0;
68 }
69
70 return color(X, Y, Z);
71}
72
73color xyz_to_rgb(float x, float y, float z)
74{
75 return color(3.240479 * x + -1.537150 * y + -0.498535 * z,
76 -0.969256 * x + 1.875991 * y + 0.041556 * z,
77 0.055648 * x + -0.204043 * y + 1.057311 * z);
78}
79
80color rgb_to_hsv(color rgb)
81{
82 float cmax, cmin, h, s, v, cdelta;
83 color c;
84
85 cmax = max(rgb[0], max(rgb[1], rgb[2]));
86 cmin = min(rgb[0], min(rgb[1], rgb[2]));
87 cdelta = cmax - cmin;
88
89 v = cmax;
90
91 if (cmax != 0.0) {
92 s = cdelta / cmax;
93 }
94 else {
95 s = 0.0;
96 h = 0.0;
97 }
98
99 if (s == 0.0) {
100 h = 0.0;
101 }
102 else {
103 c = (color(cmax, cmax, cmax) - rgb) / cdelta;
104
105 if (rgb[0] == cmax) {
106 h = c[2] - c[1];
107 }
108 else if (rgb[1] == cmax) {
109 h = 2.0 + c[0] - c[2];
110 }
111 else {
112 h = 4.0 + c[1] - c[0];
113 }
114
115 h /= 6.0;
116
117 if (h < 0.0) {
118 h += 1.0;
119 }
120 }
121
122 return color(h, s, v);
123}
124
125color hsv_to_rgb(color hsv)
126{
127 float i, f, p, q, t, h, s, v;
128 color rgb;
129
130 h = hsv[0];
131 s = hsv[1];
132 v = hsv[2];
133
134 if (s == 0.0) {
135 rgb = color(v, v, v);
136 }
137 else {
138 if (h == 1.0) {
139 h = 0.0;
140 }
141
142 h *= 6.0;
143 i = floor(h);
144 f = h - i;
145 rgb = color(f, f, f);
146 p = v * (1.0 - s);
147 q = v * (1.0 - (s * f));
148 t = v * (1.0 - (s * (1.0 - f)));
149
150 if (i == 0.0) {
151 rgb = color(v, t, p);
152 }
153 else if (i == 1.0) {
154 rgb = color(q, v, p);
155 }
156 else if (i == 2.0) {
157 rgb = color(p, v, t);
158 }
159 else if (i == 3.0) {
160 rgb = color(p, q, v);
161 }
162 else if (i == 4.0) {
163 rgb = color(t, p, v);
164 }
165 else {
166 rgb = color(v, p, q);
167 }
168 }
169
170 return rgb;
171}
172
173color rgb_to_hsl(color rgb)
174{
175 float cmax, cmin, h, s, l;
176
177 cmax = max(rgb[0], max(rgb[1], rgb[2]));
178 cmin = min(rgb[0], min(rgb[1], rgb[2]));
179 l = min(1.0, (cmax + cmin) / 2.0);
180
181 if (cmax == cmin) {
182 h = s = 0.0; /* achromatic */
183 }
184 else {
185 float cdelta = cmax - cmin;
186 s = l > 0.5 ? cdelta / (2.0 - cmax - cmin) : cdelta / (cmax + cmin);
187 if (cmax == rgb[0]) {
188 h = (rgb[1] - rgb[2]) / cdelta + (rgb[1] < rgb[2] ? 6.0 : 0.0);
189 }
190 else if (cmax == rgb[1]) {
191 h = (rgb[2] - rgb[0]) / cdelta + 2.0;
192 }
193 else {
194 h = (rgb[0] - rgb[1]) / cdelta + 4.0;
195 }
196 }
197 h /= 6.0;
198
199 return color(h, s, l);
200}
201
202color hsl_to_rgb(color hsl)
203{
204 float nr, ng, nb, chroma, h, s, l;
205
206 h = hsl[0];
207 s = hsl[1];
208 l = hsl[2];
209
210 nr = abs(h * 6.0 - 3.0) - 1.0;
211 ng = 2.0 - abs(h * 6.0 - 2.0);
212 nb = 2.0 - abs(h * 6.0 - 4.0);
213
214 nr = clamp(nr, 0.0, 1.0);
215 nb = clamp(nb, 0.0, 1.0);
216 ng = clamp(ng, 0.0, 1.0);
217
218 chroma = (1.0 - abs(2.0 * l - 1.0)) * s;
219
220 return color((nr - 0.5) * chroma + l, (ng - 0.5) * chroma + l, (nb - 0.5) * chroma + l);
221}
#define X
#define Z
#define Y
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define pow
#define abs
#define floor
constexpr T clamp(T, U, U) RET
float color_scene_linear_to_srgb(float c)
Definition node_color.h:17
color hsl_to_rgb(color hsl)
Definition node_color.h:202
color hsv_to_rgb(color hsv)
Definition node_color.h:125
float color_srgb_to_scene_linear(float c)
Definition node_color.h:7
color rgb_to_hsl(color rgb)
Definition node_color.h:173
color color_unpremultiply(color c, float alpha)
Definition node_color.h:41
color xyY_to_xyz(float x, float y, float Y)
Definition node_color.h:52
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
color rgb_to_hsv(color rgb)
Definition node_color.h:80
#define min(a, b)
Definition sort.cc:36
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251