roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
gradient.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <initializer_list>
5#include <utility>
6#include <vector>
7
14#include "roo_logging.h"
15
16namespace roo_display {
17
18/// Multi-point gradient specification.
20 public:
21 /// A single node in the gradient.
22 struct Node {
23 float value;
25 };
26
27 /// Boundary behavior outside gradient range.
28 enum class Boundary {
29 // Boundary values are extended to infinity.
31
32 // color::Transparent is assumed outside the boundary.
34
35 // The gradient repeats periodically. The end boundary of the previous
36 // repetition is the start boundary of a next repetition.
38 };
39
40 [[deprecated("Use `ColorGradient::Boundary::kExtended` instead.")]]
42 [[deprecated("Use `ColorGradient::Boundary::kTruncated` instead.")]]
44 [[deprecated("Use `ColorGradient::Boundary::kPeriodic` instead.")]]
46
47 /// Create a gradient specification.
48 ///
49 /// Node list must contain at least one node for EXTENDED/TRUNCATED, and at
50 /// least two nodes with different values for PERIODIC. Nodes must be sorted
51 /// by value. Equal successive values create sharp transitions.
52 ColorGradient(std::vector<Node> gradient,
54
55 /// Return the color for a given value.
56 ///
57 /// Color is linearly interpolated between surrounding nodes. Values outside
58 /// the range follow the boundary specification.
59 Color getColor(float value) const;
60
61 /// Return the transparency mode of the gradient.
62 TransparencyMode getTransparencyMode() const { return transparency_mode_; }
63
64 private:
65 std::vector<Node> gradient_;
66 Boundary boundary_;
67 TransparencyMode transparency_mode_;
68 float inv_period_;
69};
70
71roo_logging::Stream& operator<<(roo_logging::Stream& os,
73
74/// Radial gradient based on distance from the center.
76 public:
77 /// Create a radial gradient.
78 ///
79 /// Value equals distance from `center`. No anti-aliasing is performed; keep
80 /// node distances >= 1 to avoid jagged rings. For smooth outer edges, add a
81 /// terminal node with `color::Transparent` at +1 distance.
84
85 Box extents() const override { return extents_; }
86
87 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
88 Color* result) const override;
89
90 private:
91 float cx_;
92 float cy_;
93 ColorGradient gradient_;
94 Box extents_;
95};
96
97/// Radial gradient using squared distance (faster, area-uniform).
99 public:
100 /// Create a radial gradient using squared distance.
101 ///
102 /// Node values must be squared distances.
105
106 Box extents() const override { return extents_; }
107
108 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
109 Color* result) const override;
110
111 private:
112 int16_t cx_;
113 int16_t cy_;
114 ColorGradient gradient_;
115 Box extents_;
116};
117
118/// Linear gradient (horizontal, vertical, or skewed).
120 public:
121 /// Create a linear gradient.
122 ///
123 /// Color value is computed as:
124 /// \f$(x - origin.x) * dx + (y - origin.y) * dy\f$.
125 /// Use `dx = 0` for vertical, `dy = 0` for horizontal gradients.
126 /// No anti-aliasing is performed; for skewed gradients keep a minimum node
127 /// spacing of \f$\sqrt{1/dx^2 + 1/dy^2}\f$.
128 LinearGradient(Point origin, float dx, float dy, ColorGradient gradient,
130
131 Box extents() const override { return extents_; }
132
133 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
134 Color* result) const override;
135
136 bool readColorRect(int16_t xMin, int16_t yMin, int16_t xMax, int16_t yMax,
137 Color* result) const override;
138
139 private:
140 int16_t cx_;
141 int16_t cy_;
142 float dx_;
143 float dy_;
144 ColorGradient gradient_;
145 Box extents_;
146};
147
148/// Create a vertical gradient: \f$val = (x - x0) * dx\f$.
151 Box extents = Box::MaximumBox()) {
152 return LinearGradient({x0, 0}, dx, 0, gradient, extents);
153}
154
155/// Create a horizontal gradient: \f$val = (y - y0) * dy\f$.
158 Box extents = Box::MaximumBox()) {
159 return LinearGradient({0, y0}, 0, dy, gradient, extents);
160}
161
162/// Angular gradient based on angle around `center`.
164 public:
165 /// Create an angular gradient.
166 ///
167 /// Gradient values are interpreted from \f$-\pi\f$ (South) through
168 /// \f$0\f$ (North) to \f$\pi\f$ (South). For other angle conventions, use a
169 /// periodic gradient.
172
173 Box extents() const override { return extents_; }
174
175 void readColors(const int16_t* x, const int16_t* y, uint32_t count,
176 Color* result) const override;
177
178 private:
179 float cx_;
180 float cy_;
181 ColorGradient gradient_;
182 Box extents_;
183};
184
185} // namespace roo_display
Angular gradient based on angle around center.
Definition gradient.h:163
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Definition gradient.cpp:230
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition gradient.h:173
Axis-aligned integer rectangle.
Definition box.h:12
static Box MaximumBox()
Return a large sentinel box used for unbounded extents.
Definition box.h:59
Multi-point gradient specification.
Definition gradient.h:19
static constexpr Boundary EXTENDED
Definition gradient.h:41
Color getColor(float value) const
Return the color for a given value.
Definition gradient.cpp:24
Boundary
Boundary behavior outside gradient range.
Definition gradient.h:28
TransparencyMode getTransparencyMode() const
Return the transparency mode of the gradient.
Definition gradient.h:62
static constexpr Boundary TRUNCATED
Definition gradient.h:43
static constexpr Boundary PERIODIC
Definition gradient.h:45
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
Linear gradient (horizontal, vertical, or skewed).
Definition gradient.h:119
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition gradient.h:131
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Definition gradient.cpp:149
bool readColorRect(int16_t xMin, int16_t yMin, int16_t xMax, int16_t yMax, Color *result) const override
Read colors for a rectangle.
Definition gradient.cpp:178
Radial gradient using squared distance (faster, area-uniform).
Definition gradient.h:98
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Definition gradient.cpp:127
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition gradient.h:106
Radial gradient based on distance from the center.
Definition gradient.h:75
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Definition gradient.cpp:107
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition gradient.h:85
Drawable that can provide a color for any point within its extents.
Defines 140 opaque HTML named colors.
LinearGradient VerticalGradient(int16_t x0, float dx, ColorGradient gradient, Box extents=Box::MaximumBox())
Create a vertical gradient: .
Definition gradient.h:149
TransparencyMode
Transparency information for a stream or color mode.
Definition blending.h:103
LinearGradient HorizontalGradient(int16_t y0, float dy, ColorGradient gradient, Box extents=Box::MaximumBox())
Create a horizontal gradient: .
Definition gradient.h:156
roo_logging::Stream & operator<<(roo_logging::Stream &os, BlendingMode mode)
Definition blending.cpp:54
A single node in the gradient.
Definition gradient.h:22
Floating-point point.
Definition point.h:12
Integer point.
Definition point.h:6