guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
tokens.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/theme/tokens.hpp"
24
25#include <cmath>
26#include <stdexcept>
27
28namespace guillaume::theme
29{
30 namespace
31 {
32 template<typename Enum> std::size_t toIndex(Enum value)
33 {
34 const auto index = static_cast<std::size_t>(value);
35 if (index >= static_cast<std::size_t>(Enum::Count)) {
36 throw std::out_of_range("Invalid theme token");
37 }
38 return index;
39 }
40 } // namespace
41
42 float ShapeScale::getRoundness(void) const
43 {
44 return _roundness;
45 }
46
47 void ShapeScale::setRoundness(float roundness)
48 {
49 if (roundness < 0.0f) {
50 throw std::invalid_argument("Roundness must be non-negative");
51 }
52 _roundness = roundness;
53 }
54
56 {
57 static constexpr std::array<float,
58 static_cast<std::size_t>(ShapeRole::Count)>
59 base { 0.0f, 4.0f, 8.0f, 12.0f, 16.0f, 28.0f, FullRadius };
60
61 const auto index = toIndex(role);
62 if (role == ShapeRole::None || role == ShapeRole::Full) {
63 return base[index];
64 }
65 return base[index] * (_roundness / DefaultRoundness);
66 }
67
69 : _tokens { {
70 { 0.00f, 0.0f, 0 },
71 { 0.05f, 3.0f, 48 },
72 { 0.08f, 6.0f, 56 },
73 { 0.11f, 8.0f, 64 },
74 { 0.12f, 10.0f, 72 },
75 { 0.14f, 12.0f, 80 },
76 } }
77 {
78 }
79
81 {
82 return _tokens[toIndex(level)];
83 }
84
86 {
87 _tokens[toIndex(level)] = token;
88 }
89
90 utility::graphic::Color32Bit
91 ElevationScale::tint(const utility::graphic::Color32Bit &surface,
92 const utility::graphic::Color32Bit &tint,
93 ElevationLevel level) const
94 {
95 const float opacity = get(level).tintOpacity;
96 const auto alpha =
97 static_cast<std::uint8_t>(std::lround(opacity * 255.0f));
98 return tint.withAlpha(alpha).blendOver(surface);
99 }
100
102 : _opacities { 0.08f, 0.12f, 0.12f, 0.16f, 0.38f, 0.12f }
103 {
104 }
105
107 {
108 return _opacities[toIndex(layer)];
109 }
110
111 std::uint8_t StateLayerScale::getAlpha(StateLayer layer) const
112 {
113 return static_cast<std::uint8_t>(
114 std::lround(getOpacity(layer) * 255.0f));
115 }
116
117 void StateLayerScale::setOpacity(StateLayer layer, float opacity)
118 {
119 const auto index = toIndex(layer);
120 if (opacity < 0.0f || opacity > 1.0f) {
121 throw std::invalid_argument("Opacity must be within [0, 1]");
122 }
123 _opacities[index] = opacity;
124 }
125
127 : _durations { 0.05f, 0.10f, 0.15f, 0.20f, 0.25f, 0.30f, 0.35f, 0.40f,
128 0.45f, 0.50f, 0.55f, 0.60f, 0.70f, 0.80f, 0.90f, 1.00f }
129 {
130 }
131
133 {
134 return _durations[toIndex(duration)];
135 }
136
137 void MotionScale::setDuration(MotionDuration duration, float seconds)
138 {
139 const auto index = toIndex(duration);
140 if (seconds <= 0.0f) {
141 throw std::invalid_argument("Duration must be positive");
142 }
143 _durations[index] = seconds;
144 }
145
146} // namespace guillaume::theme
void set(ElevationLevel level, const ElevationToken &token)
Replace the shadow and tint of a level.
Definition tokens.cpp:85
const ElevationToken & get(ElevationLevel level) const
Get the shadow and tint of a level.
Definition tokens.cpp:80
utility::graphic::Color32Bit tint(const utility::graphic::Color32Bit &surface, const utility::graphic::Color32Bit &tint, ElevationLevel level) const
Compute the color of a surface raised to a level: the tint color blended over the surface color at th...
Definition tokens.cpp:91
ElevationScale(void)
Create the scale with the Material 3 baseline values.
Definition tokens.cpp:68
float getDuration(MotionDuration duration) const
Get the length of a duration token.
Definition tokens.cpp:132
void setDuration(MotionDuration duration, float seconds)
Replace the length of a duration token.
Definition tokens.cpp:137
MotionScale(void)
Create the scale with the Material 3 baseline durations listed in MotionDuration.
Definition tokens.cpp:126
static constexpr float DefaultRoundness
The roundness for which each role has the radius listed in ShapeRole.
Definition tokens.hpp:86
float getRoundness(void) const
Get the roundness of the scale.
Definition tokens.cpp:42
static constexpr float FullRadius
Radius returned for ShapeRole::Full, in dp. Renderers clamp radii to half the smallest extent,...
Definition tokens.hpp:80
void setRoundness(float roundness)
Change the roundness, which rescales every role except None and Full.
Definition tokens.cpp:47
float getRadius(ShapeRole role) const
Get the corner radius of a role for the current roundness.
Definition tokens.cpp:55
void setOpacity(StateLayer layer, float opacity)
Replace the opacity of a state layer.
Definition tokens.cpp:117
std::uint8_t getAlpha(StateLayer layer) const
Get the opacity of a state layer as an alpha channel value.
Definition tokens.cpp:111
StateLayerScale(void)
Create the scale with the Material 3 baseline opacities.
Definition tokens.cpp:101
float getOpacity(StateLayer layer) const
Get the opacity of a state layer.
Definition tokens.cpp:106
Design tokens: colors, type scale, shapes, elevation, motion and spacing. Read them through guillaume...
ElevationLevel
How far a surface is raised above the background, in six steps.
Definition tokens.hpp:130
StateLayer
The interaction states that change how a widget is drawn.
Definition tokens.hpp:223
MotionDuration
The named animation lengths of Material Design 3. The values given are the defaults.
Definition tokens.hpp:290
ShapeRole
The corner radii a widget can ask for, by name.
Definition tokens.hpp:42
@ None
0 dp: square corners.
Definition tokens.hpp:43
@ Full
Fully rounded (buttons, icon buttons, badges).
Definition tokens.hpp:49
@ Count
Number of roles; not a role.
Definition tokens.hpp:50
What one elevation level looks like: how much the surface is tinted and how large and dark its shadow...
Definition tokens.hpp:144