color_char/character/
mod.rs

1mod arith;
2mod convert;
3mod display;
4
5#[cfg(feature = "serde")]
6mod der;
7#[cfg(feature = "serde")]
8mod ser;
9/// A colored character, has the same memory layout as `u32`.
10///
11/// # Examples
12///
13/// ```
14/// use color_char::Character;
15/// let c = Character::default().with_char('Z').with_color(26);
16/// ```
17#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub struct Character {
19    repr: u32,
20}
21
22impl Character {
23    /// Create a new colored character with the given character and color.
24    ///
25    /// # Examples
26    ///
27    /// ```
28    /// use color_char::Character;
29    /// ```
30    pub fn new(char: char, color: u32) -> Self {
31        Self { repr: color << 21 | char as u32 }
32    }
33    /// Get the character for last 21 bits
34    ///
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// use color_char::Character;
40    /// let c = Character::from(0b000011010_000000000000001011010);
41    /// assert_eq!(c.get_char(), 'Z');
42    /// assert_eq!(c.get_color(), 26);
43    /// ```
44    #[inline]
45    pub fn get_char(&self) -> char {
46        unsafe { char::from_u32_unchecked(self.erase_color()) }
47    }
48    /// Set the character on last 21 bits
49    ///
50    /// # Examples
51    ///
52    /// ```
53    /// use color_char::Character;
54    /// let mut c = Character::default();
55    /// c.set_char('Z');
56    /// assert_eq!(c, 'Z');
57    /// ```
58    #[inline]
59    pub fn set_char(&mut self, new: char) {
60        self.repr = self.erase_char() | new as u32
61    }
62    /// Build a new character with given char
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// use color_char::Character;
68    /// let c = Character::default().with_char('Z');
69    /// assert_eq!(c, 'Z');
70    /// ```
71    #[inline]
72    pub fn with_char(self, new: char) -> Self {
73        Self { repr: self.erase_char() | new as u32 }
74    }
75    /// Get the color id from first 11 bits
76    ///
77    ///
78    /// # Examples
79    ///
80    /// ```
81    /// use color_char::Character;
82    /// let c = Character::from(0b000011010_000000000000001011010);
83    /// assert_eq!(c.get_color(), 26);
84    /// ```
85    #[inline]
86    pub fn get_color(&self) -> u32 {
87        self.repr >> 21
88    }
89    /// Set the color id on first 11 bits
90    ///
91    /// # Examples
92    ///
93    /// ```
94    /// use color_char::Character;
95    /// let mut c = Character::default();
96    /// c.set_color(26);
97    /// assert_eq!(c.get_color(), 26);
98    /// ```
99    #[inline]
100    pub fn set_color(&mut self, new: u32) {
101        debug_assert!(new <= 0x07FF);
102        self.repr = self.erase_color() | new << 21
103    }
104    /// Build a new character with given color
105    ///
106    /// # Examples
107    ///
108    /// ```
109    /// use color_char::Character;
110    /// let c = Character::default().with_color(26);
111    /// assert_eq!(c.get_color(), 26);
112    /// ```
113    #[inline]
114    pub fn with_color(self, new: u32) -> Self {
115        debug_assert!(new <= 0x07FF);
116        Self { repr: self.erase_color() | new << 21 }
117    }
118    #[inline]
119    fn erase_color(&self) -> u32 {
120        self.repr & 0x001FFFFF
121    }
122    #[inline]
123    fn erase_char(&self) -> u32 {
124        self.repr & 0xFFE00000
125    }
126}