Skip to main content

ferrochart_core/
theme.rs

1//! Themes: plain data (colors, sizes). All colors are resolved by the core before
2//! the frame is built; backends never see "up" or "down" semantics beyond the
3//! per-draw [`SeriesStyle`](crate::frame::SeriesStyle).
4//!
5
6use crate::geom::{Css, Rgba};
7
8/// Colors and sizes of a chart.
9///
10/// # Examples
11///
12/// ```
13/// use ferrochart_core::theme::Theme;
14///
15/// let dark = Theme::dark();
16/// assert_ne!(dark.background, Theme::light().background);
17/// assert_eq!(Theme::default(), Theme::light());
18/// ```
19#[derive(Clone, Copy, Debug, PartialEq)]
20pub struct Theme {
21    /// Chart background.
22    pub background: Rgba,
23    /// Axis label text.
24    pub text: Rgba,
25    /// Grid lines.
26    pub grid: Rgba,
27    /// Axis border lines.
28    pub axis_line: Rgba,
29    /// Up candle body.
30    pub up: Rgba,
31    /// Down candle body.
32    pub down: Rgba,
33    /// Up candle wick.
34    pub up_wick: Rgba,
35    /// Down candle wick.
36    pub down_wick: Rgba,
37    /// Volume bar of an up bar.
38    pub volume_up: Rgba,
39    /// Volume bar of a down bar.
40    pub volume_down: Rgba,
41    /// Crosshair lines.
42    pub crosshair: Rgba,
43    /// Background of the crosshair's axis labels.
44    pub crosshair_label_bg: Rgba,
45    /// Text of the crosshair's axis labels.
46    pub crosshair_label_text: Rgba,
47    /// Axis label font size.
48    pub font_size: Css,
49    /// Padding around axis labels.
50    pub axis_padding: Css,
51    /// Default color of line, area and histogram series.
52    pub line: Rgba,
53}
54
55const UP: Rgba = Rgba::hex(0x0026_A69A);
56const DOWN: Rgba = Rgba::hex(0x00EF_5350);
57
58impl Theme {
59    /// The built-in light theme.
60    pub const fn light() -> Self {
61        Self {
62            background: Rgba::hex(0x00FF_FFFF),
63            text: Rgba::hex(0x001F_232B),
64            grid: Rgba::hex(0x00EE_F0F4),
65            axis_line: Rgba::hex(0x00D5_D9E0),
66            up: UP,
67            down: DOWN,
68            up_wick: UP,
69            down_wick: DOWN,
70            volume_up: UP.with_alpha(110),
71            volume_down: DOWN.with_alpha(110),
72            crosshair: Rgba::hex(0x0095_98A1),
73            crosshair_label_bg: Rgba::hex(0x0013_1722),
74            crosshair_label_text: Rgba::hex(0x00FF_FFFF),
75            font_size: Css(11.0),
76            axis_padding: Css(6.0),
77            line: Rgba::hex(0x0029_62FF),
78        }
79    }
80
81    /// The built-in dark theme.
82    pub const fn dark() -> Self {
83        Self {
84            background: Rgba::hex(0x0013_161C),
85            text: Rgba::hex(0x00C9_CDD6),
86            grid: Rgba::hex(0x0021_292F),
87            axis_line: Rgba::hex(0x003A_404A),
88            crosshair: Rgba::hex(0x0075_8696),
89            crosshair_label_bg: Rgba::hex(0x0036_3A45),
90            ..Self::light()
91        }
92    }
93}
94
95impl Default for Theme {
96    fn default() -> Self {
97        Self::light()
98    }
99}