1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#[cfg(feature = "embeddedgraphics")]
use embedded_graphics::pixelcolor::{
    raw::RawU24, Bgr555, Bgr565, Bgr888, BinaryColor, Gray2, Gray4, Gray8, GrayColor, PixelColor,
    Rgb555, Rgb565, Rgb888, RgbColor,
};

/// The Rust handle representing a color you'd like to display.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LedColor {
    /// the red channel value
    pub red: u8,
    /// the green channel value
    pub green: u8,
    /// the blue channel value
    pub blue: u8,
}

#[cfg(feature = "embeddedgraphics")]
impl PixelColor for LedColor {
    type Raw = RawU24;
}

#[cfg(feature = "embeddedgraphics")]
impl From<Bgr555> for LedColor {
    fn from(p: Bgr555) -> Self {
        LedColor {
            red: p.r() << 3,
            green: p.g() << 3,
            blue: p.b() << 3,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Bgr565> for LedColor {
    fn from(p: Bgr565) -> Self {
        LedColor {
            red: p.r() << 3,
            green: p.g() << 2,
            blue: p.b() << 3,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Bgr888> for LedColor {
    fn from(p: Bgr888) -> Self {
        LedColor {
            red: p.r(),
            green: p.g(),
            blue: p.b(),
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Gray2> for LedColor {
    fn from(p: Gray2) -> Self {
        LedColor {
            red: p.luma() << 6,
            green: p.luma() << 6,
            blue: p.luma() << 6,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Gray4> for LedColor {
    fn from(p: Gray4) -> Self {
        LedColor {
            red: p.luma() << 4,
            green: p.luma() << 4,
            blue: p.luma() << 4,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Gray8> for LedColor {
    fn from(p: Gray8) -> Self {
        LedColor {
            red: p.luma(),
            green: p.luma(),
            blue: p.luma(),
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Rgb555> for LedColor {
    fn from(p: Rgb555) -> Self {
        LedColor {
            red: p.r() << 3,
            green: p.g() << 3,
            blue: p.b() << 3,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Rgb565> for LedColor {
    fn from(p: Rgb565) -> Self {
        LedColor {
            red: p.r() << 3,
            green: p.g() << 2,
            blue: p.b() << 3,
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<Rgb888> for LedColor {
    fn from(p: Rgb888) -> Self {
        LedColor {
            red: p.r(),
            green: p.g(),
            blue: p.b(),
        }
    }
}

#[cfg(feature = "embeddedgraphics")]
impl From<BinaryColor> for LedColor {
    fn from(p: BinaryColor) -> Self {
        let value = if p == BinaryColor::On {
            std::u8::MAX
        } else {
            0
        };
        Self {
            red: value,
            green: value,
            blue: value,
        }
    }
}