fluent_ansi/colors/
rgb.rs1use core::fmt::Result;
2
3use crate::{
4 CodeWriter, ColorTarget,
5 color::{Color, WriteColorCodes},
6 impl_macros::color_type::impl_color_type,
7};
8
9pub type RGB = RGBColor;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub struct RGBColor {
25 pub r: u8,
27 pub g: u8,
29 pub b: u8,
31}
32
33impl RGBColor {
34 #[must_use]
36 pub const fn new(r: u8, g: u8, b: u8) -> Self {
37 Self { r, g, b }
38 }
39}
40
41impl_color_type!(RGBColor {
42 args: [self];
43 to_color: { Color::RGB(self) }
44});
45
46impl WriteColorCodes for RGBColor {
47 fn write_color_codes(self, target: ColorTarget, writer: &mut CodeWriter) -> Result {
48 let target_code = match target {
49 ColorTarget::Foreground => 38,
50 ColorTarget::Background => 48,
51 ColorTarget::Underline => 58,
52 };
53
54 writer.write_code(target_code)?;
55 writer.write_code(2)?;
56 writer.write_code(self.r)?;
57 writer.write_code(self.g)?;
58 writer.write_code(self.b)?;
59 Ok(())
60 }
61}