ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Cyber theme for IronMonitor GUI
//!
//! A dark cyberpunk-inspired theme with neon accents
//! Now with Glances-style threshold colors

use super::app::ColorTheme;
use egui::{
    Color32, FontData, FontDefinitions, FontFamily, FontId, Stroke, Style, TextStyle, Visuals,
};
use std::sync::Arc;

fn theme_id() -> egui::Id {
    egui::Id::new("ironmonitor_color_theme")
}

/// The family data labels are painted in: a bold face, because egui's only
/// bundled proportional font is Ubuntu Light.
///
/// That was what made values hard to read. A light weight is fine for a
/// paragraph at 13 px and not for a chart scale at 10 px drawn in the dimmest
/// text colour, and egui has no bold to fall back on — `RichText::strong`
/// changes the colour, not the weight.
const DATA_FAMILY: &str = "data";

/// The smallest size a data label is painted at. The chart scales were 10 px,
/// the legend ranges 9.
pub const DATA_LABEL_MIN_SIZE: f32 = 11.0;

fn data_font_id() -> egui::Id {
    egui::Id::new("ironmonitor_data_font")
}

/// Install the fonts every theme shares: the emoji fallback, and the bold
/// family `data_font` paints with.
fn set_fonts(ctx: &egui::Context) {
    let mut fonts = FontDefinitions::default();

    fonts.font_data.insert(
        "emoji".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../../assets/fonts/NotoEmoji-VariableFont.ttf"
        ))),
    );
    // SIL Open Font License; see assets/fonts/OFL.txt.
    fonts.font_data.insert(
        "noto-sans-bold".to_owned(),
        Arc::new(FontData::from_static(include_bytes!(
            "../../assets/fonts/NotoSans-Bold.ttf"
        ))),
    );

    fonts
        .families
        .entry(FontFamily::Proportional)
        .or_default()
        .push("emoji".to_owned());

    fonts
        .families
        .entry(FontFamily::Monospace)
        .or_default()
        .push("emoji".to_owned());

    // Bold first, then the proportional chain, so a glyph Noto Sans lacks
    // (an arrow, a symbol) still renders rather than painting as a box.
    let mut data = vec!["noto-sans-bold".to_owned()];
    data.extend(fonts.families[&FontFamily::Proportional].iter().cloned());
    fonts
        .families
        .insert(FontFamily::Name(DATA_FAMILY.into()), data);

    ctx.set_fonts(fonts);
    ctx.data_mut(|d| d.insert_temp(data_font_id(), true));
}

/// The contrast `legible` holds data labels to: WCAG AA for body text, which
/// leaves margin over the 3:1 a bold face is entitled to.
pub const LEGIBLE_CONTRAST: f32 = 4.5;

/// WCAG 2 contrast ratio between two opaque colours.
fn contrast_ratio(a: Color32, b: Color32) -> f32 {
    fn luminance(c: Color32) -> f32 {
        let channel = |v: u8| {
            let v = f32::from(v) / 255.0;
            if v <= 0.03928 {
                v / 12.92
            } else {
                ((v + 0.055) / 1.055).powf(2.4)
            }
        };
        0.2126 * channel(c.r()) + 0.7152 * channel(c.g()) + 0.0722 * channel(c.b())
    }
    let (x, y) = (luminance(a), luminance(b));
    (x.max(y) + 0.05) / (x.min(y) + 0.05)
}

/// A text colour that can be read on this theme's panels and cards.
///
/// Threshold and accent colours are chosen for a dark surface, and every
/// theme but Cyber passed them through unchanged — so on the Light theme a
/// healthy reading was neon green on white, 1.5:1, and the value a user most
/// wants to see was the one they could not. A colour that already reads is
/// returned as is; one that does not is blended toward the theme's strong
/// text colour just far enough to, which keeps its hue as the threshold cue.
///
/// Translucent colours are returned untouched: they are shadows painted
/// beneath a label, not the label.
pub fn legible(ctx: &egui::Context, color: Color32) -> Color32 {
    if color.a() < 255 {
        return color;
    }
    let style = ctx.style();
    let visuals = &style.visuals;
    let backgrounds = [visuals.panel_fill, visuals.faint_bg_color];
    let toward = visuals.strong_text_color();
    let reads = |c: Color32| {
        backgrounds
            .iter()
            .all(|b| contrast_ratio(c, *b) >= LEGIBLE_CONTRAST)
    };
    if reads(color) {
        return color;
    }
    (1..=10)
        .map(|step| color.lerp_to_gamma(toward, step as f32 / 10.0))
        .find(|c| reads(*c))
        .unwrap_or(toward)
}

/// `color`, made `legible`: the colour to paint a reading's text in.
pub fn text_color(ctx: &egui::Context, base: Color32) -> Color32 {
    legible(ctx, color(ctx, base))
}

/// The font for a data label: a value, a unit, a scale tick, a legend entry.
///
/// Bold, and never smaller than `DATA_LABEL_MIN_SIZE`. Falls back to the
/// regular proportional font on a context no theme has been applied to,
/// because egui panics on a family that was never registered and a label is
/// not worth a crash.
pub fn data_font(ctx: &egui::Context, size: f32) -> FontId {
    let size = size.max(DATA_LABEL_MIN_SIZE);
    if ctx
        .data(|d| d.get_temp::<bool>(data_font_id()))
        .unwrap_or(false)
    {
        FontId::new(size, FontFamily::Name(DATA_FAMILY.into()))
    } else {
        FontId::proportional(size)
    }
}

/// Apply the selected palette without changing typography or widget geometry.
pub fn apply_theme(ctx: &egui::Context, selected: ColorTheme) {
    if selected == ColorTheme::Light {
        apply_light_theme(ctx);
    } else {
        apply_cyber_theme(ctx);
    }
    ctx.data_mut(|data| data.insert_temp(theme_id(), selected));
    ctx.style_mut(|style| {
        let visuals = &mut style.visuals;
        if selected == ColorTheme::Monochrome {
            visuals.window_fill = Color32::from_gray(8);
            visuals.panel_fill = Color32::from_gray(8);
            visuals.faint_bg_color = Color32::from_gray(15);
            visuals.extreme_bg_color = Color32::from_gray(4);
            for widget in [
                &mut visuals.widgets.noninteractive,
                &mut visuals.widgets.inactive,
                &mut visuals.widgets.hovered,
                &mut visuals.widgets.active,
                &mut visuals.widgets.open,
            ] {
                widget.bg_fill = gray(widget.bg_fill);
                widget.weak_bg_fill = gray(widget.weak_bg_fill);
                widget.bg_stroke.color = gray(widget.bg_stroke.color);
                widget.fg_stroke.color = gray(widget.fg_stroke.color);
            }
            visuals.selection.bg_fill = Color32::from_gray(52);
            visuals.selection.stroke.color = Color32::from_gray(225);
            visuals.hyperlink_color = Color32::from_gray(215);
            visuals.warn_fg_color = Color32::from_gray(210);
            visuals.error_fg_color = Color32::from_gray(240);
            visuals.text_cursor.stroke.color = Color32::from_gray(225);
        } else {
            // Adapt the editor palettes to the existing monitor surfaces.
            let palette = match selected {
                ColorTheme::Dracula => Some((0x282a36, 0x21222c, 0x343746, 0xf8f8f2)),
                ColorTheme::OneDarkPro => Some((0x282c34, 0x21252b, 0x303640, 0xabb2bf)),
                ColorTheme::TokyoNight => Some((0x1a1b26, 0x16161e, 0x24283b, 0xc0caf5)),
                ColorTheme::Nord => Some((0x2e3440, 0x242933, 0x3b4252, 0xeceff4)),
                ColorTheme::Monokai => Some((0x272822, 0x1e1f1c, 0x34352f, 0xf8f8f2)),
                ColorTheme::GitHubDark => Some((0x0d1117, 0x010409, 0x161b22, 0xe6edf3)),
                _ => None,
            };
            if let Some((background, dark, surface, foreground)) = palette {
                let rgb =
                    |hex: u32| Color32::from_rgb((hex >> 16) as u8, (hex >> 8) as u8, hex as u8);
                visuals.window_fill = rgb(background);
                visuals.panel_fill = rgb(background);
                visuals.extreme_bg_color = rgb(dark);
                visuals.faint_bg_color = rgb(surface);
                for widget in [
                    &mut visuals.widgets.noninteractive,
                    &mut visuals.widgets.inactive,
                    &mut visuals.widgets.hovered,
                    &mut visuals.widgets.active,
                    &mut visuals.widgets.open,
                ] {
                    widget.bg_fill = rgb(surface);
                    widget.weak_bg_fill = rgb(surface);
                    widget.fg_stroke.color = rgb(foreground);
                }
            }
            let accent = selected.accent_color();
            visuals.widgets.hovered.fg_stroke.color = accent;
            visuals.widgets.hovered.bg_stroke.color = accent.linear_multiply(0.7);
            visuals.widgets.active.bg_stroke.color = accent;
            visuals.widgets.open.fg_stroke.color = accent;
            visuals.widgets.open.bg_stroke.color = accent.linear_multiply(0.7);
            visuals.selection.stroke.color = accent;
            visuals.selection.bg_fill = accent.linear_multiply(0.2);
            visuals.hyperlink_color = accent;
        }
    });
}

fn gray(color: Color32) -> Color32 {
    // Work in premultiplied channels so translucent chart fills retain alpha.
    let value = ((u32::from(color.r()) * 54
        + u32::from(color.g()) * 183
        + u32::from(color.b()) * 19)
        / 256) as u8;
    Color32::from_rgba_premultiplied(value, value, value, color.a())
}

/// Keep a fixed dark surface while respecting the monochrome palette.
pub fn fixed_color(ctx: &egui::Context, base: Color32) -> Color32 {
    if ctx.data(|data| data.get_temp::<ColorTheme>(theme_id())) == Some(ColorTheme::Monochrome) {
        gray(base)
    } else {
        base
    }
}

/// Resolve shared Overview colors for text, charts, and custom-painted widgets.
pub fn color(ctx: &egui::Context, base: Color32) -> Color32 {
    let selected = ctx
        .data(|data| data.get_temp::<ColorTheme>(theme_id()))
        .unwrap_or_default();
    if selected == ColorTheme::Monochrome {
        return gray(base);
    }
    if selected == ColorTheme::Cyber {
        return base;
    }
    let visuals = &ctx.style().visuals;
    match base {
        CyberColors::BACKGROUND => visuals.panel_fill,
        CyberColors::BACKGROUND_DARK => visuals.extreme_bg_color,
        CyberColors::SURFACE | CyberColors::BACKGROUND_LIGHT => visuals.faint_bg_color,
        CyberColors::SURFACE_HOVER => visuals.widgets.hovered.bg_fill,
        CyberColors::TEXT_PRIMARY => visuals.strong_text_color(),
        CyberColors::TEXT_SECONDARY | CyberColors::TEXT_MUTED => visuals.text_color(),
        CyberColors::BORDER => visuals.widgets.noninteractive.bg_stroke.color,
        CyberColors::CYAN | CyberColors::CYAN_DIM | CyberColors::BORDER_GLOW => {
            selected.accent_color()
        }
        CyberColors::MAGENTA | CyberColors::MAGENTA_DIM | CyberColors::NEON_PURPLE => {
            selected.secondary_color()
        }
        _ => base,
    }
}

/// Cyber color palette
pub struct CyberColors;

impl CyberColors {
    // Primary colors
    pub const BACKGROUND: Color32 = Color32::from_rgb(6, 8, 12);
    pub const BACKGROUND_DARK: Color32 = Color32::from_rgb(3, 5, 8);
    #[allow(dead_code)]
    pub const BACKGROUND_LIGHT: Color32 = Color32::from_rgb(10, 14, 20);
    pub const SURFACE: Color32 = Color32::from_rgb(12, 16, 23);
    pub const SURFACE_HOVER: Color32 = Color32::from_rgb(20, 27, 37);

    // Accent colors (neon)
    pub const CYAN: Color32 = Color32::from_rgb(64, 232, 216);
    pub const CYAN_DIM: Color32 = Color32::from_rgb(40, 159, 151);
    pub const MAGENTA: Color32 = Color32::from_rgb(184, 140, 255);
    #[allow(dead_code)]
    pub const MAGENTA_DIM: Color32 = Color32::from_rgb(122, 88, 181);
    pub const NEON_GREEN: Color32 = Color32::from_rgb(87, 235, 148);
    pub const NEON_ORANGE: Color32 = Color32::from_rgb(255, 164, 80);
    pub const NEON_YELLOW: Color32 = Color32::from_rgb(245, 218, 102);
    pub const NEON_RED: Color32 = Color32::from_rgb(255, 100, 119);
    pub const NEON_BLUE: Color32 = Color32::from_rgb(78, 169, 255);
    pub const NEON_PURPLE: Color32 = Color32::from_rgb(173, 128, 255);

    // Text colors
    pub const TEXT_PRIMARY: Color32 = Color32::from_rgb(232, 237, 245);
    pub const TEXT_SECONDARY: Color32 = Color32::from_rgb(163, 175, 194);
    pub const TEXT_MUTED: Color32 = Color32::from_rgb(122, 138, 160);

    // Status colors
    #[allow(dead_code)]
    pub const SUCCESS: Color32 = Self::NEON_GREEN;
    pub const WARNING: Color32 = Self::NEON_YELLOW;
    pub const ERROR: Color32 = Self::NEON_RED;
    #[allow(dead_code)]
    pub const INFO: Color32 = Self::NEON_BLUE;

    // Glances-style threshold colors
    pub const THRESHOLD_OK: Color32 = Self::NEON_GREEN;
    pub const THRESHOLD_CAREFUL: Color32 = Self::CYAN;
    pub const THRESHOLD_WARNING: Color32 = Self::NEON_YELLOW;
    pub const THRESHOLD_CRITICAL: Color32 = Self::NEON_RED;

    // Grid and borders
    #[allow(dead_code)]
    pub const GRID: Color32 = Color32::from_rgb(52, 74, 91);
    pub const BORDER: Color32 = Color32::from_rgb(52, 74, 91);
    #[allow(dead_code)]
    pub const BORDER_GLOW: Color32 = Color32::from_rgb(0, 200, 200);
}

pub fn threshold_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::THRESHOLD_CRITICAL,
        p if p >= 70.0 => CyberColors::THRESHOLD_WARNING,
        p if p >= 50.0 => CyberColors::THRESHOLD_CAREFUL,
        _ => CyberColors::THRESHOLD_OK,
    }
}

// ── Device-class color palettes (matches TUI conventions) ────────────────────

/// Device-class title colors (bright cyberpunk neon)
pub struct DeviceTitleColors;

impl DeviceTitleColors {
    pub const CPU: Color32 = CyberColors::CYAN; // Neon Cyan
    pub const ACCEL: Color32 = CyberColors::NEON_GREEN; // Neon Green
    pub const MEMORY: Color32 = CyberColors::MAGENTA; // Neon Magenta
    pub const DISK: Color32 = CyberColors::NEON_ORANGE; // Neon Orange
    pub const NETWORK: Color32 = CyberColors::NEON_BLUE; // Neon Blue
}

/// CPU device-class color (Neon Cyan family)
pub fn cpu_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::NEON_RED,
        p if p >= 70.0 => CyberColors::NEON_YELLOW,
        p if p >= 50.0 => CyberColors::CYAN, // shared Overview accent
        _ => Color32::from_rgb(64, 232, 216), // neon cyan
    }
}

/// Accelerator (GPU/NPU) device-class color (Neon Green family)
pub fn accel_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::NEON_RED,
        p if p >= 70.0 => CyberColors::NEON_YELLOW,
        p if p >= 50.0 => CyberColors::NEON_GREEN, // shared Overview accent
        _ => Color32::from_rgb(87, 235, 148),      // neon green
    }
}

/// Memory device-class color (Neon Magenta family)
pub fn memory_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::NEON_RED,
        p if p >= 70.0 => CyberColors::NEON_YELLOW,
        p if p >= 50.0 => CyberColors::MAGENTA, // shared Overview accent
        _ => Color32::from_rgb(184, 140, 255),  // neon magenta
    }
}

/// Disk device-class color (Neon Orange family)
#[allow(dead_code)]
pub fn disk_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::NEON_RED,
        p if p >= 70.0 => CyberColors::NEON_YELLOW,
        p if p >= 50.0 => CyberColors::NEON_ORANGE, // shared Overview accent
        _ => Color32::from_rgb(255, 164, 80),       // neon orange
    }
}

/// Network device-class color (Neon Blue family)
#[allow(dead_code)]
pub fn network_color(percent: f32) -> Color32 {
    match percent {
        p if p >= 90.0 => CyberColors::NEON_RED,
        p if p >= 70.0 => CyberColors::NEON_YELLOW,
        p if p >= 50.0 => CyberColors::NEON_BLUE, // shared Overview accent
        _ => Color32::from_rgb(78, 169, 255),     // neon blue
    }
}

pub fn trend_indicator(current: f32, previous: f32) -> (&'static str, Color32) {
    let delta = current - previous;
    if delta.abs() < 0.5 {
        ("-", CyberColors::TEXT_MUTED)
    } else if delta > 0.0 {
        ("^", CyberColors::THRESHOLD_CRITICAL)
    } else {
        ("v", CyberColors::THRESHOLD_OK)
    }
}

pub fn apply_cyber_theme(ctx: &egui::Context) {
    ctx.data_mut(|data| data.insert_temp(theme_id(), ColorTheme::Cyber));
    set_fonts(ctx);

    let mut style = Style::default();
    let mut visuals = Visuals::dark();

    visuals.window_fill = CyberColors::BACKGROUND;
    visuals.panel_fill = CyberColors::BACKGROUND;
    visuals.faint_bg_color = CyberColors::SURFACE;
    visuals.extreme_bg_color = CyberColors::BACKGROUND_DARK;

    visuals.widgets.noninteractive.bg_fill = CyberColors::SURFACE;
    visuals.widgets.noninteractive.fg_stroke = Stroke::new(1.0_f32, CyberColors::TEXT_SECONDARY);
    visuals.widgets.noninteractive.bg_stroke = Stroke::new(1.0_f32, CyberColors::BORDER);

    visuals.widgets.inactive.bg_fill = CyberColors::SURFACE;
    visuals.widgets.inactive.fg_stroke = Stroke::new(1.0_f32, CyberColors::TEXT_PRIMARY);
    visuals.widgets.inactive.bg_stroke = Stroke::new(1.0_f32, CyberColors::BORDER);

    visuals.widgets.hovered.bg_fill = CyberColors::SURFACE_HOVER;
    visuals.widgets.hovered.fg_stroke = Stroke::new(1.0_f32, CyberColors::CYAN);
    visuals.widgets.hovered.bg_stroke = Stroke::new(1.0_f32, CyberColors::CYAN_DIM);

    // `widgets.active` is not only the pressed-widget style: egui derives the global
    // strong-text colour from it (`Visuals::strong_text_color` -> `widgets.active
    // .text_color()`). This used to be a bright CYAN_DIM fill with BACKGROUND-coloured
    // text, which reads well on a pressed button and is invisible everywhere else —
    // every `RichText::strong()` in the app was drawn in rgb(13,17,23) on the
    // rgb(13,17,23) panel. That is what made the Profiles tab look empty: it rendered
    // all 19 groups, each with an unreadable heading.
    //
    // A dark fill with bright text satisfies both roles: legible when pressed, legible
    // as strong body text. The cyan identity moves to the border.
    visuals.widgets.active.bg_fill = CyberColors::SURFACE_HOVER;
    visuals.widgets.active.fg_stroke = Stroke::new(1.0_f32, CyberColors::TEXT_PRIMARY);
    visuals.widgets.active.bg_stroke = Stroke::new(1.0_f32, CyberColors::CYAN);

    visuals.widgets.open.bg_fill = CyberColors::SURFACE_HOVER;
    visuals.widgets.open.fg_stroke = Stroke::new(1.0_f32, CyberColors::CYAN);
    visuals.widgets.open.bg_stroke = Stroke::new(1.0_f32, CyberColors::CYAN_DIM);

    visuals.selection.bg_fill = CyberColors::CYAN_DIM.linear_multiply(0.3);
    visuals.selection.stroke = Stroke::new(1.0_f32, CyberColors::CYAN);

    visuals.hyperlink_color = CyberColors::CYAN;

    visuals.window_shadow.color = Color32::from_black_alpha(120);
    visuals.popup_shadow.color = Color32::from_black_alpha(100);

    visuals.window_corner_radius = egui::CornerRadius::same(8);
    visuals.menu_corner_radius = egui::CornerRadius::same(6);

    style.visuals = visuals;

    style.text_styles = [
        (
            TextStyle::Small,
            FontId::new(11.0, FontFamily::Proportional),
        ),
        (TextStyle::Body, FontId::new(13.0, FontFamily::Proportional)),
        (
            TextStyle::Button,
            FontId::new(13.0, FontFamily::Proportional),
        ),
        (
            TextStyle::Heading,
            FontId::new(18.0, FontFamily::Proportional),
        ),
        (
            TextStyle::Monospace,
            FontId::new(12.0, FontFamily::Monospace),
        ),
    ]
    .into();

    style.spacing.item_spacing = egui::vec2(8.0, 6.0);
    style.spacing.window_margin = egui::Margin::same(12);
    style.spacing.button_padding = egui::vec2(10.0, 4.0);

    ctx.set_style(style);
}

pub fn utilization_color(percent: f32) -> Color32 {
    if percent < 50.0 {
        CyberColors::NEON_GREEN
    } else if percent < 75.0 {
        CyberColors::NEON_YELLOW
    } else if percent < 90.0 {
        CyberColors::NEON_ORANGE
    } else {
        CyberColors::NEON_RED
    }
}

pub fn temperature_color(temp: u32) -> Color32 {
    if temp < 50 {
        CyberColors::NEON_GREEN
    } else if temp < 70 {
        CyberColors::NEON_YELLOW
    } else if temp < 85 {
        CyberColors::NEON_ORANGE
    } else {
        CyberColors::NEON_RED
    }
}

#[allow(dead_code)]
pub fn neon_color_by_index(index: usize) -> Color32 {
    const COLORS: &[Color32] = &[
        CyberColors::CYAN,
        CyberColors::MAGENTA,
        CyberColors::NEON_GREEN,
        CyberColors::NEON_ORANGE,
        CyberColors::NEON_PURPLE,
        CyberColors::NEON_BLUE,
        CyberColors::NEON_YELLOW,
        CyberColors::NEON_RED,
    ];
    COLORS[index % COLORS.len()]
}

/// Light theme colors
pub struct LightColors;

impl LightColors {
    pub const BACKGROUND: Color32 = Color32::from_rgb(250, 250, 252);
    pub const SURFACE: Color32 = Color32::from_rgb(255, 255, 255);
    pub const SURFACE_HOVER: Color32 = Color32::from_rgb(240, 242, 245);
    pub const TEXT_PRIMARY: Color32 = Color32::from_rgb(30, 41, 59);
    pub const TEXT_SECONDARY: Color32 = Color32::from_rgb(100, 116, 139);
    pub const ACCENT: Color32 = Color32::from_rgb(59, 130, 246);
    pub const ACCENT_DIM: Color32 = Color32::from_rgb(96, 165, 250);
    pub const BORDER: Color32 = Color32::from_rgb(226, 232, 240);
}

/// Apply light theme to the egui context
pub fn apply_light_theme(ctx: &egui::Context) {
    ctx.data_mut(|data| data.insert_temp(theme_id(), ColorTheme::Light));
    set_fonts(ctx);

    let mut style = Style::default();
    let mut visuals = Visuals::light();
    visuals.window_fill = LightColors::BACKGROUND;
    visuals.panel_fill = LightColors::BACKGROUND;
    visuals.faint_bg_color = LightColors::SURFACE;
    visuals.extreme_bg_color = Color32::WHITE;
    visuals.widgets.noninteractive.bg_fill = LightColors::SURFACE;
    visuals.widgets.noninteractive.fg_stroke = Stroke::new(1.0_f32, LightColors::TEXT_SECONDARY);
    visuals.widgets.noninteractive.bg_stroke = Stroke::new(1.0_f32, LightColors::BORDER);
    visuals.widgets.inactive.bg_fill = LightColors::SURFACE;
    visuals.widgets.inactive.fg_stroke = Stroke::new(1.0_f32, LightColors::TEXT_PRIMARY);
    visuals.widgets.inactive.bg_stroke = Stroke::new(1.0_f32, LightColors::BORDER);
    visuals.widgets.hovered.bg_fill = LightColors::SURFACE_HOVER;
    visuals.widgets.hovered.fg_stroke = Stroke::new(1.0_f32, LightColors::ACCENT);
    visuals.widgets.hovered.bg_stroke = Stroke::new(1.0_f32, LightColors::ACCENT_DIM);
    // Same constraint as the dark theme: this stroke doubles as the global strong-text
    // colour, so white here made every `strong()` label invisible on the near-white
    // panel. Dark text on a light fill satisfies both roles.
    visuals.widgets.active.bg_fill = LightColors::SURFACE_HOVER;
    visuals.widgets.active.fg_stroke = Stroke::new(1.0_f32, LightColors::TEXT_PRIMARY);
    visuals.widgets.active.bg_stroke = Stroke::new(1.0_f32, LightColors::ACCENT);
    visuals.widgets.open.bg_fill = LightColors::SURFACE_HOVER;
    visuals.widgets.open.fg_stroke = Stroke::new(1.0_f32, LightColors::ACCENT);
    visuals.widgets.open.bg_stroke = Stroke::new(1.0_f32, LightColors::ACCENT_DIM);
    visuals.selection.bg_fill = LightColors::ACCENT.linear_multiply(0.2);
    visuals.selection.stroke = Stroke::new(1.0_f32, LightColors::ACCENT);
    visuals.hyperlink_color = LightColors::ACCENT;
    visuals.window_shadow.color = Color32::from_black_alpha(30);
    visuals.popup_shadow.color = Color32::from_black_alpha(20);
    visuals.window_corner_radius = egui::CornerRadius::same(8);
    visuals.menu_corner_radius = egui::CornerRadius::same(6);
    style.visuals = visuals;
    style.text_styles = [
        (
            TextStyle::Small,
            FontId::new(11.0, FontFamily::Proportional),
        ),
        (TextStyle::Body, FontId::new(13.0, FontFamily::Proportional)),
        (
            TextStyle::Button,
            FontId::new(13.0, FontFamily::Proportional),
        ),
        (
            TextStyle::Heading,
            FontId::new(18.0, FontFamily::Proportional),
        ),
        (
            TextStyle::Monospace,
            FontId::new(12.0, FontFamily::Monospace),
        ),
    ]
    .into();
    style.spacing.item_spacing = egui::vec2(8.0, 6.0);
    style.spacing.window_margin = egui::Margin::same(12);
    style.spacing.button_padding = egui::vec2(10.0, 4.0);
    ctx.set_style(style);
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Perceptual distance between two colours, as a rough sum of channel deltas.
    /// Good enough to catch "text drawn in the background colour"; not a WCAG metric.
    fn channel_distance(a: Color32, b: Color32) -> i32 {
        (a.r() as i32 - b.r() as i32).abs()
            + (a.g() as i32 - b.g() as i32).abs()
            + (a.b() as i32 - b.b() as i32).abs()
    }

    /// Every text role must be legible against the surface it is drawn on.
    ///
    /// `RichText::strong()` does not carry its own colour — egui resolves it through
    /// `Visuals::strong_text_color()`, which returns `widgets.active.text_color()`.
    /// That same stroke styles a pressed widget, so it is tempting to set it to the
    /// background colour for dark-on-accent buttons. Doing so silently paints every
    /// strong label in the app the same colour as the panel: the Profiles tab rendered
    /// all 19 of its groups with headings that could not be seen, which read as the
    /// tab being broken rather than as a contrast bug.
    #[test]
    fn strong_text_is_legible_on_the_panel_it_is_drawn_on() {
        for (name, apply) in [
            ("cyber", apply_cyber_theme as fn(&egui::Context)),
            ("light", apply_light_theme as fn(&egui::Context)),
            ("dark monochrome", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::Monochrome)
            }),
            ("dracula", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::Dracula)
            }),
            ("one dark pro", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::OneDarkPro)
            }),
            ("tokyo night", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::TokyoNight)
            }),
            ("nord", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::Nord)
            }),
            ("monokai", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::Monokai)
            }),
            ("github dark", |ctx: &egui::Context| {
                apply_theme(ctx, ColorTheme::GitHubDark)
            }),
        ] {
            let ctx = egui::Context::default();
            apply(&ctx);
            let visuals = ctx.style().visuals.clone();

            let panel = visuals.panel_fill;
            let strong = visuals.strong_text_color();
            let body = visuals.text_color();

            assert!(
                channel_distance(strong, panel) > 60,
                "{name}: strong text {strong:?} is indistinguishable from the panel \
                 fill {panel:?} — every RichText::strong() would be invisible"
            );
            assert!(
                channel_distance(body, panel) > 60,
                "{name}: body text {body:?} is indistinguishable from the panel fill \
                 {panel:?}"
            );

            // A pressed widget draws its own label on `active.bg_fill`, so that pair
            // has to hold up too — this is the constraint that motivated the original
            // background-coloured stroke.
            let active_fill = visuals.widgets.active.bg_fill;
            let active_text = visuals.widgets.active.text_color();
            assert!(
                channel_distance(active_text, active_fill) > 60,
                "{name}: pressed-widget text {active_text:?} is indistinguishable from \
                 its own fill {active_fill:?}"
            );
        }
    }
}