DISTRHO Plugin Framework
 All Classes Functions Variables Modules Pages
Color.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DGL_COLOR_HPP_INCLUDED
18 #define DGL_COLOR_HPP_INCLUDED
19 
20 #include "Base.hpp"
21 
22 struct NVGcolor;
23 
24 START_NAMESPACE_DGL
25 
26 // -----------------------------------------------------------------------
27 
28 // TODO: create color from "#333" and "#112233" like strings
29 
30 /**
31  A color made from red, green, blue and alpha floating-point values in [0..1] range.
32 */
33 struct Color {
34  /**
35  Direct access to the color values.
36  */
37  union {
38  float rgba[4];
39  struct { float red, green, blue, alpha; };
40  };
41 
42  /**
43  Create black color.
44  */
45  Color() noexcept;
46 
47  /**
48  Create a color from red, green, blue and alpha numeric values.
49  Values must be in [0..255] range.
50  */
51  Color(int red, int green, int blue, int alpha = 255) noexcept;
52 
53  /**
54  Create a color from red, green, blue and alpha floating-point values.
55  Values must in [0..1] range.
56  */
57  Color(float red, float green, float blue, float alpha = 1.0f) noexcept;
58 
59  /**
60  Create a color by copying another color.
61  */
62  Color(const Color& color) noexcept;
63  Color& operator=(const Color& color) noexcept;
64 
65  /**
66  Create a color by linearly interpolating two other colors.
67  */
68  Color(const Color& color1, const Color& color2, float u) noexcept;
69 
70  /**
71  Create a color specified by hue, saturation and lightness.
72  Values must in [0..1] range.
73  */
74  static Color fromHSL(float hue, float saturation, float lightness, float alpha = 1.0f);
75 
76  /**
77  Create a color from a HTML string like "#333" or "#112233".
78  */
79  static Color fromHTML(const char* rgb, float alpha = 1.0f);
80 
81  /**
82  Linearly interpolate this color against another.
83  */
84  void interpolate(const Color& other, float u) noexcept;
85 
86  /**
87  Check if this color matches another.
88  @note: Comparison is forced within 8-bit color values.
89  */
90  bool isEqual(const Color& color, bool withAlpha = true) noexcept;
91  bool isNotEqual(const Color& color, bool withAlpha = true) noexcept;
92  bool operator==(const Color& color) noexcept;
93  bool operator!=(const Color& color) noexcept;
94 
95  /**
96  Fix color bounds if needed.
97  */
98  void fixBounds() noexcept;
99 
100  /**
101  @internal
102  Needed for NanoVG compatibility.
103  */
104  Color(const NVGcolor&) noexcept;
105  operator NVGcolor() const noexcept;
106 };
107 
108 // -----------------------------------------------------------------------
109 
110 END_NAMESPACE_DGL
111 
112 #endif // DGL_COLOR_HPP_INCLUDED
void fixBounds() noexcept
Definition: Color.hpp:33
Color() noexcept
static Color fromHSL(float hue, float saturation, float lightness, float alpha=1.0f)
bool isEqual(const Color &color, bool withAlpha=true) noexcept
void interpolate(const Color &other, float u) noexcept
static Color fromHTML(const char *rgb, float alpha=1.0f)