33//! EPD representation of multicolor with separate buffers
44//! for each bit makes it hard to properly represent colors here
55
6+ use crate :: prelude:: DisplayMode ;
67#[ cfg( feature = "graphics" ) ]
78use embedded_graphics_core:: pixelcolor:: BinaryColor ;
89#[ cfg( feature = "graphics" ) ]
@@ -77,21 +78,21 @@ pub trait ColorType: PixelColor {
7778
7879 /// Return the data used to set a pixel color
7980 ///
80- /// * bwrbit is used to tell the value of the unused bit when a chromatic
81+ /// * MODE is used to tell the value of the unused bit when a chromatic
8182 /// color is set (TriColor only as for now)
8283 /// * pos is the pixel position in the line, used to know which pixels must be set
8384 ///
8485 /// Return values are :
8586 /// * .0 is the mask used to exclude this pixel from the byte (eg: 0x7F in BiColor)
8687 /// * .1 are the bits used to set the color in the byte (eg: 0x80 in BiColor)
8788 /// this is u16 because we set 2 bytes in case of split buffer
88- fn bitmask ( & self , bwrbit : bool , pos : u32 ) -> ( u8 , u16 ) ;
89+ fn bitmask ( & self , mode : DisplayMode , pos : u32 ) -> ( u8 , u16 ) ;
8990}
9091
9192impl ColorType for Color {
9293 const BITS_PER_PIXEL_PER_BUFFER : usize = 1 ;
9394 const BUFFER_COUNT : usize = 1 ;
94- fn bitmask ( & self , _bwrbit : bool , pos : u32 ) -> ( u8 , u16 ) {
95+ fn bitmask ( & self , _mode : DisplayMode , pos : u32 ) -> ( u8 , u16 ) {
9596 let bit = 0x80 >> ( pos % 8 ) ;
9697 match self {
9798 Color :: Black => ( !bit, 0u16 ) ,
@@ -103,27 +104,34 @@ impl ColorType for Color {
103104impl ColorType for TriColor {
104105 const BITS_PER_PIXEL_PER_BUFFER : usize = 1 ;
105106 const BUFFER_COUNT : usize = 2 ;
106- fn bitmask ( & self , bwrbit : bool , pos : u32 ) -> ( u8 , u16 ) {
107+
108+ fn bitmask ( & self , mode : DisplayMode , pos : u32 ) -> ( u8 , u16 ) {
107109 let bit = 0x80 >> ( pos % 8 ) ;
108- match self {
109- TriColor :: Black => ( !bit, 0u16 ) ,
110- TriColor :: White => ( !bit, bit as u16 ) ,
111- TriColor :: Chromatic => (
112- !bit,
113- if bwrbit {
114- ( bit as u16 ) << 8
115- } else {
116- ( bit as u16 ) << 8 | bit as u16
117- } ,
118- ) ,
119- }
110+ let mask = match mode {
111+ DisplayMode :: BwrBitOnColorInverted => match self {
112+ TriColor :: Black => ( bit as u16 ) << 8 ,
113+ TriColor :: White => ( bit as u16 ) << 8 | bit as u16 ,
114+ TriColor :: Chromatic => 0u16 ,
115+ } ,
116+ DisplayMode :: BwrBitOn => match self {
117+ TriColor :: Black => 0u16 ,
118+ TriColor :: White => bit as u16 ,
119+ TriColor :: Chromatic => ( bit as u16 ) << 8 ,
120+ } ,
121+ DisplayMode :: BwrBitOff => match self {
122+ TriColor :: Black => 0u16 ,
123+ TriColor :: White => bit as u16 ,
124+ TriColor :: Chromatic => ( bit as u16 ) << 8 | bit as u16 ,
125+ } ,
126+ } ;
127+ ( !bit, mask)
120128 }
121129}
122130
123131impl ColorType for OctColor {
124132 const BITS_PER_PIXEL_PER_BUFFER : usize = 4 ;
125133 const BUFFER_COUNT : usize = 1 ;
126- fn bitmask ( & self , _bwrbit : bool , pos : u32 ) -> ( u8 , u16 ) {
134+ fn bitmask ( & self , _mode : DisplayMode , pos : u32 ) -> ( u8 , u16 ) {
127135 let mask = !( 0xF0 >> ( pos % 2 ) ) ;
128136 let bits = self . get_nibble ( ) as u16 ;
129137 ( mask, if pos % 2 == 1 { bits } else { bits << 4 } )
@@ -307,16 +315,16 @@ impl From<BinaryColor> for Color {
307315impl From < embedded_graphics_core:: pixelcolor:: Rgb888 > for Color {
308316 fn from ( rgb : embedded_graphics_core:: pixelcolor:: Rgb888 ) -> Self {
309317 use embedded_graphics_core:: pixelcolor:: RgbColor ;
310- if rgb == RgbColor :: BLACK {
311- Color :: Black
312- } else if rgb == RgbColor :: WHITE {
313- Color :: White
314- } else {
315- // choose closest color
316- if ( rgb . r ( ) as u16 + rgb . g ( ) as u16 + rgb . b ( ) as u16 ) > 255 * 3 / 2 {
317- Color :: White
318- } else {
319- Color :: Black
318+ match rgb {
319+ RgbColor :: BLACK => Color :: Black ,
320+ RgbColor :: WHITE => Color :: White ,
321+ _ => {
322+ // choose closest color
323+ if ( rgb . r ( ) as u16 + rgb . g ( ) as u16 + rgb . b ( ) as u16 ) > 255 * 3 / 2 {
324+ Color :: White
325+ } else {
326+ Color :: Black
327+ }
320328 }
321329 }
322330 }
@@ -369,13 +377,11 @@ impl From<BinaryColor> for TriColor {
369377impl From < embedded_graphics_core:: pixelcolor:: Rgb888 > for TriColor {
370378 fn from ( rgb : embedded_graphics_core:: pixelcolor:: Rgb888 ) -> Self {
371379 use embedded_graphics_core:: pixelcolor:: RgbColor ;
372- if rgb == RgbColor :: BLACK {
373- TriColor :: Black
374- } else if rgb == RgbColor :: WHITE {
375- TriColor :: White
376- } else {
380+ match rgb {
381+ RgbColor :: BLACK => TriColor :: Black ,
382+ RgbColor :: WHITE => TriColor :: White ,
377383 // there is no good approximation here since we don't know which color is 'chromatic'
378- TriColor :: Chromatic
384+ _ => TriColor :: Chromatic ,
379385 }
380386 }
381387}
0 commit comments