Skip to content

Commit ea9831f

Browse files
committed
scxtop: Add gradient colors
Add helpers to return gradient colors for making graphicals data easier to interpret. Signed-off-by: Daniel Hodges <[email protected]>
1 parent dbaac94 commit ea9831f

File tree

1 file changed

+358
-0
lines changed

1 file changed

+358
-0
lines changed

tools/scxtop/src/theme.rs

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,364 @@ impl AppTheme {
281281
}
282282
}
283283

284+
/// Returns the low-level color for a 3-level gradient.
285+
///
286+
/// # Arguments
287+
/// * `reverse` - If true, high values get the "good" color, if false, low values get the "good" color
288+
pub fn gradient_3_low(&self, reverse: bool) -> Color {
289+
if reverse {
290+
// High values are good, so low values get the "bad" color
291+
self.text_disabled_color()
292+
} else {
293+
// Low values are good, so low values get the "good" color
294+
self.text_enabled_color()
295+
}
296+
}
297+
298+
/// Returns the mid-level color for a 3-level gradient.
299+
/// Mid-level always uses the important/warning color regardless of reverse direction.
300+
pub fn gradient_3_mid(&self) -> Color {
301+
// Mid-level always uses the important/warning color regardless of reverse
302+
self.text_important_color()
303+
}
304+
305+
/// Returns the high-level color for a 3-level gradient.
306+
///
307+
/// # Arguments
308+
/// * `reverse` - If true, high values get the "good" color, if false, low values get the "good" color
309+
pub fn gradient_3_high(&self, reverse: bool) -> Color {
310+
if reverse {
311+
// High values are good, so high values get the "good" color
312+
self.text_enabled_color()
313+
} else {
314+
// Low values are good, so high values get the "bad" color
315+
self.text_disabled_color()
316+
}
317+
}
318+
319+
/// Returns a color for a 3-level gradient (LOW, MID, HIGH) based on value and thresholds.
320+
///
321+
/// # Arguments
322+
/// * `value` - The current value to evaluate
323+
/// * `low_threshold` - Values <= this are considered LOW
324+
/// * `high_threshold` - Values >= this are considered HIGH
325+
/// * `reverse` - If true, high values get the "good" color (green), if false, low values get the "good" color
326+
pub fn gradient_3(
327+
&self,
328+
value: f64,
329+
low_threshold: f64,
330+
high_threshold: f64,
331+
reverse: bool,
332+
) -> Color {
333+
if value <= low_threshold {
334+
self.gradient_3_low(reverse)
335+
} else if value >= high_threshold {
336+
self.gradient_3_high(reverse)
337+
} else {
338+
self.gradient_3_mid()
339+
}
340+
}
341+
342+
/// Returns a color for a 5-level gradient (VERY_LOW, LOW, MID, HIGH, VERY_HIGH) based on value and thresholds.
343+
///
344+
/// # Arguments
345+
/// * `value` - The current value to evaluate
346+
/// * `very_low_threshold` - Values <= this are considered VERY_LOW
347+
/// * `low_threshold` - Values <= this (but > very_low) are considered LOW
348+
/// * `high_threshold` - Values >= this (but < very_high) are considered HIGH
349+
/// * `very_high_threshold` - Values >= this are considered VERY_HIGH
350+
/// * `reverse` - If true, high values get the "good" color, if false, low values get the "good" color
351+
pub fn gradient_5(
352+
&self,
353+
value: f64,
354+
very_low_threshold: f64,
355+
low_threshold: f64,
356+
high_threshold: f64,
357+
very_high_threshold: f64,
358+
reverse: bool,
359+
) -> Color {
360+
let (very_low_color, low_color, mid_color, high_color, very_high_color) = match self {
361+
AppTheme::Default => {
362+
if reverse {
363+
(
364+
Color::Red,
365+
Color::Rgb(255, 165, 0),
366+
Color::Yellow,
367+
Color::LightGreen,
368+
Color::Green,
369+
)
370+
} else {
371+
(
372+
Color::Green,
373+
Color::LightGreen,
374+
Color::Yellow,
375+
Color::Rgb(255, 165, 0),
376+
Color::Red,
377+
)
378+
}
379+
}
380+
AppTheme::MidnightGreen => {
381+
if reverse {
382+
(
383+
Color::Red,
384+
Color::Rgb(255, 140, 0),
385+
Color::Yellow,
386+
Color::LightGreen,
387+
Color::Green,
388+
)
389+
} else {
390+
(
391+
Color::Green,
392+
Color::LightGreen,
393+
Color::Yellow,
394+
Color::Rgb(255, 140, 0),
395+
Color::Red,
396+
)
397+
}
398+
}
399+
AppTheme::IAmBlue => {
400+
if reverse {
401+
(
402+
Color::Red,
403+
Color::Rgb(255, 140, 0),
404+
Color::Yellow,
405+
Color::Cyan,
406+
Color::Blue,
407+
)
408+
} else {
409+
(
410+
Color::Blue,
411+
Color::Cyan,
412+
Color::Yellow,
413+
Color::Rgb(255, 140, 0),
414+
Color::Red,
415+
)
416+
}
417+
}
418+
AppTheme::SolarizedDark => {
419+
if reverse {
420+
(
421+
Color::Rgb(220, 50, 47),
422+
Color::Rgb(203, 75, 22),
423+
Color::Rgb(181, 137, 0),
424+
Color::Rgb(133, 153, 0),
425+
Color::Rgb(42, 161, 152),
426+
)
427+
} else {
428+
(
429+
Color::Rgb(42, 161, 152),
430+
Color::Rgb(133, 153, 0),
431+
Color::Rgb(181, 137, 0),
432+
Color::Rgb(203, 75, 22),
433+
Color::Rgb(220, 50, 47),
434+
)
435+
}
436+
}
437+
AppTheme::Greyscale => {
438+
if reverse {
439+
(
440+
Color::Rgb(80, 80, 80),
441+
Color::Rgb(120, 120, 120),
442+
Color::Rgb(160, 160, 160),
443+
Color::Rgb(200, 200, 200),
444+
Color::Rgb(240, 240, 240),
445+
)
446+
} else {
447+
(
448+
Color::Rgb(240, 240, 240),
449+
Color::Rgb(200, 200, 200),
450+
Color::Rgb(160, 160, 160),
451+
Color::Rgb(120, 120, 120),
452+
Color::Rgb(80, 80, 80),
453+
)
454+
}
455+
}
456+
AppTheme::Nord => {
457+
if reverse {
458+
(
459+
Color::Rgb(191, 97, 106),
460+
Color::Rgb(208, 135, 112),
461+
Color::Rgb(235, 203, 139),
462+
Color::Rgb(163, 190, 140),
463+
Color::Rgb(136, 192, 208),
464+
)
465+
} else {
466+
(
467+
Color::Rgb(136, 192, 208),
468+
Color::Rgb(163, 190, 140),
469+
Color::Rgb(235, 203, 139),
470+
Color::Rgb(208, 135, 112),
471+
Color::Rgb(191, 97, 106),
472+
)
473+
}
474+
}
475+
AppTheme::Dracula => {
476+
if reverse {
477+
(
478+
Color::Rgb(255, 85, 85),
479+
Color::Rgb(255, 184, 108),
480+
Color::Rgb(241, 250, 140),
481+
Color::Rgb(139, 233, 253),
482+
Color::Rgb(80, 250, 123),
483+
)
484+
} else {
485+
(
486+
Color::Rgb(80, 250, 123),
487+
Color::Rgb(139, 233, 253),
488+
Color::Rgb(241, 250, 140),
489+
Color::Rgb(255, 184, 108),
490+
Color::Rgb(255, 85, 85),
491+
)
492+
}
493+
}
494+
AppTheme::Monokai => {
495+
if reverse {
496+
(
497+
Color::Rgb(249, 38, 114),
498+
Color::Rgb(253, 151, 31),
499+
Color::Rgb(230, 219, 116),
500+
Color::Rgb(102, 217, 239),
501+
Color::Rgb(166, 226, 46),
502+
)
503+
} else {
504+
(
505+
Color::Rgb(166, 226, 46),
506+
Color::Rgb(102, 217, 239),
507+
Color::Rgb(230, 219, 116),
508+
Color::Rgb(253, 151, 31),
509+
Color::Rgb(249, 38, 114),
510+
)
511+
}
512+
}
513+
AppTheme::Gruvbox => {
514+
if reverse {
515+
(
516+
Color::Rgb(251, 73, 52),
517+
Color::Rgb(254, 128, 25),
518+
Color::Rgb(250, 189, 47),
519+
Color::Rgb(184, 187, 38),
520+
Color::Rgb(104, 157, 106),
521+
)
522+
} else {
523+
(
524+
Color::Rgb(104, 157, 106),
525+
Color::Rgb(184, 187, 38),
526+
Color::Rgb(250, 189, 47),
527+
Color::Rgb(254, 128, 25),
528+
Color::Rgb(251, 73, 52),
529+
)
530+
}
531+
}
532+
AppTheme::TokyoNight => {
533+
if reverse {
534+
(
535+
Color::Rgb(247, 118, 142),
536+
Color::Rgb(255, 158, 100),
537+
Color::Rgb(224, 175, 104),
538+
Color::Rgb(125, 207, 255),
539+
Color::Rgb(158, 206, 106),
540+
)
541+
} else {
542+
(
543+
Color::Rgb(158, 206, 106),
544+
Color::Rgb(125, 207, 255),
545+
Color::Rgb(224, 175, 104),
546+
Color::Rgb(255, 158, 100),
547+
Color::Rgb(247, 118, 142),
548+
)
549+
}
550+
}
551+
AppTheme::CatppuccinMocha => {
552+
if reverse {
553+
(
554+
Color::Rgb(243, 139, 168),
555+
Color::Rgb(250, 179, 135),
556+
Color::Rgb(249, 226, 175),
557+
Color::Rgb(137, 220, 235),
558+
Color::Rgb(166, 227, 161),
559+
)
560+
} else {
561+
(
562+
Color::Rgb(166, 227, 161),
563+
Color::Rgb(137, 220, 235),
564+
Color::Rgb(249, 226, 175),
565+
Color::Rgb(250, 179, 135),
566+
Color::Rgb(243, 139, 168),
567+
)
568+
}
569+
}
570+
AppTheme::OneDark => {
571+
if reverse {
572+
(
573+
Color::Rgb(224, 108, 117),
574+
Color::Rgb(209, 154, 102),
575+
Color::Rgb(229, 192, 123),
576+
Color::Rgb(86, 182, 194),
577+
Color::Rgb(152, 195, 121),
578+
)
579+
} else {
580+
(
581+
Color::Rgb(152, 195, 121),
582+
Color::Rgb(86, 182, 194),
583+
Color::Rgb(229, 192, 123),
584+
Color::Rgb(209, 154, 102),
585+
Color::Rgb(224, 108, 117),
586+
)
587+
}
588+
}
589+
AppTheme::AyuDark => {
590+
if reverse {
591+
(
592+
Color::Rgb(255, 51, 51),
593+
Color::Rgb(255, 160, 122),
594+
Color::Rgb(255, 204, 102),
595+
Color::Rgb(95, 175, 239),
596+
Color::Rgb(195, 232, 141),
597+
)
598+
} else {
599+
(
600+
Color::Rgb(195, 232, 141),
601+
Color::Rgb(95, 175, 239),
602+
Color::Rgb(255, 204, 102),
603+
Color::Rgb(255, 160, 122),
604+
Color::Rgb(255, 51, 51),
605+
)
606+
}
607+
}
608+
AppTheme::USA => {
609+
if reverse {
610+
(
611+
Color::Rgb(187, 19, 62),
612+
Color::Rgb(255, 100, 100),
613+
Color::White,
614+
Color::Rgb(100, 149, 237),
615+
Color::Rgb(10, 49, 97),
616+
)
617+
} else {
618+
(
619+
Color::Rgb(10, 49, 97),
620+
Color::Rgb(100, 149, 237),
621+
Color::White,
622+
Color::Rgb(255, 100, 100),
623+
Color::Rgb(187, 19, 62),
624+
)
625+
}
626+
}
627+
};
628+
629+
if value <= very_low_threshold {
630+
very_low_color
631+
} else if value <= low_threshold {
632+
low_color
633+
} else if value < high_threshold {
634+
mid_color
635+
} else if value < very_high_threshold {
636+
high_color
637+
} else {
638+
very_high_color
639+
}
640+
}
641+
284642
/// Returns the next theme.
285643
pub fn next(&self) -> Self {
286644
match self {

0 commit comments

Comments
 (0)