TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_style_gradient.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6#include <span>
7#include <cmath>
8#include <array>
9#include <vector>
10
11namespace tachyon
12{
15
16 // ------------------------------------------------------------------------
17 // Color and Gradient Types
18 // ------------------------------------------------------------------------
19
21 struct ColorStop
22 {
23 private:
24 color4b color = {};
25 uint16_t offset = 0;
26
27 public:
28 bool operator==(const ColorStop& other) const noexcept = default;
29
30 ColorStop& SetColor(color4b newColor) { color = newColor; return *this; }
31
34 {
35 newColor.r = (newColor.r < 0) ? 0 : (newColor.r > 1) ? 1 : newColor.r;
36 newColor.g = (newColor.g < 0) ? 0 : (newColor.g > 1) ? 1 : newColor.g;
37 newColor.b = (newColor.b < 0) ? 0 : (newColor.b > 1) ? 1 : newColor.b;
38 newColor.a = (newColor.a < 0) ? 0 : (newColor.a > 1) ? 1 : newColor.a;
39 auto pack = [](float value) -> byte
40 {
41 int iv = static_cast<int>(std::lround(value * 255.0f));
42 iv = std::clamp(iv, 0, 255);
43 return static_cast<byte>(iv);
44 };
45 color = color4b{
46 .r = pack(newColor.r),
47 .g = pack(newColor.g),
48 .b = pack(newColor.b),
49 .a = pack(newColor.a)
50 };
51 return *this;
52 }
53
55 ColorStop& SetOffset(float newOffset)
56 {
57 if (newOffset < 0.0f) newOffset = 0.0f;
58 if (newOffset >= 1.0f) newOffset = 1.0f;
59 offset = static_cast<uint16_t>(newOffset * 65535.0f);
60 return *this;
61 }
62
64 float GetOffset() const { return (offset / 65535.0f); }
66 color4f GetColor4f() const { return color4f { color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f }; }
67 color4b GetColor4b() const { return color; }
68 };
69
72 enum class GradientType : uint8_t
73 {
75 Linear,
77 };
78
80 enum class ColorSpace : uint8_t
81 {
88 };
89
92 struct Gradient
93 {
94 static constexpr int kMaxStops = 8;
95
96 std::array<ColorStop, kMaxStops> stops{};
97 uint16_t value = 0;
98 uint8_t stopCount = 0;
101
103 float GetValue() const
104 {
105 // multiply it by even number so that 90 degrees is exactly 0.25, 180 is 0.5, etc.
106 return float(value) / 65534.0f;
107 }
108
110 static Gradient Solid(const color4f& color)
111 {
112 Gradient g;
114 g.AddStop(color, 0.0f);
115 g.AddStop(color, 1.0f);
116 return g;
117 }
118
120 static Gradient Solid(const color4b& color)
121 {
122 Gradient g;
124 g.AddStop(color, 0.0f);
125 g.AddStop(color, 1.0f);
126 return g;
127 }
128
130 static Gradient Linear(const color4f& c0, const color4f& c1)
131 {
132 Gradient g;
134 g.AddStop(c0, 0.0f);
135 g.AddStop(c1, 1.0f);
136 return g;
137 }
138
140 static Gradient Linear(const color4b& c0, const color4b& c1)
141 {
142 Gradient g;
144 g.AddStop(c0, 0.0f);
145 g.AddStop(c1, 1.0f);
146 return g;
147 }
148
150 static Gradient Linear(const color4f& c0, const color4f& c1, const color4f& c2)
151 {
152 Gradient g;
154 g.AddStop(c0, 0.0f);
155 g.AddStop(c1, 0.5f);
156 g.AddStop(c2, 1.0f);
157 return g;
158 }
159
161 static Gradient Linear(const color4b& c0, const color4b& c1, const color4b& c2)
162 {
163 Gradient g;
165 g.AddStop(c0, 0.0f);
166 g.AddStop(c1, 0.5f);
167 g.AddStop(c2, 1.0f);
168 return g;
169 }
170
172 static Gradient Linear(const color4f& c0, const color4f& c1, const color4f& c2, const color4f& c3)
173 {
174 Gradient g;
176 g.AddStop(c0, 0.00f);
177 g.AddStop(c1, 0.25f);
178 g.AddStop(c2, 0.50f);
179 g.AddStop(c3, 1.00f);
180 return g;
181 }
182
184 static Gradient Linear(const color4b& c0, const color4b& c1, const color4b& c2, const color4b& c3)
185 {
186 Gradient g;
188 g.AddStop(c0, 0.00f);
189 g.AddStop(c1, 0.25f);
190 g.AddStop(c2, 0.50f);
191 g.AddStop(c3, 1.00f);
192 return g;
193 }
194
198 static uint16_t EncodeAngle(float angleDegrees)
199 {
200 double whole;
201 double turn = std::modf(double(angleDegrees) / 360.0, &whole);
202 if (turn < 0.0) turn += 1.0;
203 return static_cast<uint16_t>(turn * 65534.0);
204 }
205
207 static Gradient Linear(float angleDegrees = 0.0f)
208 {
209 Gradient g;
211 g.value = EncodeAngle(angleDegrees);
212 return g;
213 }
214
217 static Gradient Radial(float angleDegrees = 1.0f)
218 {
219 Gradient g;
221 g.value = EncodeAngle(angleDegrees);
222 return g;
223 }
224
226 inline Gradient& WithColorSpace(ColorSpace cs) { colorSpace = cs; return *this; }
228 inline ColorSpace GetColorSpace() const { return colorSpace; }
229
231 inline bool AddStop(const color4f& color, float offset)
232 {
233 if (stopCount >= kMaxStops)
234 return false;
235 stops[stopCount++] = ColorStop {}.SetColor(color).SetOffset(offset);
236 return true;
237 }
238
240 inline bool AddStop(const color4b& color, float offset)
241 {
242 if (stopCount >= kMaxStops)
243 return false;
244 stops[stopCount++] = ColorStop{}.SetColor(color).SetOffset(offset);
245 return true;
246 }
247
249 inline void ClearStops() { stopCount = 0; }
250
253 inline uint8_t StopCount() const { return stopCount < kMaxStops ? stopCount : uint8_t(kMaxStops); }
254
256 inline bool IsEmpty() const { return type == GradientType::None || stopCount == 0; }
257
259 void WriteTo(std::span<color4b> out) const;
260
261 bool operator==(const Gradient& other) const noexcept
262 {
263 if (type != other.type || stopCount != other.stopCount ||
264 value != other.value || colorSpace != other.colorSpace)
265 return false;
266
267 for (uint8_t i = 0; i < StopCount(); ++i)
268 if (stops[i] != other.stops[i])
269 return false;
270
271 return true;
272 }
273 };
274
276 enum class StopInterpolation : uint8_t
277 {
278 Linear,
281 };
282
285 {
286 float offset = 0.0f;
289
290 bool operator==(const GradientStop&) const = default;
291 };
292
303 {
304 static constexpr int kMaxStops = 64;
305
306 std::vector<GradientStop> stops;
308
310 bool AddStop(const color4f& color, float offset, StopInterpolation interpolation = StopInterpolation::Linear);
312 int StopCount() const { return stops.size() < size_t(kMaxStops) ? int(stops.size()) : kMaxStops; }
313
316 color4f Sample(float t) const;
318 void WriteTo(std::span<color4f> out) const;
320 void WriteTo(std::span<color4b> out) const;
321
323 static GradientF From(const Gradient& gradient);
325 void Assign(const Gradient& gradient);
329
330 bool operator==(const GradientF&) const = default;
331 };
332
334}//namespace tachyon
@ None
Definition gui_element_flags.h:18
StopInterpolation
How a GradientF blends from a stop to the next one.
Definition gui_style_gradient.h:277
GradientType
Kind of Gradient.
Definition gui_style_gradient.h:73
ColorSpace
The space gradient stops are interpolated in.
Definition gui_style_gradient.h:81
@ Linear
a straight blend
Definition gui_style_gradient.h:278
@ Constant
the stop's color all the way to the next stop: a step
Definition gui_style_gradient.h:280
@ Smooth
eases out of the stop and into the next (smoothstep)
Definition gui_style_gradient.h:279
@ Linear
Definition gui_style_gradient.h:75
@ None
Definition gui_style_gradient.h:74
@ Radial
Definition gui_style_gradient.h:76
@ HSV
Definition gui_style_gradient.h:85
@ HSL
Definition gui_style_gradient.h:84
@ Oklab
perceptual
Definition gui_style_gradient.h:87
@ sRGB
the default, as CSS
Definition gui_style_gradient.h:82
@ LinearRGB
Definition gui_style_gradient.h:83
@ Lab
CIE Lab, perceptual.
Definition gui_style_gradient.h:86
Definition gui_logging.h:24
Color stops for gradients.
Definition gui_style_gradient.h:22
bool operator==(const ColorStop &other) const noexcept=default
float GetOffset() const
Position along the gradient, 0..1.
Definition gui_style_gradient.h:64
ColorStop & SetColor(color4b newColor)
Definition gui_style_gradient.h:30
color4f GetColor4f() const
The color as 0..1 floats.
Definition gui_style_gradient.h:66
color4b GetColor4b() const
Definition gui_style_gradient.h:67
ColorStop & SetOffset(float newOffset)
Position along the gradient, 0..1 (clamped); stored as 16 bits.
Definition gui_style_gradient.h:55
ColorStop & SetColor(color4f newColor)
Clamps each channel to 0..1 and stores it as 8 bits.
Definition gui_style_gradient.h:33
A gradient of float colors, for data such as a material editor's gradient map: up to kMaxStops stops,...
Definition gui_style_gradient.h:303
int StopCount() const
The stops in use: at most kMaxStops.
Definition gui_style_gradient.h:312
static GradientF From(const Gradient &gradient)
The same stops as gradient, blending linearly in its color space.
bool AddStop(const color4f &color, float offset, StopInterpolation interpolation=StopInterpolation::Linear)
Adds a stop at offset (clamped to 0..1); false when kMaxStops are in use.
void Assign(const Gradient &gradient)
As From, into this one's storage.
std::vector< GradientStop > stops
in any order
Definition gui_style_gradient.h:306
color4f Sample(float t) const
The color at t (0..1, clamped), unclamped: HDR stays.
void WriteTo(std::span< color4b > out) const
As above, clamped to 0..1 and stored as 8 bits, for display.
Gradient ToGradient() const
The first Gradient::kMaxStops stops as a linear Gradient: colors clamped to 0..1 and stored as 8 bits...
ColorSpace colorSpace
space the stops are blended in
Definition gui_style_gradient.h:307
static constexpr int kMaxStops
stops past this are left out
Definition gui_style_gradient.h:304
void WriteTo(std::span< color4f > out) const
Samples the gradient evenly across out, unclamped.
bool operator==(const GradientF &) const =default
A stop of a GradientF.
Definition gui_style_gradient.h:285
color4f color
alpha not premultiplied; red, green and blue may pass 1 (HDR)
Definition gui_style_gradient.h:287
float offset
along the gradient, 0..1
Definition gui_style_gradient.h:286
bool operator==(const GradientStop &) const =default
StopInterpolation interpolation
from this stop to the next
Definition gui_style_gradient.h:288
Up to kMaxStops color stops plus an angle and interpolation color space.
Definition gui_style_gradient.h:93
uint8_t StopCount() const
The stops in use: stopCount, but never past the stop array (a gradient built in code can set stopCoun...
Definition gui_style_gradient.h:253
static Gradient Linear(const color4f &c0, const color4f &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:130
static Gradient Linear(float angleDegrees=0.0f)
An empty linear gradient at an angle in degrees; add stops with AddStop.
Definition gui_style_gradient.h:207
std::array< ColorStop, kMaxStops > stops
the first stopCount are in use
Definition gui_style_gradient.h:96
static constexpr int kMaxStops
capacity of stops
Definition gui_style_gradient.h:94
bool IsEmpty() const
No stops, or type None: nothing to draw.
Definition gui_style_gradient.h:256
ColorSpace colorSpace
space the stops are interpolated in
Definition gui_style_gradient.h:100
bool operator==(const Gradient &other) const noexcept
Definition gui_style_gradient.h:261
float GetValue() const
value as a fraction of a full turn (see EncodeAngle).
Definition gui_style_gradient.h:103
bool AddStop(const color4f &color, float offset)
Adds a stop at offset (0..1); false when all kMaxStops are in use.
Definition gui_style_gradient.h:231
uint8_t stopCount
stops in use
Definition gui_style_gradient.h:98
static uint16_t EncodeAngle(float angleDegrees)
Angle as a fraction of a full turn in [0, 1), wrapping negative angles (-90deg == 270deg).
Definition gui_style_gradient.h:198
bool AddStop(const color4b &color, float offset)
Adds a stop at offset (0..1); false when all kMaxStops are in use.
Definition gui_style_gradient.h:240
static Gradient Linear(const color4b &c0, const color4b &c1, const color4b &c2, const color4b &c3)
Four stops at 0, 0.25, 0.5 and 1.
Definition gui_style_gradient.h:184
uint16_t value
the angle, encoded by EncodeAngle
Definition gui_style_gradient.h:97
void ClearStops()
Removes all stops.
Definition gui_style_gradient.h:249
static Gradient Linear(const color4b &c0, const color4b &c1, const color4b &c2)
Three stops at 0, 0.5 and 1.
Definition gui_style_gradient.h:161
static Gradient Solid(const color4f &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:110
GradientType type
Definition gui_style_gradient.h:99
Gradient & WithColorSpace(ColorSpace cs)
Sets the space the stops are interpolated in; returns *this.
Definition gui_style_gradient.h:226
static Gradient Solid(const color4b &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:120
static Gradient Radial(float angleDegrees=1.0f)
An empty radial gradient; add stops with AddStop.
Definition gui_style_gradient.h:217
static Gradient Linear(const color4b &c0, const color4b &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:140
void WriteTo(std::span< color4b > out) const
Samples the gradient evenly across out, sRGB encoded.
ColorSpace GetColorSpace() const
Space the stops are interpolated in.
Definition gui_style_gradient.h:228
static Gradient Linear(const color4f &c0, const color4f &c1, const color4f &c2)
Three stops at 0, 0.5 and 1.
Definition gui_style_gradient.h:150
static Gradient Linear(const color4f &c0, const color4f &c1, const color4f &c2, const color4f &c3)
Four stops at 0, 0.25, 0.5 and 1.
Definition gui_style_gradient.h:172
Definition gui_base_types.h:383
Definition gui_base_types.h:394
float b
Definition gui_base_types.h:395
float r
Definition gui_base_types.h:395
float a
Definition gui_base_types.h:395
float g
Definition gui_base_types.h:395