TinyMaterialDesign
Loading...
Searching...
No Matches
MaterialTheme.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyGPU/Fonts.h"
9#include "TinyMaterialDesign/Theme/MaterialColors.h"
10
11namespace tinymd {
12
13/// Per-channel linear blend between two colors (t=0 -> a, t=1 -> b), used
14/// for pressed/disabled state shading, cheap elevation "shadows", and the
15/// ripple fade. RGB_T has no native alpha channel, so this is the cheapest
16/// approximation that still looks right at typical embedded-UI sizes.
17template <typename RGB_T>
18RGB_T blend(RGB_T a, RGB_T b, float t) {
19 t = std::min(1.0f, std::max(0.0f, t));
20 auto lerp = [t](uint8_t x, uint8_t y) {
21 return static_cast<uint8_t>(x + (static_cast<float>(y) - x) * t);
22 };
23 return RGB_T(lerp(a.getRed(), b.getRed()), lerp(a.getGreen(), b.getGreen()),
24 lerp(a.getBlue(), b.getBlue()));
25}
26
27/// Material 3 shape-scale corner radii, in pixels.
29 int32_t small = 4;
30 int32_t medium = 8;
31 int32_t large = 16;
32 /// "Fully rounded" (pills, circular thumbs) - callers pass this straight
33 /// into fillRoundRect()/drawRoundRect(), which clamp it to min(w,h)/2, so
34 /// any generously-large constant works regardless of the widget's size.
35 int32_t full = 1000;
36};
37
38/// Typography roles, each backed by one of TinyGPU's bitmap fonts.
39template <typename RGB_T>
40struct Typography {
41 tinygpu::IFont<RGB_T>* label; // Font5x7 - buttons, chips, captions
42 tinygpu::IFont<RGB_T>* body; // Font8x8 - card/dialog body text
43 tinygpu::IFont<RGB_T>* title; // Font12x12 - card/dialog/app bar titles
44 tinygpu::IFont<RGB_T>* headline; // Font16x24 - large standalone headings
45};
46
47/// @brief Bundles a color scheme, shape tokens, typography, and spacing into
48/// one theme, passed to widgets so they all draw with consistent styling.
49template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
54 int32_t spacing = 8;
55
56 /// Grows/fades a circle from the touch point on Button taps. Set false on
57 /// very constrained targets to skip the per-frame update()/redraw cost.
58 bool enableRipple = true;
59};
60
61/// Function-local static fonts, constructed once on first use - the same
62/// "kept as static instances" idiom TinyGPU's own basic-example.ino uses,
63/// just centralized here instead of left to each sketch.
64template <typename RGB_T>
66 static tinygpu::Font5x7<RGB_T> label;
67 static tinygpu::Font8x8<RGB_T> body;
68 static tinygpu::Font12x12<RGB_T> title;
69 static tinygpu::Font16x24<RGB_T> headline;
70 return Typography<RGB_T>{&label, &body, &title, &headline};
71}
72
73template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
75 MaterialTheme<RGB_T> theme;
76 theme.colors = lightColorScheme<RGB_T>();
77 theme.typography = defaultTypography<RGB_T>();
78 return theme;
79}
80
81template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
83 MaterialTheme<RGB_T> theme;
84 theme.colors = darkColorScheme<RGB_T>();
85 theme.typography = defaultTypography<RGB_T>();
86 return theme;
87}
88
89/// Additional named seed-hue themes (see MaterialColors.h's
90/// colorSchemeFromHue()) - same shape as defaultTheme()/defaultDarkTheme(),
91/// just with a different accent hue baked into the color scheme.
92template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
94 MaterialTheme<RGB_T> theme;
95 theme.colors = blueColorScheme<RGB_T>();
96 theme.typography = defaultTypography<RGB_T>();
97 return theme;
98}
99
100template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
102 MaterialTheme<RGB_T> theme;
103 theme.colors = blueDarkColorScheme<RGB_T>();
104 theme.typography = defaultTypography<RGB_T>();
105 return theme;
106}
107
108template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
110 MaterialTheme<RGB_T> theme;
111 theme.colors = greenColorScheme<RGB_T>();
112 theme.typography = defaultTypography<RGB_T>();
113 return theme;
114}
115
116template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
118 MaterialTheme<RGB_T> theme;
119 theme.colors = greenDarkColorScheme<RGB_T>();
120 theme.typography = defaultTypography<RGB_T>();
121 return theme;
122}
123
124template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
126 MaterialTheme<RGB_T> theme;
127 theme.colors = redColorScheme<RGB_T>();
128 theme.typography = defaultTypography<RGB_T>();
129 return theme;
130}
131
132template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
134 MaterialTheme<RGB_T> theme;
135 theme.colors = redDarkColorScheme<RGB_T>();
136 theme.typography = defaultTypography<RGB_T>();
137 return theme;
138}
139
140template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
142 MaterialTheme<RGB_T> theme;
143 theme.colors = orangeColorScheme<RGB_T>();
144 theme.typography = defaultTypography<RGB_T>();
145 return theme;
146}
147
148template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
150 MaterialTheme<RGB_T> theme;
151 theme.colors = orangeDarkColorScheme<RGB_T>();
152 theme.typography = defaultTypography<RGB_T>();
153 return theme;
154}
155
159
160} // namespace tinymd
Definition: AnchorLayout.h:4
MaterialTheme< RGB_T > greenDarkTheme()
Definition: MaterialTheme.h:117
MaterialTheme< RGB_T > greenTheme()
Definition: MaterialTheme.h:109
RGB_T blend(RGB_T a, RGB_T b, float t)
Definition: MaterialTheme.h:18
MaterialTheme< RGB_T > blueTheme()
Definition: MaterialTheme.h:93
MaterialTheme< RGB_T > defaultDarkTheme()
Definition: MaterialTheme.h:82
MaterialTheme< RGB_T > redDarkTheme()
Definition: MaterialTheme.h:133
MaterialTheme< RGB_T > blueDarkTheme()
Definition: MaterialTheme.h:101
Typography< RGB_T > defaultTypography()
Definition: MaterialTheme.h:65
MaterialTheme< RGB_T > redTheme()
Definition: MaterialTheme.h:125
MaterialTheme< RGB_T > orangeDarkTheme()
Definition: MaterialTheme.h:149
MaterialTheme< RGB_T > defaultTheme()
Definition: MaterialTheme.h:74
MaterialTheme< RGB_T > orangeTheme()
Definition: MaterialTheme.h:141
Material Design 3 color roles.
Definition: MaterialColors.h:24
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50
int32_t spacing
Definition: MaterialTheme.h:54
bool enableRipple
Definition: MaterialTheme.h:58
Typography< RGB_T > typography
Definition: MaterialTheme.h:53
ColorScheme< RGB_T > colors
Definition: MaterialTheme.h:51
ShapeTokens shape
Definition: MaterialTheme.h:52
Material 3 shape-scale corner radii, in pixels.
Definition: MaterialTheme.h:28
int32_t large
Definition: MaterialTheme.h:31
int32_t small
Definition: MaterialTheme.h:29
int32_t medium
Definition: MaterialTheme.h:30
int32_t full
Definition: MaterialTheme.h:35
Typography roles, each backed by one of TinyGPU's bitmap fonts.
Definition: MaterialTheme.h:40
tinygpu::IFont< RGB_T > * title
Definition: MaterialTheme.h:43
tinygpu::IFont< RGB_T > * body
Definition: MaterialTheme.h:42
tinygpu::IFont< RGB_T > * label
Definition: MaterialTheme.h:41
tinygpu::IFont< RGB_T > * headline
Definition: MaterialTheme.h:44