| 1 | use core::ops::Range; |
| 2 | |
| 3 | use crate::{draw_target::DrawTarget, primitives::common::Scanline}; |
| 4 | |
| 5 | /// Scanline with stroke and fill regions. |
| 6 | #[derive (Clone, Eq, PartialEq, Hash, Debug)] |
| 7 | #[cfg_attr (feature = "defmt" , derive(::defmt::Format))] |
| 8 | pub struct StyledScanline { |
| 9 | y: i32, |
| 10 | stroke_range: Range<i32>, |
| 11 | fill_range: Range<i32>, |
| 12 | } |
| 13 | |
| 14 | impl StyledScanline { |
| 15 | /// Creates a new styled scanline. |
| 16 | pub fn new(y: i32, stroke_range: Range<i32>, fill_range: Option<Range<i32>>) -> Self { |
| 17 | let fill_range = fill_range.unwrap_or_else(|| stroke_range.end..stroke_range.end); |
| 18 | |
| 19 | Self { |
| 20 | y, |
| 21 | stroke_range, |
| 22 | fill_range, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Returns the stroke region on the left side. |
| 27 | /// |
| 28 | /// If the scanline contains no fill region the entire scanline will be returned. |
| 29 | pub const fn stroke_left(&self) -> Scanline { |
| 30 | Scanline::new(self.y, self.stroke_range.start..self.fill_range.start) |
| 31 | } |
| 32 | |
| 33 | /// Returns the stroke region on the right side. |
| 34 | /// |
| 35 | /// If the scanline contains no fill region an empty scanline will be returned. |
| 36 | pub const fn stroke_right(&self) -> Scanline { |
| 37 | Scanline::new(self.y, self.fill_range.end..self.stroke_range.end) |
| 38 | } |
| 39 | |
| 40 | /// Returns the fill region. |
| 41 | pub fn fill(&self) -> Scanline { |
| 42 | Scanline::new(self.y, self.fill_range.clone()) |
| 43 | } |
| 44 | |
| 45 | /// Draws the stroke regions. |
| 46 | pub fn draw_stroke<T: DrawTarget>( |
| 47 | &self, |
| 48 | target: &mut T, |
| 49 | stroke_color: T::Color, |
| 50 | ) -> Result<(), T::Error> { |
| 51 | self.stroke_left().draw(target, stroke_color)?; |
| 52 | self.stroke_right().draw(target, stroke_color) |
| 53 | } |
| 54 | |
| 55 | /// Draws the stroke and fill regions. |
| 56 | pub fn draw_stroke_and_fill<T: DrawTarget>( |
| 57 | &self, |
| 58 | target: &mut T, |
| 59 | stroke_color: T::Color, |
| 60 | fill_color: T::Color, |
| 61 | ) -> Result<(), T::Error> { |
| 62 | self.stroke_left().draw(target, stroke_color)?; |
| 63 | self.fill().draw(target, fill_color)?; |
| 64 | self.stroke_right().draw(target, stroke_color) |
| 65 | } |
| 66 | } |
| 67 | |