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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
use crate::render::{Color, LineStyle};
/// Comprehensive theme system for consistent plot styling
///
/// Themes control the visual appearance of all plot elements including colors,
/// fonts, spacing, and other visual properties.
///
/// # Available Themes
///
/// | Theme | Description |
/// |-------|-------------|
/// | [`Theme::light()`] | Default light theme with white background |
/// | [`Theme::dark()`] | Dark mode with dark background |
/// | [`Theme::seaborn()`] | Seaborn-inspired styling |
/// | [`Theme::publication()`] | Publication-ready, high contrast |
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// let x: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();
/// let y: Vec<f64> = x.iter().map(|&v| v.sin()).collect();
///
/// // Using dark theme
/// Plot::new()
/// .theme(Theme::dark())
/// .line(&x, &y)
/// .end_series()
/// .save("dark_plot.png")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Visual Comparison
///
/// | Default | Dark | Seaborn | Publication |
/// |---------|------|---------|-------------|
/// |  |  |  |  |
#[derive(Debug, Clone)]
pub struct Theme {
/// Background color of the plot area
pub background: Color,
/// Foreground color for text and axes
pub foreground: Color,
/// Grid line color
pub grid_color: Color,
/// Default line width for plot elements (in points, 1pt = 1/72 inch)
pub line_width: f32,
/// Default line style
pub line_style: LineStyle,
/// Primary font family name
pub font_family: String,
/// Default font size for labels and text (in points)
pub font_size: f32,
/// Title font size (in points)
pub title_font_size: f32,
/// Legend font size (in points)
pub legend_font_size: f32,
/// Axis label font size (in points)
pub axis_label_font_size: f32,
/// Tick label font size (in points)
pub tick_label_font_size: f32,
/// Default color palette for automatic color cycling
pub color_palette: Vec<Color>,
/// Margin around the plot (as fraction of canvas size)
pub margin: f32,
/// Padding between plot elements (in points)
pub padding: f32,
/// Use colorblind-friendly palette
pub colorblind_friendly: bool,
}
impl Theme {
/// Create a new theme builder
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// let custom_theme = Theme::builder()
/// .background(Color::from_hex("#1a1a2e").unwrap())
/// .foreground(Color::WHITE)
/// .font_size(12.0)
/// .build();
/// ```
pub fn builder() -> ThemeBuilder {
ThemeBuilder::default()
}
/// Create default light theme
///
/// White background with black text, suitable for documents and light mode interfaces.
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// Plot::new()
/// .theme(Theme::light())
/// .line(&[1.0, 2.0, 3.0], &[1.0, 4.0, 9.0])
/// .end_series()
/// .save("light_plot.png")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn light() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
grid_color: Color::from_gray(176), // #B0B0B0, matches GridStyle::default()
line_width: 1.5, // matplotlib default: 1.5pt
line_style: LineStyle::Solid,
font_family: "sans-serif".to_string(),
font_size: 10.0, // matplotlib default: 10pt
title_font_size: 14.0, // 1.4x base
legend_font_size: 9.0, // 0.9x base
axis_label_font_size: 10.0, // 1.0x base
tick_label_font_size: 9.0, // 0.9x base
color_palette: Color::default_palette().to_vec(),
margin: 0.1,
padding: 8.0,
colorblind_friendly: false,
}
}
/// Create dark theme
///
/// Dark background with light text, ideal for dark mode interfaces and reducing eye strain.
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// Plot::new()
/// .theme(Theme::dark())
/// .line(&[1.0, 2.0, 3.0], &[1.0, 4.0, 9.0])
/// .end_series()
/// .save("dark_plot.png")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn dark() -> Self {
Self {
background: Color::from_hex("#1e1e1e").unwrap(),
foreground: Color::WHITE,
grid_color: Color::DARK_GRAY,
line_width: 1.5, // matplotlib default: 1.5pt
line_style: LineStyle::Solid,
font_family: "sans-serif".to_string(),
font_size: 10.0, // matplotlib default: 10pt
title_font_size: 14.0, // 1.4x base
legend_font_size: 9.0, // 0.9x base
axis_label_font_size: 10.0, // 1.0x base
tick_label_font_size: 9.0, // 0.9x base
color_palette: Self::dark_palette(),
margin: 0.1,
padding: 8.0,
colorblind_friendly: false,
}
}
/// Create publication-ready theme (high contrast, clean)
///
/// Uses serif fonts and grayscale colors suitable for academic journals and print.
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// Plot::new()
/// .theme(Theme::publication())
/// .line(&[1.0, 2.0, 3.0], &[1.0, 4.0, 9.0])
/// .end_series()
/// .save("publication_plot.png")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn publication() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
// #B0B0B0: 2.17:1 on white, matching GridStyle::default(). The old
// #E0E0E0 was 1.31:1 and effectively invisible on screen.
grid_color: Color::from_gray(176),
line_width: 1.5,
line_style: LineStyle::Solid,
font_family: "Times New Roman".to_string(),
font_size: 10.0,
title_font_size: 12.0,
legend_font_size: 9.0,
axis_label_font_size: 10.0,
tick_label_font_size: 8.0,
color_palette: Self::publication_palette(),
margin: 0.08,
padding: 6.0,
colorblind_friendly: false,
}
}
/// Create minimal theme (minimal visual elements)
pub fn minimal() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
grid_color: Color::TRANSPARENT,
line_width: 1.5,
line_style: LineStyle::Solid,
font_family: "Helvetica".to_string(),
font_size: 11.0,
title_font_size: 14.0,
legend_font_size: 10.0,
axis_label_font_size: 11.0,
tick_label_font_size: 9.0,
color_palette: Self::minimal_palette(),
margin: 0.05,
padding: 4.0,
colorblind_friendly: false,
}
}
/// Create colorblind-friendly theme
pub fn colorblind_friendly() -> Self {
let mut theme = Self::light();
theme.color_palette = Self::colorblind_palette();
theme.colorblind_friendly = true;
theme
}
/// Create seaborn-style theme (matplotlib-inspired, clean and professional)
///
/// Inspired by Python's seaborn library, with a clean, modern aesthetic.
///
/// # Example
///
/// ```rust,no_run
/// use ruviz::prelude::*;
///
/// Plot::new()
/// .theme(Theme::seaborn())
/// .line(&[1.0, 2.0, 3.0], &[1.0, 4.0, 9.0])
/// .end_series()
/// .save("seaborn_plot.png")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn seaborn() -> Self {
Self {
background: Color::WHITE,
foreground: Color::from_hex("#262626").unwrap(), // Dark gray instead of pure black
grid_color: Color::from_gray(176), // #B0B0B0, readable mid-gray grid
line_width: 1.5,
line_style: LineStyle::Solid,
font_family: "DejaVu Sans".to_string(), // Seaborn's preferred font
font_size: 11.0,
title_font_size: 14.0,
legend_font_size: 10.0,
axis_label_font_size: 11.0,
tick_label_font_size: 9.0,
color_palette: Self::seaborn_palette(),
margin: 0.08,
padding: 8.0,
colorblind_friendly: false,
}
}
/// Create IEEE publication-ready theme
/// Optimized for IEEE journal column constraints and professional typography
pub fn ieee() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
// #C8C8C8: 1.86:1 on white. Lighter than GridStyle::default() because
// IEEE targets print, but no longer the 1.27:1 wash of #E5E5E5.
grid_color: Color::from_gray(200),
line_width: 0.75, // Match PlotStyle::IEEE data_width
line_style: LineStyle::Solid,
font_family: "serif".to_string(), // IEEE standard serif font
font_size: 8.0, // Small size for column constraints
title_font_size: 9.0, // IEEE title sizing (1.125x base)
legend_font_size: 6.0, // Compact legend (0.75x base)
axis_label_font_size: 8.0, // 1.0x base
tick_label_font_size: 7.0, // 0.875x base
color_palette: Self::wong_palette(), // Accessibility-first colorblind friendly
margin: 0.12, // IEEE standard margins
padding: 6.0,
colorblind_friendly: true,
}
}
/// Create Nature/Science journal theme
/// Follows Nature journal style guidelines for multi-panel figures
pub fn nature() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
grid_color: Color::TRANSPARENT, // Nature style: no grid
line_width: 0.75, // Match PlotStyle::Nature data_width
line_style: LineStyle::Solid,
font_family: "sans-serif".to_string(), // Nature standard sans-serif
font_size: 7.0, // Small for multi-panel figures
title_font_size: 8.05, // 1.15x base
legend_font_size: 5.95, // 0.85x base
axis_label_font_size: 7.0, // 1.0x base
tick_label_font_size: 5.95, // 0.85x base
color_palette: Self::scientific_palette(), // Scientific standard colors
margin: 0.08, // Tight margins for space efficiency
padding: 4.0,
colorblind_friendly: false,
}
}
/// Create presentation theme for slides and projectors
/// High contrast and large fonts for visibility from distance
pub fn presentation() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
// #B0B0B0: projector contrast is worse than a monitor's, so the grid
// has to be at least as readable as the default (2.17:1 on white).
grid_color: Color::from_gray(176),
line_width: 2.5, // Thick lines for visibility (same as PlotStyle::Presentation)
line_style: LineStyle::Solid,
font_family: "sans-serif".to_string(), // High legibility sans-serif
font_size: 14.0, // Large for distance viewing (same as PlotStyle::Presentation)
title_font_size: 19.6, // 1.4x base
legend_font_size: 11.9, // 0.85x base
axis_label_font_size: 14.0, // 1.0x base
tick_label_font_size: 11.9, // 0.85x base
color_palette: Self::presentation_palette(), // High contrast colors
margin: 0.15, // Extra spacing for clean look
padding: 12.0,
colorblind_friendly: false,
}
}
/// Create Paul Tol's accessibility theme
/// Uses Paul Tol's scientifically-tested color schemes
pub fn paul_tol() -> Self {
Self {
background: Color::WHITE,
foreground: Color::BLACK,
// #B0B0B0: 2.17:1 on white, matching GridStyle::default().
grid_color: Color::from_gray(176),
line_width: 1.5,
line_style: LineStyle::Solid,
font_family: "Arial".to_string(),
font_size: 11.0,
title_font_size: 14.0,
legend_font_size: 10.0,
axis_label_font_size: 11.0,
tick_label_font_size: 9.0,
color_palette: Self::paul_tol_palette(),
margin: 0.1,
padding: 8.0,
colorblind_friendly: true,
}
}
/// Get a color from the theme's palette by index (cycles if needed)
pub fn get_color(&self, index: usize) -> Color {
if self.color_palette.is_empty() {
Color::BLACK
} else {
self.color_palette[index % self.color_palette.len()]
}
}
/// Get the effective grid color (returns transparent if grid should be hidden)
pub fn effective_grid_color(&self) -> Color {
self.grid_color
}
// Color palettes for different themes
fn dark_palette() -> Vec<Color> {
vec![
Color::from_hex("#8dd3c7").unwrap(), // Light cyan
Color::from_hex("#ffffb3").unwrap(), // Light yellow
Color::from_hex("#bebada").unwrap(), // Light purple
Color::from_hex("#fb8072").unwrap(), // Light red
Color::from_hex("#80b1d3").unwrap(), // Light blue
Color::from_hex("#fdb462").unwrap(), // Light orange
Color::from_hex("#b3de69").unwrap(), // Light green
Color::from_hex("#fccde5").unwrap(), // Light pink
]
}
fn publication_palette() -> Vec<Color> {
vec![
Color::BLACK,
Color::DARK_GRAY,
Color::from_hex("#404040").unwrap(),
Color::from_hex("#606060").unwrap(),
Color::from_hex("#808080").unwrap(),
Color::from_hex("#A0A0A0").unwrap(),
]
}
fn minimal_palette() -> Vec<Color> {
vec![
Color::BLACK,
Color::from_hex("#666666").unwrap(),
Color::from_hex("#999999").unwrap(),
Color::from_hex("#CCCCCC").unwrap(),
]
}
fn colorblind_palette() -> Vec<Color> {
// Optimized for deuteranopia/protanopia (most common color blindness)
vec![
Color::from_hex("#1f77b4").unwrap(), // Blue
Color::from_hex("#ff7f0e").unwrap(), // Orange
Color::from_hex("#2ca02c").unwrap(), // Green
Color::from_hex("#d62728").unwrap(), // Red
Color::from_hex("#9467bd").unwrap(), // Purple
Color::from_hex("#8c564b").unwrap(), // Brown
Color::from_hex("#e377c2").unwrap(), // Pink
Color::from_hex("#bcbd22").unwrap(), // Olive
Color::from_hex("#17becf").unwrap(), // Cyan
]
}
/// Wong palette (Bang Wong's accessibility-tested colorblind-friendly palette)
/// Reference: https://davidmathlogic.com/colorblind/
fn wong_palette() -> Vec<Color> {
vec![
Color::from_hex("#000000").unwrap(), // Black
Color::from_hex("#E69F00").unwrap(), // Orange
Color::from_hex("#56B4E9").unwrap(), // Sky blue
Color::from_hex("#009E73").unwrap(), // Bluish green
Color::from_hex("#F0E442").unwrap(), // Yellow
Color::from_hex("#0072B2").unwrap(), // Blue
Color::from_hex("#D55E00").unwrap(), // Vermillion
Color::from_hex("#CC79A7").unwrap(), // Reddish purple
]
}
/// Paul Tol's high-contrast palette
/// Reference: https://personal.sron.nl/~pault/
fn paul_tol_palette() -> Vec<Color> {
vec![
Color::from_hex("#004488").unwrap(), // Dark blue
Color::from_hex("#DDAA33").unwrap(), // Gold
Color::from_hex("#BB5566").unwrap(), // Rose
Color::from_hex("#000000").unwrap(), // Black
Color::from_hex("#999933").unwrap(), // Olive
Color::from_hex("#DDDDDD").unwrap(), // Light gray
Color::from_hex("#EE8866").unwrap(), // Orange
Color::from_hex("#77AADD").unwrap(), // Light blue
]
}
/// Scientific palette (matplotlib v2+ accessibility tested)
fn scientific_palette() -> Vec<Color> {
vec![
Color::from_hex("#1f77b4").unwrap(), // Blue
Color::from_hex("#ff7f0e").unwrap(), // Orange
Color::from_hex("#2ca02c").unwrap(), // Green
Color::from_hex("#d62728").unwrap(), // Red
Color::from_hex("#9467bd").unwrap(), // Purple
Color::from_hex("#8c564b").unwrap(), // Brown
Color::from_hex("#e377c2").unwrap(), // Pink
Color::from_hex("#7f7f7f").unwrap(), // Gray
Color::from_hex("#bcbd22").unwrap(), // Olive
Color::from_hex("#17becf").unwrap(), // Cyan
]
}
/// High-contrast palette for presentations
fn presentation_palette() -> Vec<Color> {
vec![
Color::from_hex("#1f77b4").unwrap(), // Blue
Color::from_hex("#ff7f0e").unwrap(), // Orange
Color::from_hex("#2ca02c").unwrap(), // Green
Color::from_hex("#d62728").unwrap(), // Red
Color::from_hex("#9467bd").unwrap(), // Purple
Color::from_hex("#000000").unwrap(), // Black
]
}
fn seaborn_palette() -> Vec<Color> {
// Seaborn's default color palette (muted colors, professional)
vec![
Color::from_hex("#1f77b4").unwrap(), // Muted blue
Color::from_hex("#ff7f0e").unwrap(), // Muted orange
Color::from_hex("#2ca02c").unwrap(), // Muted green
Color::from_hex("#d62728").unwrap(), // Muted red
Color::from_hex("#9467bd").unwrap(), // Muted purple
Color::from_hex("#8c564b").unwrap(), // Muted brown
Color::from_hex("#e377c2").unwrap(), // Muted pink
Color::from_hex("#7f7f7f").unwrap(), // Muted gray
Color::from_hex("#bcbd22").unwrap(), // Muted olive
Color::from_hex("#17becf").unwrap(), // Muted cyan
]
}
/// Convert theme typography settings to a TypographyConfig
///
/// This allows Theme to be used with the new PlotConfig system.
/// All font sizes in Theme are in points.
///
/// # Example
///
/// ```rust,ignore
/// let theme = Theme::presentation();
/// let typography = theme.to_typography_config();
/// ```
pub fn to_typography_config(&self) -> crate::core::config::TypographyConfig {
use super::FontFamily;
use crate::core::config::TypographyConfig;
let defaults = TypographyConfig::default();
let base_size = if self.font_size.is_finite() && self.font_size > 0.0 {
self.font_size
} else {
defaults.base_size
};
let scale = |size: f32, fallback: f32| {
if size.is_finite() && size > 0.0 {
size / base_size
} else {
fallback
}
};
// Preserve exact font family names while keeping generic CSS family
// names mapped to their corresponding portable family.
let family = FontFamily::from(self.font_family.as_str());
TypographyConfig {
base_size,
title_scale: scale(self.title_font_size, defaults.title_scale),
label_scale: scale(self.axis_label_font_size, defaults.label_scale),
tick_scale: scale(self.tick_label_font_size, defaults.tick_scale),
legend_scale: scale(self.legend_font_size, defaults.legend_scale),
family,
title_weight: super::FontWeight::Normal,
}
}
/// Convert theme line settings to a LineConfig
///
/// This allows Theme to be used with the new PlotConfig system.
/// Line widths in Theme are in points.
pub fn to_line_config(&self) -> crate::core::config::LineConfig {
use crate::core::config::LineConfig;
let scale = self.line_width / 1.5;
LineConfig {
data_width: self.line_width,
axis_width: 0.8 * scale,
grid_width: 0.8 * scale, // matches GridStyle::default().line_width
tick_width: 0.6 * scale,
tick_length: 4.0, // Standard tick length
}
}
}
impl Default for Theme {
/// Default theme is the light theme
fn default() -> Self {
Self::light()
}
}
/// Builder pattern for creating custom themes
#[derive(Debug, Clone)]
pub struct ThemeBuilder {
theme: Theme,
}
impl ThemeBuilder {
/// Set background color
pub fn background(mut self, color: Color) -> Self {
self.theme.background = color;
self
}
/// Set foreground color
pub fn foreground(mut self, color: Color) -> Self {
self.theme.foreground = color;
self
}
/// Set grid color
pub fn grid_color(mut self, color: Color) -> Self {
self.theme.grid_color = color;
self
}
/// Set default line width
pub fn line_width(mut self, width: f32) -> Self {
self.theme.line_width = width.max(0.1);
self
}
/// Set default line style
pub fn line_style(mut self, style: LineStyle) -> Self {
self.theme.line_style = style;
self
}
/// Set font family
pub fn font<S: Into<String>>(mut self, font_family: S) -> Self {
self.theme.font_family = font_family.into();
self
}
/// Set default font size
pub fn font_size(mut self, size: f32) -> Self {
self.theme.font_size = size.max(6.0);
self
}
/// Set title font size
pub fn title_font_size(mut self, size: f32) -> Self {
self.theme.title_font_size = size.max(6.0);
self
}
/// Set legend font size
pub fn legend_font_size(mut self, size: f32) -> Self {
self.theme.legend_font_size = size.max(6.0);
self
}
/// Set color palette
pub fn palette<I>(mut self, colors: I) -> Self
where
I: IntoIterator<Item = Color>,
{
self.theme.color_palette = colors.into_iter().collect();
self
}
/// Enable colorblind-friendly palette
pub fn colorblind_palette(mut self, enabled: bool) -> Self {
self.theme.colorblind_friendly = enabled;
if enabled {
self.theme.color_palette = Theme::colorblind_palette();
}
self
}
/// Set margin
pub fn margin(mut self, margin: f32) -> Self {
self.theme.margin = margin.clamp(0.0, 0.5);
self
}
/// Set padding
pub fn padding(mut self, padding: f32) -> Self {
self.theme.padding = padding.max(0.0);
self
}
/// Build the theme
pub fn build(self) -> Theme {
self.theme
}
}
#[allow(clippy::derivable_impls)] // Theme::light() is the semantic default, not Theme::default()
impl Default for ThemeBuilder {
fn default() -> Self {
Self {
theme: Theme::light(),
}
}
}
/// Predefined theme variants
#[allow(clippy::upper_case_acronyms)] // IEEE is the standard organization acronym
pub enum ThemeVariant {
Light,
Dark,
Publication,
Minimal,
ColorblindFriendly,
Seaborn,
IEEE,
Nature,
Presentation,
PaulTol,
}
impl ThemeVariant {
/// Convert theme variant to actual theme
pub fn to_theme(&self) -> Theme {
match self {
ThemeVariant::Light => Theme::light(),
ThemeVariant::Dark => Theme::dark(),
ThemeVariant::Publication => Theme::publication(),
ThemeVariant::Minimal => Theme::minimal(),
ThemeVariant::ColorblindFriendly => Theme::colorblind_friendly(),
ThemeVariant::Seaborn => Theme::seaborn(),
ThemeVariant::IEEE => Theme::ieee(),
ThemeVariant::Nature => Theme::nature(),
ThemeVariant::Presentation => Theme::presentation(),
ThemeVariant::PaulTol => Theme::paul_tol(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_theme() {
let theme = Theme::default();
assert_eq!(theme.background, Color::WHITE);
assert_eq!(theme.foreground, Color::BLACK);
assert!(theme.font_size > 0.0);
assert!(!theme.color_palette.is_empty());
}
#[test]
fn test_theme_variants() {
let light = Theme::light();
let dark = Theme::dark();
let publication = Theme::publication();
let minimal = Theme::minimal();
let colorblind = Theme::colorblind_friendly();
assert_eq!(light.background, Color::WHITE);
assert_eq!(dark.background, Color::from_hex("#1e1e1e").unwrap());
assert_eq!(publication.font_family, "Times New Roman");
assert_eq!(minimal.font_family, "Helvetica");
assert!(colorblind.colorblind_friendly);
}
#[test]
fn test_theme_typography_preserves_named_font_families() {
let publication = Theme::publication().to_typography_config();
let minimal = Theme::minimal().to_typography_config();
let ieee = Theme::ieee().to_typography_config();
assert_eq!(
publication.family,
crate::render::FontFamily::Name("Times New Roman".to_string())
);
assert_eq!(
minimal.family,
crate::render::FontFamily::Name("Helvetica".to_string())
);
assert_eq!(ieee.family, crate::render::FontFamily::Serif);
}
#[test]
fn test_scientific_themes() {
let ieee = Theme::ieee();
let nature = Theme::nature();
let presentation = Theme::presentation();
let paul_tol = Theme::paul_tol();
// IEEE theme tests - uses serif font, 8pt base, 0.75pt lines
assert_eq!(ieee.font_family, "serif");
assert_eq!(ieee.font_size, 8.0);
assert!(ieee.colorblind_friendly);
assert!((ieee.line_width - 0.75).abs() < 0.01);
// Nature theme tests - uses sans-serif, 7pt base, no grid
assert_eq!(nature.font_family, "sans-serif");
assert_eq!(nature.font_size, 7.0);
assert_eq!(nature.grid_color, Color::TRANSPARENT);
// Presentation theme tests - 14pt base, 2.5pt lines
assert_eq!(presentation.font_size, 14.0);
assert!((presentation.line_width - 2.5).abs() < 0.01);
assert!((presentation.title_font_size - 19.6).abs() < 0.01);
// Paul Tol theme tests
assert!(paul_tol.colorblind_friendly);
assert_eq!(paul_tol.color_palette.len(), 8);
}
#[test]
fn test_scientific_color_palettes() {
let wong = Theme::wong_palette();
let paul_tol = Theme::paul_tol_palette();
let scientific = Theme::scientific_palette();
let presentation = Theme::presentation_palette();
// Wong palette should have 8 colors
assert_eq!(wong.len(), 8);
assert_eq!(wong[0], Color::from_hex("#000000").unwrap()); // Black
// Paul Tol palette should have 8 colors
assert_eq!(paul_tol.len(), 8);
assert_eq!(paul_tol[0], Color::from_hex("#004488").unwrap()); // Dark blue
// Scientific palette should have 10 colors
assert_eq!(scientific.len(), 10);
// Presentation palette should have 6 high-contrast colors
assert_eq!(presentation.len(), 6);
}
#[test]
fn test_theme_builder() {
let theme = Theme::builder()
.background(Color::BLUE)
.foreground(Color::WHITE)
.font("Helvetica")
.font_size(14.0)
.line_width(3.0)
.margin(0.05)
.colorblind_palette(true)
.build();
assert_eq!(theme.background, Color::BLUE);
assert_eq!(theme.foreground, Color::WHITE);
assert_eq!(theme.font_family, "Helvetica");
assert_eq!(theme.font_size, 14.0);
assert_eq!(theme.line_width, 3.0);
assert_eq!(theme.margin, 0.05);
assert!(theme.colorblind_friendly);
}
#[test]
fn test_color_cycling() {
let theme = Theme::light();
let color0 = theme.get_color(0);
let color1 = theme.get_color(1);
let color_cycle = theme.get_color(theme.color_palette.len());
assert_eq!(color0, color_cycle); // Should cycle back to first color
assert_ne!(color0, color1); // Different colors
}
#[test]
fn test_builder_validation() {
let theme = Theme::builder()
.font_size(-5.0) // Invalid, should be clamped
.line_width(-1.0) // Invalid, should be clamped
.margin(-0.1) // Invalid, should be clamped
.build();
assert!(theme.font_size >= 6.0); // Minimum font size
assert!(theme.line_width >= 0.1); // Minimum line width
assert!(theme.margin >= 0.0); // Minimum margin
}
#[test]
fn test_theme_variant_conversion() {
let light = ThemeVariant::Light.to_theme();
let dark = ThemeVariant::Dark.to_theme();
let ieee = ThemeVariant::IEEE.to_theme();
let nature = ThemeVariant::Nature.to_theme();
let presentation = ThemeVariant::Presentation.to_theme();
let paul_tol = ThemeVariant::PaulTol.to_theme();
assert_eq!(light.background, Color::WHITE);
assert_ne!(dark.background, Color::WHITE);
// Test scientific theme variants
assert_eq!(ieee.font_family, "serif");
assert_eq!(nature.grid_color, Color::TRANSPARENT);
assert_eq!(presentation.font_size, 14.0);
assert!(paul_tol.colorblind_friendly);
}
#[test]
fn test_empty_palette() {
let mut theme = Theme::light();
theme.color_palette.clear();
// Should return black for empty palette
assert_eq!(theme.get_color(0), Color::BLACK);
assert_eq!(theme.get_color(5), Color::BLACK);
}
}