embedded_graphics

Macro egtriangle

Source
macro_rules! egtriangle {
    (points = $points:expr $(,)?) => { ... };
    (points = $points:expr, style = $style:expr $(,)?) => { ... };
}
Expand description

Create a Triangle with optional styling using a convenient macro.

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

let empty_triangle: Styled<Triangle, PrimitiveStyle<Rgb565>> =
    egtriangle!(points = [(10, 20), (30, 40), (50, 60)]);

let filled_triangle: Styled<Triangle, PrimitiveStyle<Rgb565>> = egtriangle!(
    points = [(10, 20), (30, 40), (50, 60)],
    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 triangles:

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

let triangle_1: Styled<Triangle, PrimitiveStyle<Rgb565>> = egtriangle!(
    points = [(10, 20), (30, 40), (50, 60)],
    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 triangle_2: Styled<Triangle, PrimitiveStyle<Rgb565>> =
    Triangle::new(Point::new(10, 20), Point::new(30, 40), Point::new(50, 60))
        .into_styled(style);

assert_eq!(triangle_1, triangle_2);