Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
HsbColor.h
Go to the documentation of this file.
1 
2 /*-------------------------------------------------------------------------
3 HsbColor provides a color object that can be directly consumed by LumitronixIFlexBus
4 
5 Written by Michael C. Miller.
6 
7 I invest time and resources providing this open source code,
8 please support me by dontating (see https://github.com/Makuna)
9 
10 -------------------------------------------------------------------------
11 This file is part of the LUMITRONIX_iFlex_Workshop library.
12 
13 LumitronixIFlexBus is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation, either version 3 of
16 the License, or (at your option) any later version.
17 
18 LumitronixIFlexBus is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public
24 License along with LumitronixIFlex. If not, see
25 <http://www.gnu.org/licenses/>.
26 -------------------------------------------------------------------------*/
27 #pragma once
28 
29 // ------------------------------------------------------------------------
30 // HsbColor represents a color object that is represented by Hue, Saturation, Brightness
31 // component values. It contains helpful color routines to manipulate the
32 // color.
33 // ------------------------------------------------------------------------
34 struct HsbColor
35 {
36  // ------------------------------------------------------------------------
37  // Construct a HsbColor using H, S, B values (0.0 - 1.0)
38  // ------------------------------------------------------------------------
39  HsbColor(float h, float s, float b) :
40  H(h), S(s), B(b)
41  {
42  };
43 
44  // ------------------------------------------------------------------------
45  // Construct a HsbColor using RgbColor
46  // ------------------------------------------------------------------------
47  HsbColor(const RgbColor& color);
48 
49  // ------------------------------------------------------------------------
50  // Construct a HsbColor using Rgb48Color
51  // ------------------------------------------------------------------------
52  HsbColor(const Rgb48Color& color);
53 
54  // ------------------------------------------------------------------------
55  // Construct a HsbColor that will have its values set in latter operations
56  // CAUTION: The H,S,B members are not initialized and may not be consistent
57  // ------------------------------------------------------------------------
59  {
60  };
61 
62  // ------------------------------------------------------------------------
63  // LinearBlend between two colors by the amount defined by progress variable
64  // left - the color to start the blend at
65  // right - the color to end the blend at
66  // progress - (0.0 - 1.0) value where 0.0 will return left and 1.0 will return right
67  // and a value between will blend the color weighted linearly between them
68  // ------------------------------------------------------------------------
69  template <typename T_NEOHUEBLEND> static HsbColor LinearBlend(const HsbColor& left,
70  const HsbColor& right,
71  float progress)
72  {
73  return HsbColor(T_NEOHUEBLEND::HueBlend(left.H, right.H, progress),
74  left.S + ((right.S - left.S) * progress),
75  left.B + ((right.B - left.B) * progress));
76  }
77 
78  // ------------------------------------------------------------------------
79  // BilinearBlend between four colors by the amount defined by 2d variable
80  // c00 - upper left quadrant color
81  // c01 - upper right quadrant color
82  // c10 - lower left quadrant color
83  // c11 - lower right quadrant color
84  // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space
85  // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space
86  // ------------------------------------------------------------------------
87  template <typename T_NEOHUEBLEND> static HsbColor BilinearBlend(const HsbColor& c00,
88  const HsbColor& c01,
89  const HsbColor& c10,
90  const HsbColor& c11,
91  float x,
92  float y)
93  {
94  float v00 = (1.0f - x) * (1.0f - y);
95  float v10 = x * (1.0f - y);
96  float v01 = (1.0f - x) * y;
97  float v11 = x * y;
98 
99  return HsbColor(
100  T_NEOHUEBLEND::HueBlend(
101  T_NEOHUEBLEND::HueBlend(c00.H, c10.H, x),
102  T_NEOHUEBLEND::HueBlend(c01.H, c11.H, x),
103  y),
104  c00.S * v00 + c10.S * v10 + c01.S * v01 + c11.S * v11,
105  c00.B * v00 + c10.B * v10 + c01.B * v01 + c11.B * v11);
106  };
107 
108  // ------------------------------------------------------------------------
109  // Hue, Saturation, Brightness color members
110  // ------------------------------------------------------------------------
111 
112  float H;
113  float S;
114  float B;
115 
116 private:
117  static void _RgbToHsb(float r, float g, float b, HsbColor* color);
118 };
119 
Definition: HsbColor.h:35
static HsbColor LinearBlend(const HsbColor &left, const HsbColor &right, float progress)
Definition: HsbColor.h:69
HsbColor()
Definition: HsbColor.h:58
float H
Definition: HsbColor.h:106
float B
Definition: HsbColor.h:114
HsbColor(float h, float s, float b)
Definition: HsbColor.h:39
float S
Definition: HsbColor.h:113
static HsbColor BilinearBlend(const HsbColor &c00, const HsbColor &c01, const HsbColor &c10, const HsbColor &c11, float x, float y)
Definition: HsbColor.h:87
Definition: Rgb48Color.h:36
Definition: RgbColor.h:36