1use genpdf::style;
34
35use crate::{AnsiColor, AnsiMode, Color, Style, StyledStr, StyledString};
36
37impl From<Color> for style::Color {
38 fn from(c: Color) -> style::Color {
39 match c {
40 Color::Ansi { color, mode } => get_rgb_color(color, mode),
41 Color::Rgb { r, g, b } => style::Color::Rgb(r, g, b),
42 }
43 }
44}
45
46fn get_rgb_color(color: AnsiColor, mode: AnsiMode) -> style::Color {
47 use AnsiColor::*;
48 use AnsiMode::*;
49
50 let (r, g, b) = match (mode, color) {
51 (Dark, Black) => (0, 0, 0),
52 (Dark, Red) => (170, 0, 0),
53 (Dark, Green) => (0, 170, 0),
54 (Dark, Yellow) => (170, 85, 0),
55 (Dark, Blue) => (0, 0, 170),
56 (Dark, Magenta) => (170, 0, 170),
57 (Dark, Cyan) => (0, 170, 170),
58 (Dark, White) => (170, 170, 170),
59 (Light, Black) => (85, 85, 85),
60 (Light, Red) => (255, 85, 85),
61 (Light, Green) => (85, 255, 85),
62 (Light, Yellow) => (255, 255, 85),
63 (Light, Blue) => (85, 85, 255),
64 (Light, Magenta) => (255, 85, 255),
65 (Light, Cyan) => (85, 255, 255),
66 (Light, White) => (255, 255, 255),
67 };
68 style::Color::Rgb(r, g, b)
69}
70
71impl From<Style> for style::Style {
72 fn from(s: Style) -> style::Style {
73 let mut style = style::Style::new();
74 if let Some(color) = s.fg {
75 style.set_color(color.into());
76 }
77 if s.effects.is_bold {
78 style.set_bold();
79 }
80 if s.effects.is_italic {
81 style.set_italic();
82 }
83 style
84 }
85}
86
87impl<'a, 's> From<&'a StyledStr<'s>> for style::StyledStr<'s> {
88 fn from(s: &'a StyledStr<'s>) -> style::StyledStr<'s> {
89 style::StyledStr::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
90 }
91}
92
93impl<'s> From<StyledStr<'s>> for style::StyledStr<'s> {
94 fn from(s: StyledStr<'s>) -> style::StyledStr<'s> {
95 style::StyledStr::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
96 }
97}
98
99impl<'s> From<&'s StyledString> for style::StyledStr<'s> {
100 fn from(s: &'s StyledString) -> style::StyledStr<'s> {
101 style::StyledStr::new(&s.s, s.style.map(style::Style::from).unwrap_or_default())
102 }
103}
104
105impl<'a, 's> From<&'a StyledStr<'s>> for style::StyledString {
106 fn from(s: &'a StyledStr<'s>) -> style::StyledString {
107 style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
108 }
109}
110
111impl<'s> From<StyledStr<'s>> for style::StyledString {
112 fn from(s: StyledStr<'s>) -> style::StyledString {
113 style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
114 }
115}
116
117impl<'s> From<&'s StyledString> for style::StyledString {
118 fn from(s: &'s StyledString) -> style::StyledString {
119 style::StyledString::new(&s.s, s.style.map(style::Style::from).unwrap_or_default())
120 }
121}
122
123impl From<StyledString> for style::StyledString {
124 fn from(s: StyledString) -> style::StyledString {
125 style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
126 }
127}