Skip to main content

sealed_surface/
appearance.rs

1//! Optional look: type scale, chart style, composed appearance.
2//!
3//! [`Appearance::default`] uses compact 5×7 glyphs (same metrics as 0.2.0).
4//! [`TypeScale::large`] is 2×; [`TypeScale::xlarge`] is 3× — opt-in only.
5
6use crate::theme::{Rgb, Theme};
7
8/// Glyph size and cell metrics.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum TypeScale {
12    /// 5×7, row 18. Default (same metrics as 0.2.0).
13    #[default]
14    Compact,
15    /// 5×7 at 2× (10×14). Opt-in; hard to read for long tables.
16    Large,
17    /// 5×7 at 3× (15×21).
18    XLarge,
19}
20
21impl TypeScale {
22    /// Compact 5×7 (default).
23    #[must_use]
24    pub const fn compact() -> Self {
25        Self::Compact
26    }
27
28    /// 2× scale.
29    #[must_use]
30    pub const fn large() -> Self {
31        Self::Large
32    }
33
34    /// 3× scale.
35    #[must_use]
36    pub const fn xlarge() -> Self {
37        Self::XLarge
38    }
39
40    /// Integer scale of the 5×7 atlas.
41    #[must_use]
42    pub const fn pixel(self) -> u32 {
43        match self {
44            Self::Compact => 1,
45            Self::Large => 2,
46            Self::XLarge => 3,
47        }
48    }
49
50    /// Advance including 1px (× scale) gap.
51    #[must_use]
52    pub const fn advance(self) -> u32 {
53        6 * self.pixel()
54    }
55
56    /// Body row height.
57    #[must_use]
58    pub const fn row_height(self) -> u32 {
59        match self {
60            Self::Compact => 18,
61            Self::Large => 28,
62            Self::XLarge => 40,
63        }
64    }
65
66    /// Sticky header height.
67    #[must_use]
68    pub const fn header_height(self) -> u32 {
69        match self {
70            Self::Compact => 20,
71            Self::Large => 32,
72            Self::XLarge => 44,
73        }
74    }
75
76    /// Outer padding.
77    #[must_use]
78    pub const fn pad(self) -> u32 {
79        match self {
80            Self::Compact => 8,
81            Self::Large => 12,
82            Self::XLarge => 16,
83        }
84    }
85
86    /// Horizontal cell padding.
87    #[must_use]
88    pub const fn cell_pad_x(self) -> u32 {
89        match self {
90            Self::Compact => 8,
91            Self::Large => 12,
92            Self::XLarge => 16,
93        }
94    }
95
96    /// Auto column min width.
97    #[must_use]
98    pub const fn min_col(self) -> u32 {
99        match self {
100            Self::Compact => 56,
101            Self::Large => 88,
102            Self::XLarge => 120,
103        }
104    }
105
106    /// Auto column max width.
107    #[must_use]
108    pub const fn max_col(self) -> u32 {
109        match self {
110            Self::Compact => 240,
111            Self::Large => 360,
112            Self::XLarge => 480,
113        }
114    }
115
116    /// Y-axis label gutter for charts.
117    #[must_use]
118    pub const fn plot_left(self) -> u32 {
119        self.advance().saturating_mul(7).saturating_add(self.pad())
120    }
121
122    /// X-axis label band.
123    #[must_use]
124    pub const fn plot_bottom(self) -> u32 {
125        self.row_height().saturating_add(4)
126    }
127}
128
129/// Where to draw series markers.
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
131#[non_exhaustive]
132pub enum MarkerStyle {
133    /// No vertex dots.
134    None,
135    /// Last sample only.
136    Last,
137    /// Hovered / nearest sample (default).
138    #[default]
139    Hover,
140}
141
142/// Timeseries look. Independent of table metrics.
143#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144#[non_exhaustive]
145pub struct ChartStyle {
146    /// Polyline thickness in pixels (1–4). Default 2.
147    pub stroke_px: u8,
148    /// Fill under the line.
149    pub area_fill: bool,
150    /// Area blend 0–255 against the plot background.
151    pub area_alpha: u8,
152    /// Target tick count (nice numbers may differ).
153    pub tick_count: u8,
154    /// 1–2–5 × 10^n ticks.
155    pub nice_ticks: bool,
156    /// Up to four series colors.
157    pub series: [Rgb; 4],
158    /// Plot well (distinct from page background).
159    pub plot_background: Rgb,
160    /// Vertex markers.
161    pub markers: MarkerStyle,
162}
163
164impl Default for ChartStyle {
165    fn default() -> Self {
166        Self::from_theme(&Theme::default())
167    }
168}
169
170impl ChartStyle {
171    /// Derive a chart style from a theme (plot well + series palette).
172    #[must_use]
173    pub fn from_theme(theme: &Theme) -> Self {
174        Self {
175            stroke_px: 2,
176            area_fill: true,
177            area_alpha: 48,
178            tick_count: 5,
179            nice_ticks: true,
180            series: [
181                theme.series,
182                Rgb::new(0x3e, 0xc8, 0xbf),
183                theme.crosshair,
184                Rgb::new(0xc0, 0x84, 0xfc),
185            ],
186            plot_background: Rgb::new(
187                theme.background.r.saturating_add(14),
188                theme.background.g.saturating_add(16),
189                theme.background.b.saturating_add(22),
190            ),
191            markers: MarkerStyle::Hover,
192        }
193    }
194}
195
196/// Full look. Default is compact 5×7 + dark high-readability theme.
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
198#[non_exhaustive]
199pub struct Appearance {
200    /// Colors.
201    pub theme: Theme,
202    /// Glyph / cell metrics.
203    pub type_scale: TypeScale,
204    /// Timeseries stroke/fill/ticks.
205    pub chart: ChartStyle,
206}
207
208impl Default for Appearance {
209    fn default() -> Self {
210        let theme = Theme::default();
211        Self {
212            chart: ChartStyle::from_theme(&theme),
213            theme,
214            type_scale: TypeScale::Compact,
215        }
216    }
217}
218
219impl Appearance {
220    /// Compact 5×7 (same as [`Default`]).
221    #[must_use]
222    pub fn compact() -> Self {
223        Self::default()
224    }
225
226    /// Light page, dark text, compact glyphs.
227    #[must_use]
228    pub fn light() -> Self {
229        let theme = Theme::light();
230        Self {
231            chart: ChartStyle::from_theme(&theme),
232            theme,
233            type_scale: TypeScale::Compact,
234        }
235    }
236
237    /// Near-max contrast (dark), compact glyphs.
238    #[must_use]
239    pub fn high_contrast() -> Self {
240        let theme = Theme::high_contrast();
241        Self {
242            chart: ChartStyle::from_theme(&theme),
243            theme,
244            type_scale: TypeScale::Compact,
245        }
246    }
247
248    /// 2× glyphs.
249    #[must_use]
250    pub fn large() -> Self {
251        Self {
252            type_scale: TypeScale::Large,
253            ..Self::default()
254        }
255    }
256
257    /// 3× glyphs.
258    #[must_use]
259    pub fn xlarge() -> Self {
260        Self {
261            type_scale: TypeScale::XLarge,
262            ..Self::default()
263        }
264    }
265}