pub struct Components;Expand description
A struct providing convenience methods for creating different types of components.
The Components struct serves as a utility for easily generating instances of various graphical
components in a 2D space. It offers methods for creating circles, rectangles, lines, and text
components with specified attributes.
§Examples
use omage::{Components, Rgba};
// Create a new circle component
let circle = Components::Circle(50, 50, 30, Rgba([255, 0, 0, 255]));
// Create a new rectangle component
let rectangle = Components::Rectangle(40, 60, 10, 20, Rgba([0, 255, 0, 255]));
// Create a new line component
let line = Components::Line(10, 10, 80, 80, Rgba([0, 0, 255, 255]));
// Create a new text component
let text = Components::Text(30, 40, 16, "Hello, Rust!", Rgba([255, 255, 255, 255]), Some((Rgba([0, 0, 0, 255]), 2)));§Methods
The Components struct provides the following methods:
Circle: Creates a new circle component with specified attributes.Rectangle: Creates a new rectangle component with specified attributes.Line: Creates a new line component with specified attributes.Text: Creates a new text component with specified attributes, including an optional border.
§Note
The Components struct is designed to simplify the process of creating graphical components
for use in a 2D drawing context. It encapsulates the logic for constructing different types
of components with ease.
Implementations§
Source§impl Components
impl Components
Sourcepub fn Circle(cx: u32, cy: u32, r: u32, color: Rgba<u8>) -> Component
pub fn Circle(cx: u32, cy: u32, r: u32, color: Rgba<u8>) -> Component
Creates a new circle component.
§Parameters
cx: X-coordinate of the center of the circle.cy: Y-coordinate of the center of the circle.r: Radius of the circle.color: RGBA color of the circle.
§Returns
A Component::Circle instance.
Examples found in repository?
More examples
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10 let mut image = Image::new();
11
12 let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13 let circle2 = Components::Circle(
14 config.width / 2,
15 config.height / 2,
16 300,
17 Rgba([255, 0, 255, 120]),
18 );
19 let rectangle = Components::Rectangle(
20 100,
21 100,
22 config.width / 2 - 50,
23 config.height / 2 - 50,
24 Rgba([120, 0, 255, 19]),
25 );
26
27 image
28 .config(config)
29 .init()?
30 .add_components(vec![&circle1, &circle2, &rectangle])
31 .draw()?;
32 Ok(())
33}7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(
9 WIDTH,
10 HEIGHT,
11 BLACK,
12 Some(GREEN),
13 "output.png",
14 Some("./fonts/Roboto-Medium.ttf"),
15 );
16
17 let mut image = Image::new();
18
19 let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20 let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21 let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22 let text = Components::Text(
23 WIDTH / 2 - 210,
24 HEIGHT / 2 - 250,
25 40,
26 "Xiaolin Wu's Line Algorithm",
27 BLACK,
28 Some((GREEN, 3)),
29 );
30
31 image
32 .config(config)
33 .init()?
34 .add_components(vec![&line1, &line2, &circle, &text])
35 .draw()?;
36 Ok(())
37}7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(
9 WIDTH,
10 HEIGHT,
11 Rgba([255, 255, 255, 0]),
12 Some(WHITE),
13 "output.png",
14 Some("./fonts/Roboto-Medium.ttf"),
15 );
16
17 let mut image = Image::new();
18
19 let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20 let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21 let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23 let text = "OMAGE";
24 let text = Components::Text(
25 config.width / 2 - 40,
26 config.height / 2 - 25,
27 50,
28 text,
29 Rgba([255, 255, 255, 255]),
30 Some((BLACK, 3)),
31 );
32
33 image
34 .config(config)
35 .init()?
36 .add_components(vec![&text, &circle1, &circle2, &circle3])
37 .draw()?;
38 Ok(())
39}Sourcepub fn Rectangle(h: u32, w: u32, x: u32, y: u32, color: Rgba<u8>) -> Component
pub fn Rectangle(h: u32, w: u32, x: u32, y: u32, color: Rgba<u8>) -> Component
Creates a new rectangle component.
§Parameters
h: Height of the rectangle.w: Width of the rectangle.x: X-coordinate of the top-left corner of the rectangle.y: Y-coordinate of the top-left corner of the rectangle.color: RGBA color of the rectangle.
§Returns
A Component::Rectangle instance.
Examples found in repository?
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);
9
10 let mut image = Image::new();
11
12 let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
13 let circle2 = Components::Circle(
14 config.width / 2,
15 config.height / 2,
16 300,
17 Rgba([255, 0, 255, 120]),
18 );
19 let rectangle = Components::Rectangle(
20 100,
21 100,
22 config.width / 2 - 50,
23 config.height / 2 - 50,
24 Rgba([120, 0, 255, 19]),
25 );
26
27 image
28 .config(config)
29 .init()?
30 .add_components(vec![&circle1, &circle2, &rectangle])
31 .draw()?;
32 Ok(())
33}Sourcepub fn Line(x1: u32, y1: u32, x2: u32, y2: u32, color: Rgba<u8>) -> Component
pub fn Line(x1: u32, y1: u32, x2: u32, y2: u32, color: Rgba<u8>) -> Component
Creates a new line component.
§Parameters
x1: X-coordinate of the starting point of the line.y1: Y-coordinate of the starting point of the line.x2: X-coordinate of the ending point of the line.y2: Y-coordinate of the ending point of the line.color: RGBA color of the line.
§Returns
A Component::Line instance.
Examples found in repository?
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(
9 WIDTH,
10 HEIGHT,
11 BLACK,
12 Some(GREEN),
13 "output.png",
14 Some("./fonts/Roboto-Medium.ttf"),
15 );
16
17 let mut image = Image::new();
18
19 let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20 let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21 let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22 let text = Components::Text(
23 WIDTH / 2 - 210,
24 HEIGHT / 2 - 250,
25 40,
26 "Xiaolin Wu's Line Algorithm",
27 BLACK,
28 Some((GREEN, 3)),
29 );
30
31 image
32 .config(config)
33 .init()?
34 .add_components(vec![&line1, &line2, &circle, &text])
35 .draw()?;
36 Ok(())
37}Sourcepub fn Text(
x: u32,
y: u32,
size: u32,
text: &'static str,
color: Rgba<u8>,
border: Option<(Rgba<u8>, u32)>,
) -> Component
pub fn Text( x: u32, y: u32, size: u32, text: &'static str, color: Rgba<u8>, border: Option<(Rgba<u8>, u32)>, ) -> Component
Creates a new text component.
§Parameters
x: X-coordinate of the text.y: Y-coordinate of the text.size: Font size of the text.text: The actual text content.color: RGBA color of the text.border: Optional border color and thickness as a tuple.
§Returns
A Component::Text instance.
Examples found in repository?
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(
9 WIDTH,
10 HEIGHT,
11 BLACK,
12 Some(GREEN),
13 "output.png",
14 Some("./fonts/Roboto-Medium.ttf"),
15 );
16
17 let mut image = Image::new();
18
19 let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
20 let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
21 let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
22 let text = Components::Text(
23 WIDTH / 2 - 210,
24 HEIGHT / 2 - 250,
25 40,
26 "Xiaolin Wu's Line Algorithm",
27 BLACK,
28 Some((GREEN, 3)),
29 );
30
31 image
32 .config(config)
33 .init()?
34 .add_components(vec![&line1, &line2, &circle, &text])
35 .draw()?;
36 Ok(())
37}More examples
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let config = Config::new(
9 WIDTH,
10 HEIGHT,
11 Rgba([255, 255, 255, 0]),
12 Some(WHITE),
13 "output.png",
14 Some("./fonts/Roboto-Medium.ttf"),
15 );
16
17 let mut image = Image::new();
18
19 let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
20 let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
21 let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));
22
23 let text = "OMAGE";
24 let text = Components::Text(
25 config.width / 2 - 40,
26 config.height / 2 - 25,
27 50,
28 text,
29 Rgba([255, 255, 255, 255]),
30 Some((BLACK, 3)),
31 );
32
33 image
34 .config(config)
35 .init()?
36 .add_components(vec![&text, &circle1, &circle2, &circle3])
37 .draw()?;
38 Ok(())
39}