fluent_ansi/
targeted_color.rs1use core::fmt::{Display, Formatter, Result};
2
3use crate::{
4 Style, color::Color, impl_macros::additive_styling::impl_additive_styling_type,
5 impl_styling_atribute_for, impl_styling_element_for,
6};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct TargetedColor {
11 color: Color,
12 target: ColorTarget,
13}
14
15impl TargetedColor {
16 #[must_use]
18 pub fn new(color: impl Into<Color>, target: ColorTarget) -> Self {
19 let color = color.into();
20 Self { color, target }
21 }
22
23 #[must_use]
25 pub fn new_for_fg(color: impl Into<Color>) -> Self {
26 Self::new(color, ColorTarget::Foreground)
27 }
28
29 #[must_use]
31 pub fn new_for_bg(color: impl Into<Color>) -> Self {
32 Self::new(color, ColorTarget::Background)
33 }
34
35 #[must_use]
37 pub fn new_for_underline(color: impl Into<Color>) -> Self {
38 Self::new(color, ColorTarget::Underline)
39 }
40
41 #[must_use]
43 pub const fn get_color(self) -> Color {
44 self.color
45 }
46
47 #[must_use]
49 pub const fn get_target(self) -> ColorTarget {
50 self.target
51 }
52}
53
54impl_additive_styling_type!(TargetedColor {
55 args: [self];
56 to_style: { Style::new().color(self) }
57});
58
59impl_styling_element_for! { TargetedColor {
60 args: [self, composed_styling];
61 add_to: {
62 composed_styling.set_color(self.get_target(), Some(self.get_color()))
63 }
64}}
65
66impl Display for TargetedColor {
67 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
68 self.to_style().fmt(f)
69 }
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
74pub enum ColorTarget {
75 Foreground,
77 Background,
79 Underline,
81}
82
83impl_styling_atribute_for! { ColorTarget {
84 type Value = Option<Color>;
85 args: [self, composed_styling, value];
86
87 set_in: {
88 composed_styling.set_color(self, value)
89 }
90
91 get_from: {
92 composed_styling.get_color(self)
93 }
94}}