micro_gui/types/
pixel.rs

1//! Pixel type definitions
2//! This is designed so that modules that do not need to interact with pixels directly
3//! can use pixel::Pixel or standard pixel traits and be compatible with all pixel representations.
4//!
5//! Copyright 2017 Ryan Kurte
6
7/// Black and White trait to be implemented by all colours
8pub trait BW {
9    fn black() -> Self;
10    fn white() -> Self;
11}
12
13/// RGB trait to be implemented by all colours
14pub trait RGB {
15    fn red()   -> Self;
16    fn green() -> Self;
17    fn blue()  -> Self;
18}
19
20/// 24-bit RGB pixel implementation
21#[derive(Clone, Copy, Default, Debug, PartialEq)]
22pub struct PixelRGB24 {
23    pub r: u8,
24    pub g: u8,
25    pub b: u8
26}
27
28impl PixelRGB24 {
29    pub fn new(r: u8, g: u8, b: u8) -> Self {
30        return Self{r, g, b}
31    }
32
33    pub fn from_hex(hex: u32) -> Self {
34        return Self{
35            r: ((hex >> 16) & 0xFF) as u8,
36            g: ((hex >> 8) & 0xFF) as u8,
37            b: ((hex >> 0) & 0xFF) as u8,
38        }
39    }
40
41    pub fn nice_red() -> Self { Self::from_hex(0xB2000E) }
42    pub fn nice_blue() -> Self { Self::from_hex(0x0009B2) }
43    pub fn nice_green() -> Self { Self::from_hex(0x00B22B) }
44    pub fn nice_yellow() -> Self { Self::from_hex(0xFFD119) }
45}
46
47impl BW for PixelRGB24 {
48    fn black() -> Self { Self{r: 0x00, g: 0x00, b: 0x00} }
49    fn white() -> Self { Self{r: 0xff, g: 0xff, b: 0xff} }
50}
51
52impl RGB for PixelRGB24 {
53    fn red()   -> Self { Self::nice_red() }
54    fn green() -> Self { Self::nice_green() }
55    fn blue()  -> Self { Self::nice_blue() }
56}
57
58
59/// 8-bit grey-scale pixel implementation
60#[derive(Clone, Copy, Default, Debug, PartialEq)]
61pub struct PixelG8 (u8);
62
63impl BW for PixelG8 {
64    fn black() -> Self { Self(0xff) }
65    fn white() -> Self { Self(0x00) }
66}
67
68impl RGB for PixelG8 {
69    fn red()   -> Self { Self(0b0010_0000) }
70    fn green() -> Self { Self(0b0100_0000) }
71    fn blue()  -> Self { Self(0b1000_0000) }
72}
73
74
75/// 1-bit wlack and white pixel implementation
76pub type PixelBW = bool;
77
78impl BW for PixelBW {
79    fn black() -> Self { true }
80    fn white() -> Self { false }
81}
82
83impl RGB for PixelBW {
84    fn red()   -> Self { true }
85    fn green() -> Self { true }
86    fn blue()  -> Self { true }
87}
88