egui-charts 0.2.0

High-performance financial charting engine for egui — candlesticks, 95 drawing tools, 130+ indicators, and a full design-token theme system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Semantic Tokens - Meaning-based color access
//!
//! Semantic tokens map raw palette colors to their intended USE in the application.
//! This is the layer that components actually consume.
//!
//! # Why Semantic Tokens?
//!
//! Instead of:
//! ```ignore
//! let color = DESIGN_TOKENS.semantic.ui.panel_bg_light;  // Which one to use?
//! ```
//!
//! You write:
//! ```ignore
//! let color = theme.semantic.ui.panel_bg;  // Already resolved for current theme!
//! ```
//!
//! # Architecture
//!
//! This module builds theme-resolved semantic tokens from `DESIGN_TOKENS`,
//! which contains both light and dark variants. Based on the `ThemePreset`'s
//! `is_dark_ui()` and `is_dark_chart()` flags, we select the appropriate colors.

use crate::tokens::DESIGN_TOKENS;
use egui::Color32;

// ============================================================================
// SEMANTIC UI TOKENS
// ============================================================================

/// Semantic tokens for UI chrome (toolbars, panels, menus)
/// These are resolved based on the theme's light/dark mode.
#[derive(Clone, Copy, Debug)]
pub struct UiSemanticTokens {
    // Panel backgrounds
    pub panel_bg: Color32,
    pub panel_bg_secondary: Color32,
    pub panel_bg_floating: Color32,

    // Toolbar
    pub toolbar_bg: Color32,
    pub toolbar_separator: Color32,

    // Button states
    pub btn_bg: Color32,
    pub btn_bg_hover: Color32,
    pub btn_bg_active: Color32,
    pub btn_bg_disabled: Color32,

    // Text
    pub text: Color32,
    pub text_secondary: Color32,
    pub text_muted: Color32,
    pub text_disabled: Color32,
    pub text_link: Color32,

    // Icons
    pub icon: Color32,
    pub icon_hover: Color32,
    pub icon_active: Color32,
    pub icon_disabled: Color32,

    // Borders
    pub border: Color32,
    pub border_subtle: Color32,
    pub border_focus: Color32,

    // Selection
    pub selection_bg: Color32,
    pub selection_text: Color32,

    // Accent (for primary actions)
    pub accent: Color32,
    pub accent_hover: Color32,
    pub accent_active: Color32,
    pub accent_text: Color32,

    // Status colors
    pub success: Color32,
    pub warning: Color32,
    pub error: Color32,
    pub info: Color32,
}

impl UiSemanticTokens {
    /// Build UI semantic tokens from DESIGN_TOKENS based on dark mode flag
    pub fn from_design_tokens(is_dark_ui: bool) -> Self {
        let tokens = &DESIGN_TOKENS.semantic;

        if is_dark_ui {
            Self {
                panel_bg: tokens.ui.panel_bg_dark,
                panel_bg_secondary: tokens.ui.panel_bg_secondary_dark,
                panel_bg_floating: tokens.ui.panel_bg_dark,

                toolbar_bg: tokens.ui.panel_bg_dark,
                toolbar_separator: tokens.ui.border_subtle_dark,

                btn_bg: tokens.ui.btn_bg_dark,
                btn_bg_hover: tokens.ui.btn_bg_hover_dark,
                btn_bg_active: tokens.ui.btn_bg_active_dark,
                btn_bg_disabled: tokens.ui.panel_bg_dark,

                text: tokens.ui.text_dark,
                text_secondary: tokens.ui.text_secondary_dark,
                text_muted: tokens.ui.text_muted_dark,
                text_disabled: tokens.ui.text_muted_dark,
                text_link: tokens.ui.accent,

                icon: tokens.ui.icon_dark,
                icon_hover: tokens.ui.icon_hover_dark,
                icon_active: tokens.ui.icon_active,
                icon_disabled: tokens.ui.text_muted_dark,

                border: tokens.ui.border_dark,
                border_subtle: tokens.ui.border_subtle_dark,
                border_focus: tokens.brand.accent,

                selection_bg: tokens.brand.accent,
                selection_text: tokens.chart.selection_text,

                accent: tokens.brand.accent,
                accent_hover: tokens.brand.accent_hover,
                accent_active: tokens.buttons.primary_bg_active,
                accent_text: tokens.chart.selection_text,

                success: tokens.ui.success,
                warning: tokens.ui.warning,
                error: tokens.status.error,
                info: tokens.status.info,
            }
        } else {
            Self {
                panel_bg: tokens.ui.panel_bg_light,
                panel_bg_secondary: tokens.ui.panel_bg_secondary_light,
                panel_bg_floating: tokens.ui.panel_bg_light,

                toolbar_bg: tokens.ui.panel_bg_light,
                toolbar_separator: tokens.ui.border_subtle_light,

                btn_bg: tokens.ui.btn_bg_light,
                btn_bg_hover: tokens.ui.btn_bg_hover_light,
                btn_bg_active: tokens.ui.btn_bg_active_light,
                btn_bg_disabled: tokens.ui.panel_bg_secondary_light,

                text: tokens.ui.text_light,
                text_secondary: tokens.ui.text_secondary_light,
                text_muted: tokens.ui.text_muted_light,
                text_disabled: tokens.ui.text_muted_light,
                text_link: tokens.ui.accent,

                icon: tokens.ui.icon_light,
                icon_hover: tokens.ui.icon_hover_light,
                icon_active: tokens.ui.icon_active,
                icon_disabled: tokens.ui.text_muted_light,

                border: tokens.ui.border_light,
                border_subtle: tokens.ui.border_subtle_light,
                border_focus: tokens.brand.accent,

                selection_bg: tokens.brand.accent,
                selection_text: tokens.chart.selection_text,

                accent: tokens.brand.accent,
                accent_hover: tokens.brand.accent_hover,
                accent_active: tokens.buttons.primary_bg_active,
                accent_text: tokens.chart.selection_text,

                success: tokens.ui.success,
                warning: tokens.ui.warning,
                error: tokens.status.error,
                info: tokens.status.info,
            }
        }
    }
}

// ============================================================================
// SEMANTIC CHART TOKENS
// ============================================================================

/// Semantic tokens for chart area
#[derive(Clone, Copy, Debug)]
pub struct ChartSemanticTokens {
    // Backgrounds
    pub bg: Color32,
    pub bg_axis: Color32,
    pub bg_tooltip: Color32,
    pub bg_legend: Color32,
    pub bg_selection: Color32,

    // Grid
    pub grid_line: Color32,
    pub grid_line_major: Color32,

    // Axis
    pub axis_text: Color32,
    pub axis_text_secondary: Color32,

    // Crosshair
    pub crosshair_line: Color32,
    pub crosshair_label_bg: Color32,
    pub crosshair_label_text: Color32,

    // Candles/bars
    pub candle_up: Color32,
    pub candle_up_border: Color32,
    pub candle_up_wick: Color32,
    pub candle_down: Color32,
    pub candle_down_border: Color32,
    pub candle_down_wick: Color32,

    // Volume
    pub volume_up: Color32,
    pub volume_down: Color32,

    // Price line
    pub price_line: Color32,
    pub price_label_bg: Color32,
    pub price_label_text: Color32,

    // Watermark
    pub watermark: Color32,
}

impl ChartSemanticTokens {
    /// Build chart semantic tokens from DESIGN_TOKENS
    /// Note: Charts are almost always dark
    pub fn from_design_tokens(is_dark_chart: bool) -> Self {
        let tokens = &DESIGN_TOKENS.semantic;

        if is_dark_chart {
            Self {
                bg: tokens.chart.bg,
                bg_axis: tokens.chart.bg_axis,
                bg_tooltip: tokens.chart.crosshair_label_bg,
                bg_legend: tokens.chart.crosshair_label_bg,
                bg_selection: tokens.chart.bg_selection,

                grid_line: tokens.chart.grid_line,
                grid_line_major: tokens.chart.grid_line_major,

                axis_text: tokens.chart.axis_text,
                axis_text_secondary: tokens.chart.axis_text_secondary,

                crosshair_line: tokens.chart.crosshair_line,
                crosshair_label_bg: tokens.chart.crosshair_label_bg,
                crosshair_label_text: tokens.chart.crosshair_label_text,

                candle_up: tokens.chart.bullish,
                candle_up_border: tokens.chart.bullish,
                candle_up_wick: tokens.chart.bullish,
                candle_down: tokens.chart.bearish,
                candle_down_border: tokens.chart.bearish,
                candle_down_wick: tokens.chart.bearish,

                volume_up: {
                    let [r, g, b, _] = tokens.chart.bullish.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.volume_up_alpha)
                },
                volume_down: {
                    let [r, g, b, _] = tokens.chart.bearish.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.volume_down_alpha)
                },

                price_line: tokens.chart.axis_text,
                price_label_bg: tokens.chart.crosshair_label_bg,
                price_label_text: tokens.chart.crosshair_label_text,

                watermark: {
                    let [r, g, b, _] = tokens.chart.axis_text_secondary.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.watermark_alpha)
                },
            }
        } else {
            // Light chart (rare but supported)
            Self {
                bg: tokens.chart.bg_light,
                bg_axis: tokens.chart.bg_axis_light,
                bg_tooltip: tokens.ui.panel_bg_light,
                bg_legend: tokens.ui.panel_bg_light,
                bg_selection: tokens.chart.bg_selection_light,

                grid_line: tokens.chart.grid_line_light,
                grid_line_major: tokens.chart.grid_line_major_light,

                axis_text: tokens.ui.text_light,
                axis_text_secondary: tokens.ui.text_secondary_light,

                crosshair_line: tokens.chart.crosshair_line_light,
                crosshair_label_bg: tokens.ui.panel_bg_secondary_light,
                crosshair_label_text: tokens.ui.text_light,

                candle_up: tokens.chart.bullish,
                candle_up_border: tokens.chart.bullish,
                candle_up_wick: tokens.chart.bullish,
                candle_down: tokens.chart.bearish,
                candle_down_border: tokens.chart.bearish,
                candle_down_wick: tokens.chart.bearish,

                volume_up: {
                    let [r, g, b, _] = tokens.chart.bullish.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.volume_up_alpha)
                },
                volume_down: {
                    let [r, g, b, _] = tokens.chart.bearish.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.volume_down_alpha)
                },

                price_line: tokens.ui.text_light,
                price_label_bg: tokens.ui.panel_bg_secondary_light,
                price_label_text: tokens.ui.text_light,

                watermark: {
                    let [r, g, b, _] = tokens.ui.text_muted_light.to_array();
                    Color32::from_rgba_unmultiplied(r, g, b, tokens.chart.watermark_alpha)
                },
            }
        }
    }

    /// Get bullish color with custom alpha
    #[inline]
    pub fn candle_up_alpha(&self, alpha: u8) -> Color32 {
        let [r, g, b, _] = self.candle_up.to_array();
        Color32::from_rgba_unmultiplied(r, g, b, alpha)
    }

    /// Get bearish color with custom alpha
    #[inline]
    pub fn candle_down_alpha(&self, alpha: u8) -> Color32 {
        let [r, g, b, _] = self.candle_down.to_array();
        Color32::from_rgba_unmultiplied(r, g, b, alpha)
    }
}

// ============================================================================
// SEMANTIC INDICATOR TOKENS
// ============================================================================

/// Semantic tokens for indicators
#[derive(Clone, Copy, Debug)]
pub struct IndicatorSemanticTokens {
    // Moving avgs
    pub ma_default: Color32,
    pub ema_default: Color32,
    pub sma_default: Color32,

    // Bollinger bands
    pub bb_upper: Color32,
    pub bb_middle: Color32,
    pub bb_lower: Color32,
    pub bb_fill: Color32,

    // Oscillators
    pub rsi_line: Color32,
    pub rsi_overbought: Color32,
    pub rsi_oversold: Color32,

    // MACD
    pub macd_line: Color32,
    pub macd_signal: Color32,
    pub macd_hist_positive: Color32,
    pub macd_hist_negative: Color32,

    // VWAP
    pub vwap: Color32,
}

impl IndicatorSemanticTokens {
    /// Build indicator tokens from DESIGN_TOKENS
    pub fn from_design_tokens() -> Self {
        let tokens = &DESIGN_TOKENS.semantic.indicators;

        Self {
            ma_default: tokens.ma,
            ema_default: tokens.ema,
            sma_default: tokens.sma,

            bb_upper: tokens.bb_upper,
            bb_middle: tokens.bb_middle,
            bb_lower: tokens.bb_lower,
            bb_fill: tokens.bb_fill,

            rsi_line: tokens.rsi,
            rsi_overbought: tokens.rsi_overbought,
            rsi_oversold: tokens.rsi_oversold,

            macd_line: tokens.macd_line,
            macd_signal: tokens.macd_signal,
            macd_hist_positive: tokens.macd_hist_pos,
            macd_hist_negative: tokens.macd_hist_neg,

            vwap: tokens.vwap,
        }
    }
}

// ============================================================================
// SEMANTIC DRAWING TOKENS
// ============================================================================

/// Semantic tokens for drawing tools
#[derive(Clone, Copy, Debug)]
pub struct DrawingSemanticTokens {
    // Lines
    pub line_default: Color32,
    pub line_hover: Color32,
    pub line_sel: Color32,

    // Handles
    pub handle: Color32,
    pub handle_hover: Color32,
    pub handle_active: Color32,

    // Fibonacci
    pub fib_0: Color32,
    pub fib_236: Color32,
    pub fib_382: Color32,
    pub fib_50: Color32,
    pub fib_618: Color32,
    pub fib_100: Color32,

    // Text/labels
    pub label_bg: Color32,
    pub label_text: Color32,
}

impl DrawingSemanticTokens {
    /// Build drawing tokens from DESIGN_TOKENS
    pub fn from_design_tokens() -> Self {
        let tokens = &DESIGN_TOKENS.semantic.drawings;

        Self {
            line_default: tokens.default_line,
            line_hover: tokens.line_hover,
            line_sel: tokens.line_selected,

            handle: tokens.handle,
            handle_hover: tokens.handle_hover,
            handle_active: tokens.handle_active,

            fib_0: tokens.fib_0,
            fib_236: tokens.fib_236,
            fib_382: tokens.fib_382,
            fib_50: tokens.fib_50,
            fib_618: tokens.fib_618,
            fib_100: tokens.fib_100,

            label_bg: tokens.label_bg,
            label_text: tokens.label_text,
        }
    }
}

// ============================================================================
// COMPLETE SEMANTIC TOKENS
// ============================================================================

/// Complete semantic token set - this is what components consume
#[derive(Clone, Copy, Debug)]
pub struct SemanticTokens {
    pub ui: UiSemanticTokens,
    pub chart: ChartSemanticTokens,
    pub indicators: IndicatorSemanticTokens,
    pub drawing: DrawingSemanticTokens,
}

impl SemanticTokens {
    /// Build complete semantic tokens from DESIGN_TOKENS based on theme flags
    pub fn from_design_tokens(is_dark_ui: bool, is_dark_chart: bool) -> Self {
        Self {
            ui: UiSemanticTokens::from_design_tokens(is_dark_ui),
            chart: ChartSemanticTokens::from_design_tokens(is_dark_chart),
            indicators: IndicatorSemanticTokens::from_design_tokens(),
            drawing: DrawingSemanticTokens::from_design_tokens(),
        }
    }
}