embedded_graphics/draw_target/
color_converted.rs1use crate::{
2 draw_target::DrawTarget, geometry::Dimensions, pixelcolor::PixelColor, primitives::Rectangle,
3 Pixel,
4};
5use core::marker::PhantomData;
6
7#[derive(Debug)]
14pub struct ColorConverted<'a, T, C> {
15 parent: &'a mut T,
17
18 color_type: PhantomData<C>,
20}
21
22impl<'a, T, C> ColorConverted<'a, T, C>
23where
24 T: DrawTarget,
25 C: PixelColor + Into<T::Color>,
26{
27 pub(super) fn new(parent: &'a mut T) -> Self {
28 Self {
29 parent,
30 color_type: PhantomData,
31 }
32 }
33}
34
35impl<T, C> DrawTarget for ColorConverted<'_, T, C>
36where
37 T: DrawTarget,
38 C: PixelColor + Into<T::Color>,
39{
40 type Color = C;
41 type Error = T::Error;
42
43 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
44 where
45 I: IntoIterator<Item = Pixel<Self::Color>>,
46 {
47 self.parent
48 .draw_iter(pixels.into_iter().map(|Pixel(p, c)| Pixel(p, c.into())))
49 }
50
51 fn fill_contiguous<I>(&mut self, area: &Rectangle, colors: I) -> Result<(), Self::Error>
52 where
53 I: IntoIterator<Item = Self::Color>,
54 {
55 self.parent
56 .fill_contiguous(area, colors.into_iter().map(|c| c.into()))
57 }
58
59 fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
60 self.parent.fill_solid(area, color.into())
61 }
62
63 fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
64 self.parent.clear(color.into())
65 }
66}
67
68impl<T, C> Dimensions for ColorConverted<'_, T, C>
69where
70 T: DrawTarget,
71{
72 fn bounding_box(&self) -> Rectangle {
73 self.parent.bounding_box()
74 }
75}