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