pub mod contiguous;
pub mod pixel;
use crate::{
draw_target::DrawTarget, geometry::Point, pixelcolor::PixelColor, primitives::Rectangle, Pixel,
};
pub trait IntoPixels {
type Color: PixelColor;
type Iter: Iterator<Item = Pixel<Self::Color>>;
fn into_pixels(self) -> Self::Iter;
}
pub trait ContiguousIteratorExt
where
Self: Iterator + Sized,
<Self as Iterator>::Item: PixelColor,
{
fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self>;
}
impl<I> ContiguousIteratorExt for I
where
I: Iterator,
I::Item: PixelColor,
{
fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self> {
contiguous::IntoPixels::new(self, *bounding_box)
}
}
pub trait PixelIteratorExt<C>
where
Self: Sized,
C: PixelColor,
{
fn draw<D>(self, target: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = C>;
fn translate(self, offset: Point) -> pixel::Translate<Self>;
}
impl<I, C> PixelIteratorExt<C> for I
where
C: PixelColor,
I: Iterator<Item = Pixel<C>>,
{
fn draw<D>(self, target: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = C>,
{
target.draw_iter(self)
}
fn translate(self, offset: Point) -> pixel::Translate<Self> {
pixel::Translate::new(self, offset)
}
}
#[cfg(test)]
mod tests {
use embedded_graphics::{
geometry::Point, iterator::PixelIteratorExt, mock_display::MockDisplay,
pixelcolor::BinaryColor, Pixel,
};
#[test]
fn draw_pixel_iterator() {
let pixels = [
Pixel(Point::new(0, 0), BinaryColor::On),
Pixel(Point::new(1, 0), BinaryColor::Off),
Pixel(Point::new(2, 0), BinaryColor::On),
Pixel(Point::new(2, 1), BinaryColor::Off),
];
let mut display = MockDisplay::new();
pixels.iter().copied().draw(&mut display).unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
"#.#", " .", ])
);
}
}