embedded_graphics

Macro egrectangle

Source
macro_rules! egrectangle {
    (top_left = $top_left:expr, bottom_right = $bottom_right:expr $(,)?) => { ... };
    (top_left = $top_left:expr, bottom_right = $bottom_right:expr, style = $style:expr $(,)?) => { ... };
}
Expand description

Create a Rectangle with optional styling using a convenient macro.

use embedded_graphics::{
    egrectangle,
    pixelcolor::Rgb565,
    prelude::*,
    primitive_style,
    primitives::Rectangle,
    style::{PrimitiveStyle, Styled},
};

let empty_rect: Styled<Rectangle, PrimitiveStyle<Rgb565>> =
    egrectangle!(top_left = (10, 20), bottom_right = (30, 40));

let filled_rect: Styled<Rectangle, PrimitiveStyle<Rgb565>> = egrectangle!(
    top_left = (10, 20),
    bottom_right = (30, 40),
    style = primitive_style!(stroke_color = Rgb565::RED, fill_color = Rgb565::GREEN)
);

Style properties like stroke_color map to methods on the PrimitiveStyleBuilder struct. For example, the following code makes two identical rectangles:

use embedded_graphics::{
    egrectangle,
    pixelcolor::Rgb565,
    prelude::*,
    primitive_style,
    primitives::Rectangle,
    style::{PrimitiveStyle, PrimitiveStyleBuilder, Styled},
};

let rectangle_1: Styled<Rectangle, PrimitiveStyle<Rgb565>> = egrectangle!(
    top_left = (10, 20),
    bottom_right = (30, 40),
    style = primitive_style!(
        stroke_color = Rgb565::RED,
        fill_color = Rgb565::GREEN,
        stroke_width = 1
    )
);

let style = PrimitiveStyleBuilder::new()
    .fill_color(Rgb565::GREEN)
    .stroke_color(Rgb565::RED)
    .stroke_width(1)
    .build();

let rectangle_2: Styled<Rectangle, PrimitiveStyle<Rgb565>> =
    Rectangle::new(Point::new(10, 20), Point::new(30, 40)).into_styled(style);

assert_eq!(rectangle_1, rectangle_2);