embedded_graphics/primitives/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
//! Graphics primitives
pub mod circle;
pub mod line;
pub mod rectangle;
mod thick_line_iterator;
pub mod triangle;
pub use self::{circle::Circle, line::Line, rectangle::Rectangle, triangle::Triangle};
use crate::{
geometry::Dimensions,
pixelcolor::PixelColor,
style::{PrimitiveStyle, Styled},
};
pub(crate) use thick_line_iterator::ThickLineIterator;
/// Primitive trait
pub trait Primitive: Dimensions {
/// Converts this primitive into a `Styled`.
fn into_styled<C>(self, style: PrimitiveStyle<C>) -> Styled<Self, PrimitiveStyle<C>>
where
C: PixelColor,
Self: Sized,
{
Styled::new(self, style)
}
}
/// Create a [`Circle`](./primitives/circle/struct.Circle.html) with optional styling using a
/// convenient macro.
///
/// ```rust
/// use embedded_graphics::{
/// egcircle,
/// pixelcolor::Rgb565,
/// prelude::*,
/// primitive_style,
/// primitives::Circle,
/// style::{PrimitiveStyle, Styled},
/// };
///
/// // Coordinates can be defined as any type that implements `Into<Point>`
/// let line_circle: Styled<Circle, PrimitiveStyle<Rgb565>> =
/// egcircle!(center = (10, 20), radius = 30);
///
/// let filled_circle: Styled<Circle, PrimitiveStyle<Rgb565>> = egcircle!(
/// center = (10, 20),
/// radius = 30,
/// 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 circles:
///
/// [`PrimitiveStyleBuilder`]: style/struct.PrimitiveStyleBuilder.html
///
/// ```rust
/// use embedded_graphics::{
/// egcircle,
/// pixelcolor::Rgb565,
/// prelude::*,
/// primitive_style,
/// primitives::Circle,
/// style::{PrimitiveStyle, PrimitiveStyleBuilder, Styled},
/// };
///
/// let circle_1: Styled<Circle, PrimitiveStyle<Rgb565>> = egcircle!(
/// center = (10, 20),
/// radius = 30,
/// 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 circle_2: Styled<Circle, PrimitiveStyle<Rgb565>> =
/// Circle::new(Point::new(10, 20), 30).into_styled(style);
///
/// assert_eq!(circle_1, circle_2);
/// ```
#[macro_export]
macro_rules! egcircle {
(center = $center:expr, radius = $r:expr $(,)?) => {{
$crate::egcircle!(
center = $center,
radius = $r,
style = $crate::style::PrimitiveStyle::default()
)
}};
(center = $center:expr, radius = $r:expr, style = $style:expr $(,)?) => {{
$crate::primitives::Circle::new($crate::geometry::Point::from($center), $r)
.into_styled($style)
}};
}
/// Create a [`Line`](./primitives/line/struct.Line.html) with optional styling using a
/// convenient macro.
///
/// Note that only the `stroke` property has any effect on lines currently.
///
/// ```rust
/// use embedded_graphics::{
/// egline,
/// pixelcolor::Rgb565,
/// prelude::*,
/// primitive_style,
/// primitives::Line,
/// style::{PrimitiveStyle, Styled},
/// };
///
/// let line: Styled<Line, PrimitiveStyle<Rgb565>> = egline!(start = (10, 20), end = (30, 40));
///
/// let stroke_line: Styled<Line, PrimitiveStyle<Rgb565>> = egline!(
/// start = (10, 20),
/// end = (30, 40),
/// style = primitive_style!(stroke_color = Rgb565::BLUE)
/// );
/// ```
///
/// Style properties like `stroke_color` map to methods on the [`PrimitiveStyleBuilder`] struct.
/// For example, the following code makes two identical lines:
///
/// [`PrimitiveStyleBuilder`]: style/struct.PrimitiveStyleBuilder.html
///
/// ```rust
/// use embedded_graphics::{
/// egline,
/// pixelcolor::Rgb565,
/// prelude::*,
/// primitive_style,
/// primitives::Line,
/// style::{PrimitiveStyle, PrimitiveStyleBuilder, Styled},
/// };
///
/// let line_1: Styled<Line, PrimitiveStyle<Rgb565>> = egline!(
/// start = (10, 20),
/// end = (30, 40),
/// style = primitive_style!(
/// stroke_color = Rgb565::BLUE,
/// fill_color = Rgb565::YELLOW,
/// stroke_width = 1
/// )
/// );
///
/// let style = PrimitiveStyleBuilder::new()
/// .fill_color(Rgb565::YELLOW)
/// .stroke_color(Rgb565::BLUE)
/// .stroke_width(1)
/// .build();
///
/// let line_2: Styled<Line, PrimitiveStyle<Rgb565>> =
/// Line::new(Point::new(10, 20), Point::new(30, 40)).into_styled(style);
///
/// assert_eq!(line_1, line_2);
/// ```
#[macro_export]
macro_rules! egline {
(start = $start:expr, end = $end:expr $(,)?) => {{
$crate::egline!(
start = $start,
end = $end,
style = $crate::style::PrimitiveStyle::default()
)
}};
(start = $start:expr, end = $end:expr, style = $style:expr $(,)?) => {{
$crate::primitives::Line::new(
$crate::geometry::Point::from($start),
$crate::geometry::Point::from($end),
)
.into_styled($style)
}};
}
/// Create a [`Rectangle`](./primitives/rectangle/struct.Rectangle.html) with optional styling using a
/// convenient macro.
///
/// ```rust
/// 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:
///
/// [`PrimitiveStyleBuilder`]: style/struct.PrimitiveStyleBuilder.html
///
/// ```rust
/// 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);
/// ```
#[macro_export]
macro_rules! egrectangle {
(top_left = $top_left:expr, bottom_right = $bottom_right:expr $(,)?) => {{
$crate::egrectangle!(
top_left = $top_left,
bottom_right = $bottom_right,
style = $crate::style::PrimitiveStyle::default()
)
}};
(top_left = $top_left:expr, bottom_right = $bottom_right:expr, style = $style:expr $(,)?) => {{
$crate::primitives::Rectangle::new(
$crate::geometry::Point::from($top_left),
$crate::geometry::Point::from($bottom_right),
)
.into_styled($style)
}};
}
/// Create a [`Triangle`](./primitives/triangle/struct.Triangle.html) with optional styling using a
/// convenient macro.
///
/// ```rust
/// 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:
///
/// [`PrimitiveStyleBuilder`]: style/struct.PrimitiveStyleBuilder.html
///
/// ```rust
/// 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);
/// ```
#[macro_export]
macro_rules! egtriangle {
(points = $points:expr $(,)?) => {{
$crate::egtriangle!(
points = $points,
style = $crate::style::PrimitiveStyle::default()
)
}};
(points = $points:expr, style = $style:expr $(,)?) => {{
$crate::primitives::Triangle::from_points($points).into_styled($style)
}};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
geometry::Point,
pixelcolor::{Rgb565, RgbColor},
primitive_style,
style::PrimitiveStyle,
};
#[test]
fn circle() {
let _c: Styled<Circle, PrimitiveStyle<Rgb565>> =
egcircle!(center = Point::new(10, 20), radius = 30);
let _c: Styled<Circle, PrimitiveStyle<Rgb565>> = egcircle!(center = (10, 20), radius = 30);
let _c: Styled<Circle, PrimitiveStyle<Rgb565>> = egcircle!(
center = (10, 20),
radius = 30,
style = primitive_style!(stroke_color = Rgb565::RED, fill_color = Rgb565::GREEN),
);
}
#[test]
fn line() {
let _l: Styled<Line, PrimitiveStyle<Rgb565>> =
egline!(start = Point::new(10, 20), end = Point::new(30, 40),);
let _l: Styled<Line, PrimitiveStyle<Rgb565>> = egline!(start = (10, 20), end = (30, 40));
let _l: Styled<Line, PrimitiveStyle<Rgb565>> = egline!(
start = (10, 20),
end = (30, 40),
style = primitive_style!(stroke_color = Rgb565::RED, fill_color = Rgb565::GREEN),
);
}
#[test]
fn rectangle() {
let _r: Styled<Rectangle, PrimitiveStyle<Rgb565>> = egrectangle!(
top_left = Point::new(10, 20),
bottom_right = Point::new(30, 40),
);
let _r: Styled<Rectangle, PrimitiveStyle<Rgb565>> =
egrectangle!(top_left = (10, 20), bottom_right = (30, 40),);
let _r: Styled<Rectangle, PrimitiveStyle<Rgb565>> = egrectangle!(
top_left = (10, 20),
bottom_right = (30, 40),
style = primitive_style!(stroke_color = Rgb565::RED, fill_color = Rgb565::GREEN)
);
}
#[test]
fn triangle() {
let _t: Styled<Triangle, PrimitiveStyle<Rgb565>> =
egtriangle!(points = [Point::new(10, 20), Point::new(30, 40), Point::new(50, 60)],);
let _t: Styled<Triangle, PrimitiveStyle<Rgb565>> =
egtriangle!(points = [(10, 20), (30, 40), (50, 60)]);
let _t: Styled<Triangle, PrimitiveStyle<Rgb565>> = egtriangle!(
points = [(10, 20), (30, 40), (50, 60)],
style = primitive_style!(stroke_color = Rgb565::RED, fill_color = Rgb565::GREEN)
);
}
}