chromata/integration/plotters.rs
1//! Plotters charting library integration.
2//!
3//! Provides [`From<Color>`](crate::Color) for [`plotters::style::RGBColor`] and a
4//! convenience method on [`Theme`](crate::Theme).
5//!
6//! # Enable
7//!
8//! ```toml
9//! [dependencies]
10//! chromata = { version = "1", features = ["plotters-integration"] }
11//! ```
12//!
13//! # Example
14//!
15//! ```rust,ignore
16//! use chromata::popular::gruvbox;
17//!
18//! let theme = gruvbox::DARK_HARD;
19//! let bg: plotters::style::RGBColor = theme.bg.into();
20//! ```
21//!
22//! # Convenience
23//!
24//! ```rust,ignore
25//! let series_colors = theme.to_plotters_series_colors();
26//! // Vec of accent RGBColors for chart series
27//! ```
28
29use alloc::vec::Vec;
30
31use crate::{Color, Theme};
32
33/// Convert a chromata Color to a plotters RGBColor.
34impl From<Color> for ::plotters::style::RGBColor {
35 fn from(c: Color) -> Self {
36 ::plotters::style::RGBColor(c.r, c.g, c.b)
37 }
38}
39
40impl Theme {
41 /// Return accent colors as plotters RGBColors for chart series.
42 ///
43 /// Collects defined accent colors (red, orange, yellow, green, cyan,
44 /// blue, purple, magenta) in palette order, skipping any that are `None`.
45 #[must_use]
46 pub fn to_plotters_series_colors(&self) -> Vec<::plotters::style::RGBColor> {
47 let accents = [
48 self.red,
49 self.orange,
50 self.yellow,
51 self.green,
52 self.cyan,
53 self.blue,
54 self.purple,
55 self.magenta,
56 ];
57 accents
58 .iter()
59 .filter_map(|c| c.map(::plotters::style::RGBColor::from))
60 .collect()
61 }
62}