console_display/pixel/
monochrome_pixel.rs

1use crate::{
2    color::TerminalColor,
3    impl_from_mono_chrome_pixel_for_datacell,
4    pixel::Pixel,
5};
6
7use crate::{
8    impl_getters,
9    impl_getters_mut,
10    impl_new,
11    widget::DataCell,
12};
13
14/// Represents a singular pixel implementing the [`Pixel`] trait.
15#[derive(Clone, Copy)]
16pub struct SinglePixel {
17    pixels: [bool; 1],
18}
19
20impl SinglePixel {
21    /// # Examples
22    ///
23    /// ```
24    /// #![allow(incomplete_features)]
25    /// #![feature(generic_const_exprs)]
26    ///
27    /// use console_display::pixel::{
28    ///     Pixel,
29    ///     monochrome_pixel::SinglePixel
30    /// };
31    ///
32    /// let pixel = SinglePixel::new ([
33    ///     true,
34    /// ]);
35    ///
36    /// let symbol = pixel.character();
37    ///
38    /// assert_eq!(symbol, '█');
39    ///
40    /// let pixel = SinglePixel::new ([
41    ///     false,
42    /// ]);
43    ///
44    /// let symbol = pixel.character();
45    ///
46    /// assert_eq!(symbol, ' ');
47    ///
48    /// ```
49    #[must_use]
50    pub const fn character(self) -> char {
51        if self.pixels[0] { '█' } else { ' ' }
52    }
53}
54
55impl Pixel for SinglePixel {
56    type U = bool;
57
58    const WIDTH: usize = 1;
59
60    const HEIGHT: usize = 1;
61
62    impl_getters!(pixels: [bool; 1]);
63
64    impl_getters_mut!(pixels: [bool; 1]);
65
66    impl_new!(SinglePixel, pixels: [bool; 1]);
67}
68
69impl_from_mono_chrome_pixel_for_datacell!(SinglePixel);
70
71/// Specifies a block of pixels with dimensions 1 (width) by 2 (height).
72#[derive(Clone, Copy)]
73pub struct DualPixel {
74    pixels: [bool; 2],
75}
76
77// Needed because rustfmt panics when skipping elements with some
78// special characters directly.
79// https://github.com/rust-lang/rustfmt/issues/6523
80#[rustfmt::skip]
81impl DualPixel {
82    #[rustfmt::skip]
83    const CHARS: [char; 4] = [
84        ' ', '▀',  
85        '▄', '█',
86    ];
87
88    #[rustfmt::skip]
89    const fn index(self) -> usize {
90        (self.pixels[0] as usize)      |
91        (self.pixels[1] as usize) << 1
92    }
93
94    /// # Examples
95    ///
96    /// ```
97    /// #![allow(incomplete_features)]
98    /// #![feature(generic_const_exprs)]
99    ///
100    /// use console_display::pixel::{
101    ///     Pixel,
102    ///     monochrome_pixel::DualPixel
103    /// };
104    /// let pixel = DualPixel::new ([
105    ///     true,  // #
106    ///     false, // _
107    /// ]);
108    ///
109    /// let symbol = pixel.character();
110    ///
111    /// assert_eq!(symbol, '▀');
112    ///
113    /// let pixel = DualPixel::new ([
114    ///     false, // _
115    ///     false, // _
116    /// ]);
117    ///
118    /// let symbol = pixel.character();
119    ///
120    /// assert_eq!(symbol, ' ');
121    ///
122    /// ```
123    #[must_use]
124    pub const fn character(self) -> char {
125        Self::CHARS[self.index()]
126    }
127}
128
129impl Pixel for DualPixel {
130    type U = bool;
131
132    const WIDTH: usize = 1;
133
134    const HEIGHT: usize = 2;
135
136    impl_getters!(pixels: [bool; 2]);
137
138    impl_getters_mut!(pixels: [bool; 2]);
139
140    impl_new!(DualPixel, pixels: [bool; 2]);
141}
142
143impl_from_mono_chrome_pixel_for_datacell!(DualPixel);
144
145/// Specifies a block of pixels with dimensions 2 (width) by 2 (height).
146#[derive(Clone, Copy)]
147pub struct QuadPixel {
148    pixels: [bool; 4],
149}
150
151// Needed because rustfmt panics when skipping elements with some
152// special characters directly.
153// https://github.com/rust-lang/rustfmt/issues/6523
154#[rustfmt::skip]
155impl QuadPixel {
156    #[rustfmt::skip]
157    const CHARS: [char; 16] = [
158        ' ', '▘', '▝', '▀', 
159        '▖', '▌', '▞', '▛', 
160        '▗', '▚', '▐', '▜', 
161        '▄', '▙', '▟', '█',
162    ];
163
164    #[rustfmt::skip]
165    const fn index(self) -> usize {
166        (self.pixels[0] as usize)      |
167        (self.pixels[1] as usize) << 1 |
168        (self.pixels[2] as usize) << 2 |
169        (self.pixels[3] as usize) << 3
170    }
171
172    /// # Examples
173    ///
174    /// ```
175    /// #![allow(incomplete_features)]
176    /// #![feature(generic_const_exprs)]
177    ///
178    /// use console_display::pixel::{
179    ///     Pixel,
180    ///     monochrome_pixel::QuadPixel,
181    /// };
182    ///
183    /// let pixel = QuadPixel::new([
184    ///     true, false, // #_
185    ///     false, true, // _#
186    /// ]);
187    ///
188    /// let symbol = pixel.character();
189    ///
190    /// assert_eq!(symbol, '▚')
191    /// ```
192    #[must_use]
193    pub const fn character(self) -> char {
194        Self::CHARS[self.index()]
195    }
196}
197
198impl Pixel for QuadPixel {
199    type U = bool;
200
201    const WIDTH: usize = 2;
202
203    const HEIGHT: usize = 2;
204
205    impl_getters!(pixels: [bool; 4]);
206
207    impl_getters_mut!(pixels: [bool; 4]);
208
209    impl_new!(QuadPixel, pixels: [bool; 4]);
210}
211
212impl_from_mono_chrome_pixel_for_datacell!(QuadPixel);
213
214/// Specifies a block of pixels with dimensions 2 (width) by 3 (height).
215#[derive(Clone, Copy)]
216pub struct HexPixel {
217    pixels: [bool; 6],
218}
219
220// Needed because rustfmt panics when skipping elements with some
221// special characters directly.
222// https://github.com/rust-lang/rustfmt/issues/6523
223#[rustfmt::skip]
224impl HexPixel {
225    #[rustfmt::skip]
226    const CHARS: [char; 64] = [
227        ' ', '🬀', '🬁', '🬂', '🬃', '🬄', '🬅', '🬆', '🬇', '🬈', '🬉', '🬊', '🬋', '🬌', '🬍', '🬎', 
228        '🬏', '🬐', '🬑', '🬒', '🬓', '▌', '🬔', '🬕', '🬖', '🬗', '🬘', '🬙', '🬚', '🬛', '🬜', '🬝', 
229        '🬞', '🬟', '🬠', '🬡', '🬢', '🬣', '🬤', '🬥', '🬦', '🬧', '▐', '🬨', '🬩', '🬪', '🬫', '🬬', 
230        '🬭', '🬮', '🬯', '🬰', '🬱', '🬲', '🬳', '🬴', '🬵', '🬶', '🬷', '🬸', '🬹', '🬺', '🬻', '█'
231    ];
232
233    #[rustfmt::skip]
234    const fn index(self) -> usize {
235        (self.pixels[0] as usize)      |
236        (self.pixels[1] as usize) << 1 |
237        (self.pixels[2] as usize) << 2 |
238        (self.pixels[3] as usize) << 3 |
239        (self.pixels[4] as usize) << 4 |
240        (self.pixels[5] as usize) << 5
241    }
242
243    /// # Examples
244    ///
245    /// ```
246    /// #![allow(incomplete_features)]
247    /// #![feature(generic_const_exprs)]
248    ///
249    /// use console_display::pixel::{
250    ///     Pixel,
251    ///     monochrome_pixel::HexPixel
252    /// };
253    ///
254    /// let pixel = HexPixel::new ([
255    ///     true, false, // #_
256    ///     false, true, // _#
257    ///     true, true,  // ##
258    /// ]);
259    ///
260    /// let symbol = pixel.character();
261    ///
262    /// assert_eq!(symbol, '🬶')
263    /// ```
264    #[must_use]
265    pub const fn character(self) -> char {
266        Self::CHARS[self.index()]
267    }
268}
269
270impl Pixel for HexPixel {
271    type U = bool;
272
273    const WIDTH: usize = 2;
274
275    const HEIGHT: usize = 3;
276
277    impl_getters!(pixels: [bool; 6]);
278
279    impl_getters_mut!(pixels: [bool; 6]);
280
281    impl_new!(HexPixel, pixels: [bool; 6]);
282}
283
284impl_from_mono_chrome_pixel_for_datacell!(HexPixel);
285
286/// Specifies a block of pixels with dimensions 2 (width) by 4 (height).
287#[derive(Clone, Copy)]
288pub struct OctPixel {
289    pixels: [bool; 8],
290}
291
292// Needed because rustfmt panics when skipping elements with some
293// special characters directly.
294// https://github.com/rust-lang/rustfmt/issues/6523
295#[rustfmt::skip]
296impl OctPixel {
297    #[rustfmt::skip]
298    const CHARS: [char; 256] = [
299        ' ', '𜺨', '𜺫', '🮂', '𜴀', '▘', '𜴁', '𜴂', '𜴃', '𜴄', '▝', '𜴅', '𜴆', '𜴇', '𜴈', '▀',
300        '𜴉', '𜴊', '𜴋', '𜴌', '🯦', '𜴍', '𜴎', '𜴏', '𜴐', '𜴑', '𜴒', '𜴓', '𜴔', '𜴕', '𜴖', '𜴗',
301        '𜴘', '𜴙', '𜴚', '𜴛', '𜴜', '𜴝', '𜴞', '𜴟', '🯧', '𜴠', '𜴡', '𜴢', '𜴣', '𜴤', '𜴥', '𜴦',
302        '𜴧', '𜴨', '𜴩', '𜴪', '𜴫', '𜴬', '𜴭', '𜴮', '𜴯', '𜴰', '𜴱', '𜴲', '𜴳', '𜴴', '𜴵', '🮅',
303        '𜺣', '𜴶', '𜴷', '𜴸', '𜴹', '𜴺', '𜴻', '𜴼', '𜴽', '𜴾', '𜴿', '𜵀', '𜵁', '𜵂', '𜵃', '𜵄',
304        '▖', '𜵅', '𜵆', '𜵇', '𜵈', '▌', '𜵉', '𜵊', '𜵋', '𜵌', '▞', '𜵍', '𜵎', '𜵏', '𜵐', '▛',
305        '𜵑', '𜵒', '𜵓', '𜵔', '𜵕', '𜵖', '𜵗', '𜵘', '𜵙', '𜵚', '𜵛', '𜵜', '𜵝', '𜵞', '𜵟', '𜵠',
306        '𜵡', '𜵢', '𜵣', '𜵤', '𜵥', '𜵦', '𜵧', '𜵨', '𜵩', '𜵪', '𜵫', '𜵬', '𜵭', '𜵮', '𜵯', '𜵰',
307        '𜺠', '𜵱', '𜵲', '𜵳', '𜵴', '𜵵', '𜵶', '𜵷', '𜵸', '𜵹', '𜵺', '𜵻', '𜵼', '𜵽', '𜵾', '𜵿',
308        '𜶀', '𜶁', '𜶂', '𜶃', '𜶄', '𜶅', '𜶆', '𜶇', '𜶈', '𜶉', '𜶊', '𜶋', '𜶌', '𜶍', '𜶎', '𜶏',
309        '▗', '𜶐', '𜶑', '𜶒', '𜶓', '▚', '𜶔', '𜶕', '𜶖', '𜶗', '▐', '𜶘', '𜶙', '𜶚', '𜶛', '▜',
310        '𜶜', '𜶝', '𜶞', '𜶟', '𜶠', '𜶡', '𜶢', '𜶣', '𜶤', '𜶥', '𜶦', '𜶧', '𜶨', '𜶩', '𜶪', '𜶫',
311        '▂', '𜶬', '𜶭', '𜶮', '𜶯', '𜶰', '𜶱', '𜶲', '𜶳', '𜶴', '𜶵', '𜶶', '𜶷', '𜶸', '𜶹', '𜶺',
312        '𜶻', '𜶼', '𜶽', '𜶾', '𜶿', '𜷀', '𜷁', '𜷂', '𜷃', '𜷄', '𜷅', '𜷆', '𜷇', '𜷈', '𜷉', '𜷊',
313        '𜷋', '𜷌', '𜷍', '𜷎', '𜷏', '𜷐', '𜷑', '𜷒', '𜷓', '𜷔', '𜷕', '𜷖', '𜷗', '𜷘', '𜷙', '𜷚',
314        '▄', '𜷛', '𜷜', '𜷝', '𜷞', '▙', '𜷟', '𜷠', '𜷡', '𜷢', '▟', '𜷣', '▆', '𜷤', '𜷥', '█',
315    ];
316
317    #[rustfmt::skip]
318    const fn index(self) -> usize {
319        (self.pixels[0] as usize)      |
320        (self.pixels[1] as usize) << 1 |
321        (self.pixels[2] as usize) << 2 |
322        (self.pixels[3] as usize) << 3 |
323        (self.pixels[4] as usize) << 4 |
324        (self.pixels[5] as usize) << 5 |
325        (self.pixels[6] as usize) << 6 |
326        (self.pixels[7] as usize) << 7
327    }
328
329    /// # Examples
330    ///
331    /// ```
332    /// #![allow(incomplete_features)]
333    /// #![feature(generic_const_exprs)]
334    ///
335    /// use console_display::pixel::{
336    ///     Pixel,
337    ///     monochrome_pixel::OctPixel
338    /// };
339    /// 
340    /// let pixel = OctPixel::new ([
341    ///     true, false, // #_
342    ///     false, true, // _#
343    ///     true, true,  // ##
344    ///     false, false // __
345    /// ]);
346    ///
347    /// let symbol = pixel.character();
348    ///
349    /// assert_eq!(symbol, '𜴰')
350    /// ```
351    #[must_use]
352    pub const fn character(self) -> char {
353        Self::CHARS[self.index()]
354    }
355}
356
357impl Pixel for OctPixel {
358    type U = bool;
359
360    const WIDTH: usize = 2;
361
362    const HEIGHT: usize = 4;
363
364    impl_getters!(pixels: [bool; 8]);
365
366    impl_getters_mut!(pixels: [bool; 8]);
367
368    impl_new!(OctPixel, pixels: [bool; 8]);
369}
370
371impl_from_mono_chrome_pixel_for_datacell!(OctPixel);
372
373/// Specifies a block of pixels with dimensions 2 (width) by 4 (height) with braille points.
374#[derive(Clone, Copy)]
375pub struct BrailleOctPixel {
376    pixels: [bool; 8],
377}
378
379impl BrailleOctPixel {
380    #[rustfmt::skip]
381    const CHARS: [char; 256] = [
382        '⠀', '⠁', '⠈', '⠉', '⠂', '⠃', '⠊', '⠋', '⠐', '⠑', '⠘', '⠙', '⠒', '⠓', '⠚', '⠛',
383        '⠄', '⠅', '⠌', '⠍', '⠆', '⠇', '⠎', '⠏', '⠔', '⠕', '⠜', '⠝', '⠖', '⠗', '⠞', '⠟',
384        '⠠', '⠡', '⠨', '⠩', '⠢', '⠣', '⠪', '⠫', '⠰', '⠱', '⠸', '⠹', '⠲', '⠳', '⠺', '⠻',
385        '⠤', '⠥', '⠬', '⠭', '⠦', '⠧', '⠮', '⠯', '⠴', '⠵', '⠼', '⠽', '⠶', '⠷', '⠾', '⠿',
386        '⡀', '⡁', '⡈', '⡉', '⡂', '⡃', '⡊', '⡋', '⡐', '⡑', '⡘', '⡙', '⡒', '⡓', '⡚', '⡛',
387        '⡄', '⡅', '⡌', '⡍', '⡆', '⡇', '⡎', '⡏', '⡔', '⡕', '⡜', '⡝', '⡖', '⡗', '⡞', '⡟',
388        '⡠', '⡡', '⡨', '⡩', '⡢', '⡣', '⡪', '⡫', '⡰', '⡱', '⡸', '⡹', '⡲', '⡳', '⡺', '⡻',
389        '⡤', '⡥', '⡬', '⡭', '⡦', '⡧', '⡮', '⡯', '⡴', '⡵', '⡼', '⡽', '⡶', '⡷', '⡾', '⡿',
390        '⢀', '⢁', '⢈', '⢉', '⢂', '⢃', '⢊', '⢋', '⢐', '⢑', '⢘', '⢙', '⢒', '⢓', '⢚', '⢛',
391        '⢄', '⢅', '⢌', '⢍', '⢆', '⢇', '⢎', '⢏', '⢔', '⢕', '⢜', '⢝', '⢖', '⢗', '⢞', '⢟',
392        '⢠', '⢡', '⢨', '⢩', '⢢', '⢣', '⢪', '⢫', '⢰', '⢱', '⢸', '⢹', '⢲', '⢳', '⢺', '⢻',
393        '⢤', '⢥', '⢬', '⢭', '⢦', '⢧', '⢮', '⢯', '⢴', '⢵', '⢼', '⢽', '⢶', '⢷', '⢾', '⢿',
394        '⣀', '⣁', '⣈', '⣉', '⣂', '⣃', '⣊', '⣋', '⣐', '⣑', '⣘', '⣙', '⣒', '⣓', '⣚', '⣛',
395        '⣄', '⣅', '⣌', '⣍', '⣆', '⣇', '⣎', '⣏', '⣔', '⣕', '⣜', '⣝', '⣖', '⣗', '⣞', '⣟',
396        '⣠', '⣡', '⣨', '⣩', '⣢', '⣣', '⣪', '⣫', '⣰', '⣱', '⣸', '⣹', '⣲', '⣳', '⣺', '⣻',
397        '⣤', '⣥', '⣬', '⣭', '⣦', '⣧', '⣮', '⣯', '⣴', '⣵', '⣼', '⣽', '⣶', '⣷', '⣾', '⣿',
398    ];
399
400    #[rustfmt::skip]
401    const fn index(self) -> usize {
402        (self.pixels[0] as usize)      |
403        (self.pixels[1] as usize) << 1 |
404        (self.pixels[2] as usize) << 2 |
405        (self.pixels[3] as usize) << 3 |
406        (self.pixels[4] as usize) << 4 |
407        (self.pixels[5] as usize) << 5 |
408        (self.pixels[6] as usize) << 6 |
409        (self.pixels[7] as usize) << 7
410    }
411
412    /// # Examples
413    ///
414    /// ```
415    /// #![allow(incomplete_features)]
416    /// #![feature(generic_const_exprs)]
417    ///
418    /// use console_display::pixel::{
419    ///     Pixel,
420    ///     monochrome_pixel::BrailleOctPixel
421    /// };
422    ///
423    /// let pixel = BrailleOctPixel::new ([
424    ///     true, false, // #_
425    ///     false, true, // _#
426    ///     true, true,  // ##
427    ///     false, false // __
428    /// ]);
429    ///
430    /// let symbol = pixel.character();
431    ///
432    /// assert_eq!(symbol, '⠵')
433    /// ```
434    #[must_use]
435    pub const fn character(self) -> char {
436        Self::CHARS[self.index()]
437    }
438}
439
440impl Pixel for BrailleOctPixel {
441    type U = bool;
442
443    const WIDTH: usize = 2;
444
445    const HEIGHT: usize = 4;
446
447    impl_getters!(pixels: [bool; 8]);
448
449    impl_getters_mut!(pixels: [bool; 8]);
450
451    impl_new!(BrailleOctPixel, pixels: [bool; 8]);
452}
453
454impl_from_mono_chrome_pixel_for_datacell!(BrailleOctPixel);