roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
basic.h
Go to the documentation of this file.
1#pragma once
2
3#include "roo_display.h"
7
8namespace roo_display {
9
10/// Base class for simple colored shapes.
11class BasicShape : virtual public Drawable {
12 public:
13 /// Return the shape color.
14 Color color() const { return color_; }
15
16 protected:
18
19 private:
20 Color color_;
21};
22
23/// A straight line segment between two points.
24class Line : public BasicShape {
25 public:
26 Line(Point a, Point b, Color color) : Line(a.x, a.y, b.x, b.y, color) {}
27
29 : BasicShape(color), x0_(x0), y0_(y0), x1_(x1), y1_(y1), diag_(TL_BR) {
30 if (x1 < x0) {
31 std::swap(x0_, x1_);
32 diag_ = diag_ == TL_BR ? TR_BL : TL_BR;
33 }
34 if (y1 < y0) {
35 std::swap(y0_, y1_);
36 diag_ = diag_ == TL_BR ? TR_BL : TL_BR;
37 }
38 }
39
40 Box extents() const override { return Box(x0_, y0_, x1_, y1_); }
41
42 private:
43 enum Diagonal { TL_BR = 0, TR_BL = 1 };
44
45 void drawTo(const Surface &s) const override;
46
47 bool steep() const { return y1_ - y0_ > x1_ - x0_; }
48
49 int16_t x0_;
50 int16_t y0_;
51 int16_t x1_;
52 int16_t y1_;
53 Diagonal diag_;
54};
55
56/// Base class for axis-aligned rectangle shapes.
57class RectBase : public BasicShape {
58 public:
60 : BasicShape(color), x0_(x0), y0_(y0), x1_(x1), y1_(y1) {
61 if (x1 < x0) std::swap(x0_, x1_);
62 if (y1 < y0) std::swap(y0_, y1_);
63 }
64
65 Box extents() const override { return Box(x0_, y0_, x1_, y1_); }
66
67 protected:
72};
73
74/// Outline rectangle.
75class Rect : public RectBase {
76 public:
77 Rect(const Box &box, Color color)
78 : RectBase(box.xMin(), box.yMin(), box.xMax(), box.yMax(), color) {}
79
80 Rect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
81 : RectBase(x0, y0, x1, y1, color) {}
82
83 private:
84 void drawTo(const Surface &s) const override;
85};
86
87/// Rectangle border with configurable thickness on each side.
88class Border : public RectBase {
89 public:
90 Border(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t border,
91 Color color)
92 : Border(x0, y0, x1, y1, border, border, border, border, color) {}
93
95 int16_t vborder, Color color)
96 : Border(x0, y0, x1, y1, hborder, vborder, hborder, vborder, color) {}
97
99 Color color)
100 : Border(outer.xMin(), outer.yMin(), outer.xMax(), outer.yMax(),
101 inner.xMin() - outer.xMin(), inner.yMin() - outer.yMin(),
102 outer.xMax() - inner.xMax(), outer.yMax() - inner.yMax(),
103 color) {}
104
106 int16_t top, int16_t right, int16_t bottom, Color color)
107 : RectBase(x0, y0, x1, y1, color),
108 left_(left < 0 ? 0 : left),
109 top_(top < 0 ? 0 : top),
110 right_(right < 0 ? 0 : right),
111 bottom_(bottom < 0 ? 0 : bottom) {
112 if ((x0_ + left_ + 1 >= x1_ - right_) ||
113 (y0_ + top_ + 1 >= y1_ - bottom_)) {
114 // Full filled rect.
115 left_ = x1_ - x0_ + 1;
116 right_ = 0;
117 top_ = 0;
118 bottom_ = 0;
119 }
120 }
121
122 private:
123 int16_t left_;
124 int16_t top_;
125 int16_t right_;
126 int16_t bottom_;
127 void drawTo(const Surface &s) const override;
128};
129
130/// Filled rectangle (rasterizable).
131class FilledRect : public RectBase, public Rasterizable {
132 public:
133 FilledRect(const Box &box, Color color)
134 : RectBase(box.xMin(), box.yMin(), box.xMax(), box.yMax(), color) {}
135
137 : RectBase(x0, y0, x1, y1, color) {}
138
139 Box extents() const override { return RectBase::extents(); }
140
145
146 std::unique_ptr<PixelStream> createStream() const override;
147 std::unique_ptr<PixelStream> createStream(const Box &bounds) const override;
148
149 void readColors(const int16_t *x, const int16_t *y, uint32_t count,
150 Color *result) const override {
151 FillColor(result, count, color());
152 }
153
154 private:
155 void drawTo(const Surface &s) const override;
156};
157
158/// Base class for rounded rectangles.
159class RoundRectBase : public BasicShape {
160 public:
162 Color color)
163 : BasicShape(color), x0_(x0), y0_(y0), x1_(x1), y1_(y1), radius_(radius) {
164 if (x1 < x0) std::swap(x0_, x1_);
165 if (y1 < y0) std::swap(y0_, y1_);
166 int16_t w = x1 - x0 + 1;
167 int16_t h = y1 - y0 + 1;
168 int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
170 }
171
172 Box extents() const override { return Box(x0_, y0_, x1_, y1_); }
173
174 protected:
180};
181
182/// Outline rounded rectangle.
183class RoundRect : public RoundRectBase {
184 public:
186 Color color)
187 : RoundRectBase(x0, y0, x1, y1, radius, color) {}
188
189 private:
190 void drawInteriorTo(const Surface &s) const override;
191};
192
193/// Filled rounded rectangle.
195 public:
197 int16_t radius, Color color)
198 : RoundRectBase(x0, y0, x1, y1, radius, color) {}
199
200 private:
201 void drawTo(const Surface &s) const override;
202};
203
204/// Base class for circles.
205class CircleBase : public BasicShape {
206 public:
207 Box extents() const override {
208 return Box(x0_, y0_, x0_ + diameter_ - 1, y0_ + diameter_ - 1);
209 }
210
211 protected:
214
218};
219
220/// Outline circle.
221class Circle : public CircleBase {
222 public:
223 /// Create an outline circle by center point and radius.
224 ///
225 /// @param center Circle center.
226 /// @param radius Radius in pixels.
227 /// @param color Outline color.
228 static Circle ByRadius(Point center, int16_t radius, Color color) {
229 return ByRadius(center.x, center.y, radius, color);
230 }
231
232 /// Create an outline circle by center coordinates and radius.
233 ///
234 /// @param x_center Center x-coordinate.
235 /// @param y_center Center y-coordinate.
236 /// @param radius Radius in pixels.
237 /// @param color Outline color.
239 Color color) {
240 return Circle(x_center - radius, y_center - radius, (radius << 1) + 1,
241 color);
242 }
243
244 /// Create an outline circle by top-left and diameter.
245 ///
246 /// @param top_left Top-left corner of the bounding square.
247 /// @param diameter Diameter in pixels.
248 /// @param color Outline color.
252
253 /// Create an outline circle by top-left coordinates and diameter.
254 ///
255 /// @param x0 Top-left x-coordinate of the bounding square.
256 /// @param y0 Top-left y-coordinate of the bounding square.
257 /// @param diameter Diameter in pixels.
258 /// @param color Outline color.
260 Color color) {
261 return Circle(x0, y0, diameter, color);
262 }
263
264 private:
266 : CircleBase(x0, y0, diameter, color) {}
267
268 void drawInteriorTo(const Surface &s) const override;
269};
270
271/// Filled circle.
272class FilledCircle : public CircleBase {
273 public:
274 /// Create a filled circle by center point and radius.
275 ///
276 /// @param center Circle center.
277 /// @param radius Radius in pixels.
278 /// @param color Fill color.
280 return ByRadius(center.x, center.y, radius, color);
281 }
282
283 /// Create a filled circle by center coordinates and radius.
284 ///
285 /// @param x_center Center x-coordinate.
286 /// @param y_center Center y-coordinate.
287 /// @param radius Radius in pixels.
288 /// @param color Fill color.
290 int16_t radius, Color color) {
291 return FilledCircle(x_center - radius, y_center - radius, (radius << 1) + 1,
292 color);
293 }
294
295 /// Create a filled circle by top-left and diameter.
296 ///
297 /// @param top_left Top-left corner of the bounding square.
298 /// @param diameter Diameter in pixels.
299 /// @param color Fill color.
303
304 /// Create a filled circle by top-left coordinates and diameter.
305 ///
306 /// @param x0 Top-left x-coordinate of the bounding square.
307 /// @param y0 Top-left y-coordinate of the bounding square.
308 /// @param diameter Diameter in pixels.
309 /// @param color Fill color.
311 Color color) {
312 return FilledCircle(x0, y0, diameter, color);
313 }
314
315 private:
317 : CircleBase(x0, y0, diameter, color) {}
318
319 void drawTo(const Surface &s) const override;
320};
321
322class TriangleBase : public BasicShape {
323 public:
325 int16_t y2, Color color)
326 : BasicShape(color),
327 x0_(x0),
328 y0_(y0),
329 x1_(x1),
330 y1_(y1),
331 x2_(x2),
332 y2_(y2) {
333 // Sort coordinates by Y order (y2 >= y1 >= y0)
334 if (y0_ > y1_) {
335 std::swap(y0_, y1_);
336 std::swap(x0_, x1_);
337 }
338 if (y1_ > y2_) {
339 std::swap(y2_, y1_);
340 std::swap(x2_, x1_);
341 }
342 if (y0_ > y1_) {
343 std::swap(y0_, y1_);
344 std::swap(x0_, x1_);
345 }
346 }
347
348 Box extents() const override {
349 return Box(std::min(x0_, std::min(x1_, x2_)), y0_,
350 std::max(x0_, std::max(x1_, x2_)), y2_);
351 }
352
353 protected:
360};
361
362class Triangle : public TriangleBase {
363 public:
365 : Triangle(a.x, a.y, b.x, b.y, c.x, c.y, color) {}
366
368 int16_t y2, Color color)
369 : TriangleBase(x0, y0, x1, y1, x2, y2, color) {}
370
371 private:
372 void drawInteriorTo(const Surface &s) const override;
373};
374
376 public:
378 : FilledTriangle(a.x, a.y, b.x, b.y, c.x, c.y, color) {}
379
381 int16_t y2, Color color)
382 : TriangleBase(x0, y0, x1, y1, x2, y2, color) {}
383
384 private:
385 void drawInteriorTo(const Surface &s) const override;
386};
387
388} // namespace roo_display
Base class for simple colored shapes.
Definition basic.h:11
Color color() const
Return the shape color.
Definition basic.h:14
BasicShape(Color color)
Definition basic.h:17
Rectangle border with configurable thickness on each side.
Definition basic.h:88
Border(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t border, Color color)
Definition basic.h:90
Border(const roo_display::Box &outer, const roo_display::Box &inner, Color color)
Definition basic.h:98
Border(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t left, int16_t top, int16_t right, int16_t bottom, Color color)
Definition basic.h:105
Border(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t hborder, int16_t vborder, Color color)
Definition basic.h:94
Axis-aligned integer rectangle.
Definition box.h:12
Base class for circles.
Definition basic.h:205
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:207
CircleBase(int16_t x0, int16_t y0, int16_t diameter, Color color)
Definition basic.h:212
Outline circle.
Definition basic.h:221
static Circle ByRadius(int16_t x_center, int16_t y_center, int16_t radius, Color color)
Create an outline circle by center coordinates and radius.
Definition basic.h:238
static Circle ByExtents(int16_t x0, int16_t y0, int16_t diameter, Color color)
Create an outline circle by top-left coordinates and diameter.
Definition basic.h:259
static Circle ByExtents(Point top_left, int16_t diameter, Color color)
Create an outline circle by top-left and diameter.
Definition basic.h:249
static Circle ByRadius(Point center, int16_t radius, Color color)
Create an outline circle by center point and radius.
Definition basic.h:228
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
constexpr bool isOpaque() const
Return true if the color is fully opaque (alpha = 255).
Definition color.h:89
Interface for objects that can be drawn to an output device.
Definition drawable.h:229
Filled circle.
Definition basic.h:272
static FilledCircle ByExtents(int16_t x0, int16_t y0, int16_t diameter, Color color)
Create a filled circle by top-left coordinates and diameter.
Definition basic.h:310
static FilledCircle ByRadius(int16_t x_center, int16_t y_center, int16_t radius, Color color)
Create a filled circle by center coordinates and radius.
Definition basic.h:289
static FilledCircle ByExtents(Point top_left, int16_t diameter, Color color)
Create a filled circle by top-left and diameter.
Definition basic.h:300
static FilledCircle ByRadius(Point center, int16_t radius, Color color)
Create a filled circle by center point and radius.
Definition basic.h:279
Filled rectangle (rasterizable).
Definition basic.h:131
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:139
FilledRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
Definition basic.h:136
std::unique_ptr< PixelStream > createStream() const override
Create a stream covering the full extents().
Definition basic.cpp:180
FilledRect(const Box &box, Color color)
Definition basic.h:133
void readColors(const int16_t *x, const int16_t *y, uint32_t count, Color *result) const override
Read colors for the given points.
Definition basic.h:149
TransparencyMode getTransparencyMode() const override
Return the transparency mode for pixels in this stream.
Definition basic.h:141
Filled rounded rectangle.
Definition basic.h:194
FilledRoundRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t radius, Color color)
Definition basic.h:196
FilledTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, Color color)
Definition basic.h:380
FilledTriangle(Point a, Point b, Point c, Color color)
Definition basic.h:377
A straight line segment between two points.
Definition basic.h:24
Line(Point a, Point b, Color color)
Definition basic.h:26
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:40
Line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
Definition basic.h:28
Drawable that can provide a color for any point within its extents.
Base class for axis-aligned rectangle shapes.
Definition basic.h:57
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:65
RectBase(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
Definition basic.h:59
Outline rectangle.
Definition basic.h:75
Rect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color)
Definition basic.h:80
Rect(const Box &box, Color color)
Definition basic.h:77
Base class for rounded rectangles.
Definition basic.h:159
RoundRectBase(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t radius, Color color)
Definition basic.h:161
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:172
Outline rounded rectangle.
Definition basic.h:183
RoundRect(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t radius, Color color)
Definition basic.h:185
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
TriangleBase(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, Color color)
Definition basic.h:324
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition basic.h:348
Triangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, Color color)
Definition basic.h:367
Triangle(Point a, Point b, Point c, Color color)
Definition basic.h:364
Defines 140 opaque HTML named colors.
TransparencyMode
Transparency information for a stream or color mode.
Definition blending.h:103
@ kNone
All colors are fully opaque.
@ kFull
Colors may include partial transparency (alpha channel).
void FillColor(Color *buf, uint32_t count, Color color)
Fill an array with a single color.
Definition color.h:109
Public API surface for roo_display display, touch, and drawing utilities.
Integer point.
Definition point.h:6