blend.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef COLOR_UTIL_BLEND_H
36 #define COLOR_UTIL_BLEND_H
37 
38 #include <color_util/types.h>
39 #include <algorithm>
40 
41 namespace color_util
42 {
50 template <typename rgba>
51 inline rgba rgbaBlend(const rgba& color_a, const rgba& color_b, double ratio)
52 {
53  ratio = std::min(ratio, 1.0);
54  ratio = std::max(ratio, 0.0);
55 
56  double i_ratio = 1.0 - ratio;
57 
58  rgba color;
59  color.r = color_a.r * i_ratio + color_b.r * ratio;
60  color.g = color_a.g * i_ratio + color_b.g * ratio;
61  color.b = color_a.b * i_ratio + color_b.b * ratio;
62  color.a = color_a.a * i_ratio + color_b.a * ratio;
63  return color;
64 }
65 
73 template <typename hsva>
74 inline hsva hueBlend(const hsva& color_a, const hsva& color_b, double ratio)
75 {
76  ratio = std::min(ratio, 1.0);
77  ratio = std::max(ratio, 0.0);
78 
79  double i_ratio = 1.0 - ratio;
80 
81  hsva color;
82  color.h = color_a.h * i_ratio + color_b.h * ratio;
83  color.s = color_a.s * i_ratio + color_b.s * ratio;
84  color.v = color_a.v * i_ratio + color_b.v * ratio;
85  color.a = color_a.a * i_ratio + color_b.a * ratio;
86  return color;
87 }
88 
102  const color_util::ColorHSVA& color_b,
103  double ratio)
104 {
105  ratio = std::min(ratio, 1.0);
106  ratio = std::max(ratio, 0.0);
107 
108  double i_ratio = 1.0 - ratio;
109 
110  // Direct interpolation for saturation/value/alpha
111  color_util::ColorHSVA color;
112  color.s = color_a.s * i_ratio + color_b.s * ratio;
113  color.v = color_a.v * i_ratio + color_b.v * ratio;
114  color.a = color_a.a * i_ratio + color_b.a * ratio;
115 
116  // Hue interpolation
117  double start_h, end_h, diff;
118  if (color_a.h > color_b.h)
119  {
120  start_h = color_b.h;
121  end_h = color_a.h;
122  ratio = i_ratio;
123  }
124  else
125  {
126  start_h = color_a.h;
127  end_h = color_b.h;
128  }
129  diff = end_h - start_h;
130 
131  // If the hue difference is greater than 0.5, interpolate the other way around
132  if (diff > 0.5) // 180deg
133  {
134  start_h = start_h + 1; // 360deg
135  double ipart;
136  color.h = modf(start_h + ratio * (end_h - start_h), &ipart); // 360deg
137  }
138  else
139  {
140  color.h = start_h + ratio * diff;
141  }
142  return color;
143 }
144 
145 
146 } // namespace color_util
147 
148 #endif // COLOR_UTIL_BLEND_H
hsva hueBlend(const hsva &color_a, const hsva &color_b, double ratio)
Return a color that is a linear blending of color_a and color_b in hsv space.
Definition: blend.h:74
color_util::ColorHSVA hueBlendPlus(const color_util::ColorHSVA &color_a, const color_util::ColorHSVA &color_b, double ratio)
Return a color that blends color_a and color_b in hsv space, using the shortest distance between the ...
Definition: blend.h:101
rgba rgbaBlend(const rgba &color_a, const rgba &color_b, double ratio)
Return a color that is a linear blending of color_a and color_b in rgba space.
Definition: blend.h:51


color_util
Author(s):
autogenerated on Sun Jan 10 2021 04:08:24