FastUIDraw
fastuidraw_blend_util.frag.glsl.hpp
Go to the documentation of this file.
1 /*!
2  * \file fastuidraw_blend_util.frag.glsl.hpp
3  * \brief file fastuidraw_blend_util.frag.glsl.hpp
4  *
5  * Copyright 2018 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 /*!\addtogroup GLSLFragCode
20  * @{
21  */
22 
23 /*! \brief Compute the minimum color channel value */
24 float
26 {
27  return min(C.r, min(C.g, C.b));
28 }
29 
30 /*! \brief Compute the maximum color channel value */
31 float
33 {
34  return max(C.r, max(C.g, C.b));
35 }
36 
37 /*! \brief compute the luminosity of an RGB value */
38 float
40 {
41  return dot(C, vec3(0.3, 0.59, 0.11));
42 }
43 
44 /*! \brief compute the saturation of an RGB value */
45 float
47 {
49 }
50 
51 /*!
52  * \brief
53  * clip an RGB value so that its luminosity is within the range
54  * \ref fastuidraw_compute_min() and fastuidraw_compute_max().
55  */
56 vec3
58 {
59  float lumC = fastuidraw_compute_luminosity(C);
60  float minC = fastuidraw_compute_min(C);
61  float maxC = fastuidraw_compute_max(C);
62  vec3 lumCv = vec3(lumC);
63 
64  if (minC < 0.0)
65  {
66  float factor;
67  factor = lumC / (lumC - minC);
68  C = lumCv + (C - lumCv) * factor;
69  }
70  else if (maxC > 1.0)
71  {
72  float factor;
73  factor = (1.0 - lumC) / (maxC - lumC);
74  C = lumCv + (C - lumCv) * factor;
75  }
76  return C;
77 }
78 
79 /*!
80  * Return an RGB color value generate from taking
81  * the chroma of one color value and the luminosity
82  * of another
83  * \param C color value from which to take the chroma value
84  * \param L color value from which to take the luminosity value
85  */
86 vec3
88 {
89  float Clum = fastuidraw_compute_luminosity(C);
90  float Llum = fastuidraw_compute_luminosity(L);
91  float delta = Llum - Clum;
92  return fastuidraw_clipcolor(C + vec3(delta));
93 }
94 
95 /*!
96  * Return an RGB color value generate from taking
97  * the chroma of one color value and the luminosity
98  * and saturation of other color values
99  * \param C color value from which to take the chroma value
100  * \param S color value from which to take the saturation value
101  * \param L color value from which to take the luminosity value
102  */
103 vec3
105 {
106  float Cmin = fastuidraw_compute_min(C);
107  float Csat = fastuidraw_compute_saturation(C);
108  float Ssat = fastuidraw_compute_saturation(S);
109  vec3 color;
110 
111  if (Csat > 0.0)
112  {
113  C = (C - Cmin) * Ssat / Csat;
114  }
115  else
116  {
117  C = vec3(0.0);
118  }
119  return fastuidraw_set_luminosity(C, L);
120 }
121 
122 /*! \brief Undo pre-multiply by alpha on the RGB channels of a color value */
123 vec3
125 {
126  if (S.a != 0.0)
127  {
128  S.a = 1.0 / S.a;
129  }
130  return S.rgb * S.a;
131 }
132 
133 /*!
134  * \brief
135  * perform overlay blending on a single color channel
136  * \param S Source input value
137  * \param D Destination input value
138  */
139 float
140 fastuidraw_overlay(in float S, in float D)
141 {
142  return (D <= 0.5) ?
143  (2.0 * S * D) :
144  (1.0 - 2.0 * (1.0 - S) * (1.0 - D));
145 }
146 
147 /*!
148  * \brief
149  * perform softlight blending on a single color channel
150  * \param S Source input value
151  * \param D Destination input value
152  */
153 float
154 fastuidraw_softlight(in float S, in float D)
155 {
156  if (S <= 0.5)
157  {
158  return D - (1.0 - 2.0 * S) * D * (1.0 - D);
159  }
160  else if (D <= 0.25)
161  {
162  return D + (2.0 * S - 1.0) * D * ((16.0 * D - 12.0) * D + 3.0);
163  }
164  else
165  {
166  return D + (2.0 * S - 1.0) * (sqrt(D) - D);
167  }
168 }
169 
170 /*!
171  * \brief
172  * perform hardlight blending on a single color channel
173  * \param S Source input value
174  * \param D Destination input value
175  */
176 float
177 fastuidraw_hardlight(in float S, in float D)
178 {
179  if (S <= 0.5)
180  {
181  return 2.0 * S * D;
182  }
183  else
184  {
185  return 1.0 - 2.0 * (1.0 - S) * (1.0 - D);
186  }
187 }
188 
189 /*!
190  * \brief
191  * perform color dodge blending on a single color channel
192  * \param S Source input value
193  * \param D Destination input value
194  */
195 float
196 fastuidraw_color_dodge(in float S, in float D)
197 {
198  if (D <= 0.0)
199  {
200  return 0.0;
201  }
202  else if (S >= 1.0)
203  {
204  return 1.0;
205  }
206  else
207  {
208  return min(1.0, D / (1.0 - S));
209  }
210 }
211 
212 /*!
213  * \brief
214  * perform color burn blending on a single color channel
215  * \param S Source input value
216  * \param D Destination input value
217  */
218 float
219 fastuidraw_color_burn(in float S, in float D)
220 {
221  if (D >= 1.0)
222  {
223  return 1.0;
224  }
225  else if (S <= 0.0)
226  {
227  return 0.0;
228  }
229  else
230  {
231  return 1.0 - min(1.0, (1.0 - D) / S);
232  }
233 }
234 
235 ///@cond
236 
237 /* Naughty macro that assumes in_src, in_fb are defined. */
238 #define FASTUIDRAW_PORTER_DUFF_MACRO(src_factor, dst_factor) \
239  ((src_factor) * in_src + (dst_factor) * in_fb)
240 
241 /* Macro that runs the expression on one channel of out_src */
242 #define FASTUIDRAW_SEPERABLE_FBF_BLEND_ONE_CHANNEL_MACRO(channel, expression) \
243  S = Srgb.channel; D = Drgb.channel; \
244  out_src.channel = (expression)
245 
246 /* Naughty macro that assumes in_src, in_fb are defined AND
247  * expression is a scalar expression in S and D (temporaries)
248  * with S representing source (i.e in_src with alpha undone)
249  * and D representing destination (i.e. in_fb with alpha undone).
250  */
251 #define FASTUIDRAW_SEPERABLE_FBF_BLEND_MACRO(expression) \
252  vec3 Srgb, Drgb; \
253  float S, D; \
254  Srgb = fastuidraw_undo_alpha(in_src); \
255  Drgb = fastuidraw_undo_alpha(in_fb); \
256  out_src.a = in_src.a + in_fb.a * (1.0 - in_src.a); \
257  FASTUIDRAW_SEPERABLE_FBF_BLEND_ONE_CHANNEL_MACRO(r, expression); \
258  FASTUIDRAW_SEPERABLE_FBF_BLEND_ONE_CHANNEL_MACRO(g, expression); \
259  FASTUIDRAW_SEPERABLE_FBF_BLEND_ONE_CHANNEL_MACRO(b, expression); \
260  out_src.rgb *= in_src.a * in_fb.a;
261  out_src.rgb += Srgb * (1.0 - in_fb.a) + Drgb * (1.0 - in_src.a)
262 
263 /* Naughty macro that assumes in_src, in_fb are defined AND
264  * expression is a vec3 expression in S and D (temporaries)
265  * with S representing source (i.e in_src with alpha undone)
266  * and D representing destination (i.e. in_fb with alpha undone).
267  */
268 #define FASTUIDRAW_NON_SEPERABLE_FBF_BLEND_MACRO(expression) \
269  vec3 S, D; \
270  S = fastuidraw_undo_alpha(in_src); \
271  D = fastuidraw_undo_alpha(in_fb); \
272  out_src.a = in_src.a + in_fb.a * (1.0 - in_src.a); \
273  out_src.rgb = expression; \
274  out_src.rgb *= in_src.a * in_fb.a; \
275  out_src.rgb += S * (1.0 - in_fb.a) + D * (1.0 - in_src.a);
276 
277 ///@endcond
278 
279 /*! @} */
float fastuidraw_color_dodge(in float S, in float D)
perform color dodge blending on a single color channel
float fastuidraw_hardlight(in float S, in float D)
perform hardlight blending on a single color channel
float fastuidraw_color_burn(in float S, in float D)
perform color burn blending on a single color channel
vec3 fastuidraw_set_luminosity(in vec3 C, in vec3 L)
float fastuidraw_compute_max(in vec3 C)
Compute the maximum color channel value.
T dot(const vecN< T, N > &a, const vecN< T, N > &b)
Definition: vecN.hpp:1124
vec3 fastuidraw_clipcolor(in vec3 C)
clip an RGB value so that its luminosity is within the range fastuidraw_compute_min() and fastuidraw_...
float fastuidraw_softlight(in float S, in float D)
perform softlight blending on a single color channel
vecN< float, 4 > vec4
Definition: vecN.hpp:1239
float fastuidraw_overlay(in float S, in float D)
perform overlay blending on a single color channel
float fastuidraw_compute_saturation(in vec3 C)
compute the saturation of an RGB value
float fastuidraw_compute_min(in vec3 C)
Compute the minimum color channel value.
float fastuidraw_compute_luminosity(in vec3 C)
compute the luminosity of an RGB value
vecN< float, 3 > vec3
Definition: vecN.hpp:1235
vec3 fastuidraw_undo_alpha(in vec4 S)
Undo pre-multiply by alpha on the RGB channels of a color value.
vec3 fastuidraw_set_saturation_and_luminosity(in vec3 C, in vec3 S, in vec3 L)