Dusk Audio Framework
Loading...
Searching...
No Matches
Color.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2025 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
22struct NVGcolor;
23
24START_NAMESPACE_DGL
25
26// --------------------------------------------------------------------------------------------------------------------
27
28/**
29 A color made from red, green, blue and alpha floating-point values in [0..1] range.
30*/
31struct Color {
32 /**
33 Direct access to the color values.
34 */
35 union {
36 float rgba[4];
37 struct { float red, green, blue, alpha; };
38 };
39
40 /**
41 Create solid black color.
42 */
43 Color() noexcept;
44
45 /**
46 Create a color from red, green, blue and alpha numeric values.
47 All values except alpha must be in [0..255] range, with alpha in [0..1] range.
48 */
49 Color(int red, int green, int blue, float alpha = 1.0f) noexcept;
50
51 /**
52 Create a color from red, green, blue and alpha floating-point values.
53 All values must in [0..1] range.
54 */
55 Color(float red, float green, float blue, float alpha = 1.0f) noexcept;
56
57 /**
58 Create a color by copying another color.
59 */
60 Color(const Color& color) noexcept;
61 Color& operator=(const Color& color) noexcept;
62
63 /**
64 Create a color by linearly interpolating two other colors.
65 */
66 Color(const Color& color1, const Color& color2, float u) noexcept;
67
68 /**
69 Create a new color based on this one but with a different alpha value.
70 */
71 Color withAlpha(float alpha) const noexcept;
72
73 /**
74 Create a new color based on this one but with subtracted numeric value on all elements.
75 Value must be in [0..255] range.
76 */
77 Color minus(int value) const noexcept;
78
79 /**
80 Create a new color based on this one but with subtracted floating-point value on all elements.
81 Value must be in [0..1] range.
82 */
83 Color minus(float value) const noexcept;
84
85 /**
86 Create a new color based on this one but with added numeric value on all elements.
87 Value must be in [0..255] range.
88 */
89 Color plus(int value) const noexcept;
90
91 /**
92 Create a new color based on this one but with added floating-point value on all elements.
93 Value must be in [0..1] range.
94 */
95 Color plus(float value) const noexcept;
96
97 /**
98 Create a new color based on this one but colors inverted.
99 */
100 Color invert() const noexcept;
101
102 /**
103 Create a new color based on this one but in grayscale (using weighted average).
104 */
105 Color asGrayscale() const noexcept;
106
107 /**
108 Create a color specified by hue, saturation and lightness.
109 Values must in [0..1] range.
110 */
111 static Color fromHSL(float hue, float saturation, float lightness, float alpha = 1.0f);
112
113 /**
114 Create a color from a HTML string like "#333" or "#112233".
115 */
116 static Color fromHTML(const char* rgb, float alpha = 1.0f) noexcept;
117
118 /**
119 Create a color from a RGB unsigned integer.
120 Basically doing:
121 ```
122 uint8_t red = (color >> 24) & 0xff;
123 uint8_t green = (color >> 16) & 0xff;
124 uint8_t blue = (color >> 8) & 0xff;
125 ```
126 */
127 static Color fromRGB(uint color, float alpha = 1.0f) noexcept;
128
129 /**
130 Linearly interpolate this color against another.
131 */
132 void interpolate(const Color& other, float u) noexcept;
133
134 /**
135 Check if this color matches another.
136 @note Comparison is done within 8-bit color space.
137 */
138 bool isEqual(const Color& color, bool withAlpha = true) noexcept;
139 bool isNotEqual(const Color& color, bool withAlpha = true) noexcept;
140 bool operator==(const Color& color) noexcept;
141 bool operator!=(const Color& color) noexcept;
142
143 /**
144 Fix color bounds if needed.
145 */
146 void fixBounds() noexcept;
147
148 /**
149 Set this color for use in the next drawing operation for the provided context.
150 */
151 void setFor(const GraphicsContext& context, bool includeAlpha = false);
152
153 /**
154 @internal
155 Needed for NanoVG compatibility.
156 */
157 Color(const NVGcolor&) noexcept;
158 operator NVGcolor() const noexcept;
159};
160
161// --------------------------------------------------------------------------------------------------------------------
162
163END_NAMESPACE_DGL
164
165#endif // DGL_COLOR_HPP_INCLUDED
Color asGrayscale() const noexcept
bool isEqual(const Color &color, bool withAlpha=true) noexcept
Color invert() const noexcept
Color withAlpha(float alpha) const noexcept
static Color fromHSL(float hue, float saturation, float lightness, float alpha=1.0f)
void interpolate(const Color &other, float u) noexcept
Color minus(int value) const noexcept
void setFor(const GraphicsContext &context, bool includeAlpha=false)
void fixBounds() noexcept
static Color fromHTML(const char *rgb, float alpha=1.0f) noexcept
static Color fromRGB(uint color, float alpha=1.0f) noexcept
Color() noexcept
Color plus(int value) const noexcept
Definition Base.hpp:287