embedded_menu/adapters/
color_map.rs

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
use embedded_graphics::{
    pixelcolor::BinaryColor,
    prelude::{Dimensions, DrawTarget, PixelColor},
    primitives::{ContainsPoint, Rectangle},
    Pixel,
};

pub struct ColorMappingOverlay<'a, T, S, C> {
    parent: &'a mut T,
    area: S,
    on_color: C,
    off_color: C,
}

impl<T, S, C> Dimensions for ColorMappingOverlay<'_, T, S, C>
where
    T: Dimensions,
{
    fn bounding_box(&self) -> Rectangle {
        self.parent.bounding_box()
    }
}

impl<T, S, C> DrawTarget for ColorMappingOverlay<'_, T, S, C>
where
    T: DrawTarget<Color = C>,
    S: ContainsPoint,
    C: PixelColor,
{
    type Color = BinaryColor;
    type Error = T::Error;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<BinaryColor>>,
    {
        self.parent
            .draw_iter(pixels.into_iter().map(|Pixel(pos, color)| {
                let color = if self.area.contains(pos) ^ (color == BinaryColor::On) {
                    self.on_color
                } else {
                    self.off_color
                };

                Pixel(pos, color)
            }))
    }
}

pub trait BinaryColorDrawTargetExt: Sized {
    fn map_colors<S, C>(&mut self, area: &S, on: C, off: C) -> ColorMappingOverlay<'_, Self, S, C>
    where
        S: Clone + ContainsPoint,
        C: PixelColor;
}

impl<T> BinaryColorDrawTargetExt for T
where
    T: DrawTarget,
{
    fn map_colors<S, C>(&mut self, area: &S, on: C, off: C) -> ColorMappingOverlay<'_, Self, S, C>
    where
        S: Clone + ContainsPoint,
        C: PixelColor,
    {
        ColorMappingOverlay {
            parent: self,
            area: area.clone(),
            on_color: on,
            off_color: off,
        }
    }
}