Struct omage::Components

source ·
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

source

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?
examples/anti.rs (line 12)
7
8
9
10
11
12
13
14
15
16
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(WIDTH, HEIGHT, WHITE, None, "output.png", None);

    let mut image = Image::new();

    let circle = Components::Circle(config.width / 2, config.height / 2, 10, RED);

    image.config(config).init()?.add_component(&circle).draw()?;
    Ok(())
}
More examples
Hide additional examples
examples/circle.rs (line 12)
7
8
9
10
11
12
13
14
15
16
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);

    let mut image = Image::new();

    let circle = Components::Circle(config.width / 2, config.height / 2, 300, RED);

    image.config(config).init()?.add_component(&circle).draw()?;
    Ok(())
}
examples/blend.rs (line 12)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);

    let mut image = Image::new();

    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
    let circle2 = Components::Circle(
        config.width / 2,
        config.height / 2,
        300,
        Rgba([255, 0, 255, 120]),
    );
    let rectangle = Components::Rectangle(
        100,
        100,
        config.width / 2 - 50,
        config.height / 2 - 50,
        Rgba([120, 0, 255, 19]),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&circle1, &circle2, &rectangle])
        .draw()?;
    Ok(())
}
examples/line.rs (line 21)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(
        WIDTH,
        HEIGHT,
        BLACK,
        Some(GREEN),
        "output.png",
        Some("./fonts/Roboto-Medium.ttf"),
    );

    let mut image = Image::new();

    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
    let text = Components::Text(
        WIDTH / 2 - 210,
        HEIGHT / 2 - 250,
        40,
        "Xiaolin Wu's Line Algorithm",
        BLACK,
        Some((GREEN, 3)),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&line1, &line2, &circle, &text])
        .draw()?;
    Ok(())
}
examples/text.rs (line 19)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(
        WIDTH,
        HEIGHT,
        Rgba([255, 255, 255, 0]),
        Some(WHITE),
        "output.png",
        Some("./fonts/Roboto-Medium.ttf"),
    );

    let mut image = Image::new();

    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));

    let text = "OMAGE";
    let text = Components::Text(
        config.width / 2 - 40,
        config.height / 2 - 25,
        50,
        text,
        Rgba([255, 255, 255, 255]),
        Some((BLACK, 3)),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&text, &circle1, &circle2, &circle3])
        .draw()?;
    Ok(())
}
source

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?
examples/blend.rs (lines 19-25)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(WIDTH, HEIGHT, WHITE, Some(BLACK), "output.png", None);

    let mut image = Image::new();

    let circle1 = Components::Circle(config.width / 2, config.height / 2, 350, RED);
    let circle2 = Components::Circle(
        config.width / 2,
        config.height / 2,
        300,
        Rgba([255, 0, 255, 120]),
    );
    let rectangle = Components::Rectangle(
        100,
        100,
        config.width / 2 - 50,
        config.height / 2 - 50,
        Rgba([120, 0, 255, 19]),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&circle1, &circle2, &rectangle])
        .draw()?;
    Ok(())
}
source

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?
examples/line.rs (line 19)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(
        WIDTH,
        HEIGHT,
        BLACK,
        Some(GREEN),
        "output.png",
        Some("./fonts/Roboto-Medium.ttf"),
    );

    let mut image = Image::new();

    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
    let text = Components::Text(
        WIDTH / 2 - 210,
        HEIGHT / 2 - 250,
        40,
        "Xiaolin Wu's Line Algorithm",
        BLACK,
        Some((GREEN, 3)),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&line1, &line2, &circle, &text])
        .draw()?;
    Ok(())
}
source

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?
examples/line.rs (lines 22-29)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(
        WIDTH,
        HEIGHT,
        BLACK,
        Some(GREEN),
        "output.png",
        Some("./fonts/Roboto-Medium.ttf"),
    );

    let mut image = Image::new();

    let line1 = Components::Line(0, 0, WIDTH, HEIGHT, GREEN);
    let line2 = Components::Line(WIDTH, 0, 0, HEIGHT, GREEN);
    let circle = Components::Circle(WIDTH / 2, HEIGHT / 2, 100, Rgba([0, 255, 0, 150]));
    let text = Components::Text(
        WIDTH / 2 - 210,
        HEIGHT / 2 - 250,
        40,
        "Xiaolin Wu's Line Algorithm",
        BLACK,
        Some((GREEN, 3)),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&line1, &line2, &circle, &text])
        .draw()?;
    Ok(())
}
More examples
Hide additional examples
examples/text.rs (lines 24-31)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::new(
        WIDTH,
        HEIGHT,
        Rgba([255, 255, 255, 0]),
        Some(WHITE),
        "output.png",
        Some("./fonts/Roboto-Medium.ttf"),
    );

    let mut image = Image::new();

    let circle1 = Components::Circle(50, 55, 30, Rgba([255, 0, 0, 200]));
    let circle2 = Components::Circle(75, 55, 30, Rgba([0, 255, 0, 200]));
    let circle3 = Components::Circle(65, 35, 30, Rgba([0, 0, 255, 200]));

    let text = "OMAGE";
    let text = Components::Text(
        config.width / 2 - 40,
        config.height / 2 - 25,
        50,
        text,
        Rgba([255, 255, 255, 255]),
        Some((BLACK, 3)),
    );

    image
        .config(config)
        .init()?
        .add_components(vec![&text, &circle1, &circle2, &circle3])
        .draw()?;
    Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.