embedded_graphics/draw_target/
color_converted.rs

1use crate::{
2    draw_target::DrawTarget, geometry::Dimensions, pixelcolor::PixelColor, primitives::Rectangle,
3    Pixel,
4};
5use core::marker::PhantomData;
6
7/// Color conversion draw target.
8///
9/// Created by calling [`color_converted`] on any [`DrawTarget`].
10/// See the [`color_converted`] method documentation for more information.
11///
12/// [`color_converted`]: crate::draw_target::DrawTargetExt::color_converted
13#[derive(Debug)]
14pub struct ColorConverted<'a, T, C> {
15    /// The parent draw target.
16    parent: &'a mut T,
17
18    /// The input color type.
19    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}