Color

Struct Color 

Source
pub struct Color { /* private fields */ }
Expand description

A object representing a color in RGBA32 format. Red/Green/Blue/Alpha each range from 0 to 255.

Implementations§

Source§

impl Color

Source

pub const TRANSPARENT: Color

Source

pub const BLACK: Color

Source

pub const WHITE: Color

Source

pub const GRAY: Color

Source

pub const DARK_GRAY: Color

Source

pub const LIGHT_GRAY: Color

Source

pub const RED: Color

Source

pub const GREEN: Color

Source

pub const BLUE: Color

Source

pub const fn rgb(red: u8, green: u8, blue: u8) -> Self

Create a Color from red, green and blue.

§Examples
let color = Color::rgb(0x1D, 0x37, 0x85);
Source

pub const fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self

Create a Color from red, green, blue and alpha.

§Examples
let color = Color::rgba(0x1D, 0x37, 0x85, 127);
Source

pub fn with_alpha(self, alpha: u8) -> Self

Create a Color from an existing color with alpha set to alpha.

§Examples
let color = Color::BLUE.with_alpha(127);
Examples found in repository?
examples/hello-world.rs (line 53)
7fn main() {
8    // Create a new Pronto Graphics window, all drawing happens through this object.
9    // The window's drawable area will be of size 800 by 600, and the window title will be "Hello World".
10    let mut pg = Window::new(800, 600, "Hello World");
11
12    // Alternatively, we can create a full screen window.
13    // let mut pg = Window::new_fullscreen();
14
15    // Set the background color for our window.
16    // At the beginning of each frame, the window is cleared with this color.
17    pg.background_color((0x11, 0x11, 0x11));
18
19    // Load a texture from a file.
20    let texture = pg.load_texture("examples/test_texture.png").unwrap();
21
22    // Load a font from a file.
23    let font = pg.load_font("examples/Whisper-Regular.ttf").unwrap();
24
25    loop {
26        // Set the fill color for drawing shapes to blue.
27        pg.fill_color(Color::BLUE);
28        // Draw a circle in the middle of the window (width/2, height/2) with a radius of 64 pixels.
29        pg.circle((pg.width() / 2., pg.height() / 2.), 64.);
30
31        {
32            let pos = (
33                0.5 * pg.width() + 128. * pg.time().cos(),
34                0.5 * pg.height() + 128. * pg.time().sin(),
35            );
36
37            // Set the fill color to #CCCCCC, i.e. a light grey.
38            pg.fill_color((0xCC, 0xCC, 0xCC));
39            // Draw a circle at position `pos` with a radius of 16 pixels.
40            pg.circle(pos, 16.);
41        }
42
43        // Draw our texture at position `(10, 10)` with a width of 100 pixels.
44        // The texture's height will be deduced from the texture's aspect ratio.
45        // I.e. the texture is not distorted.
46        // If we want to set the height ourselves, we have to use `pg.texture(...)` (Note the missing `_`)
47        pg.texture_((10., 10.), texture, 150.);
48
49        // React to the the left mouse button being pressed.
50        // As long as the button is held down, the code inside the `if` is executed.
51        if pg.mouse_pressed(Button::LEFT) {
52            // Set the fill color to blue, but with it's alpha set to 127, i.e. half-transparent.
53            pg.fill_color(Color::GREEN.with_alpha(127));
54            pg.circle(pg.mouse_position(), 16.);
55        }
56
57        // React to the `SPACE` key having just been pressed this very frame.
58        // The code inside the if will only be executed once every time you press the key.
59        // I.e. Holding the key down will _not_ cause "Action!" to be printed every frame.
60        // If we want to react to a held down key, we have to use `pg.key_pressed(...)`
61        if pg.key_just_pressed(Key::SPACE) {
62            println!("Action!");
63        }
64
65        // React to the mouse wheel having been scrolled.
66        // `pg.mouse_wheel()` will give you a cumulative amount of scrolling across frames.
67        // If you want to know whether the scroll wheel has been scrolled in the current frame,
68        // use `pg.mouse_wheel_delta()` instead.
69        if pg.mouse_wheel() > 0. {
70            pg.fill_color(Color::GREEN);
71        } else {
72            pg.fill_color(Color::RED);
73        }
74
75        pg.font_color(Color::GREEN);
76        pg.font_size(40);
77        pg.font(Some(font));
78        pg.text((300., 50.), "Salutations!");
79
80        // Draw a square with side lengths of 25 pixels.
81        // Depending on what fill color has last been set above,
82        // this square will either be green or red.
83        pg.square((50., 300.), 25.);
84
85        {
86            let pos = (
87                pg.width() - 200.,
88                50. + ((50. * pg.time()) % (pg.height() - 130.)),
89            );
90
91            // Set the font color to a lovely pink of #EE4488.
92            pg.font_color((0xEE, 0x44, 0x88));
93            // Set the font size to 30 pixels.
94            pg.font_size(30);
95            // Set the font to the default font.
96            pg.font(None);
97            // Draw the string "Greetings!" as position `pos`.
98            pg.text(pos, "Greetings!");
99        }
100
101        // Set the line color from HSB, with the hue rotating through the colors.
102        pg.line_color(Color::from_hsb((60. * pg.time()) % 360., 1., 1.));
103
104        // Draw a bunch of lines.
105        pg.line((50., 500.), (50., 450.));
106        pg.line((50., 450.), (100., 500.));
107        pg.line((100., 475. + 25. * (5. * pg.time()).sin()), (150., 450.));
108
109        // At the end of our frame, we have to update the window
110        // for our drawings to appear on the screen and for the keyboard/mouse
111        // states to be updated.
112        // If your window ever is blank or looks glitchy, make sure you haven't forgotten
113        // to call `pg.update()` at the end of your `loop`.
114        pg.update();
115    }
116}
Source

pub fn from_hsb(hue: f32, saturation: f32, brightness: f32) -> Self

Create a Color from HSB values.

  • hue : Between 0 and 360.
  • saturation : Between 0 and 1.
  • brightness : Between 0 and 1.
Examples found in repository?
examples/hello-world.rs (line 102)
7fn main() {
8    // Create a new Pronto Graphics window, all drawing happens through this object.
9    // The window's drawable area will be of size 800 by 600, and the window title will be "Hello World".
10    let mut pg = Window::new(800, 600, "Hello World");
11
12    // Alternatively, we can create a full screen window.
13    // let mut pg = Window::new_fullscreen();
14
15    // Set the background color for our window.
16    // At the beginning of each frame, the window is cleared with this color.
17    pg.background_color((0x11, 0x11, 0x11));
18
19    // Load a texture from a file.
20    let texture = pg.load_texture("examples/test_texture.png").unwrap();
21
22    // Load a font from a file.
23    let font = pg.load_font("examples/Whisper-Regular.ttf").unwrap();
24
25    loop {
26        // Set the fill color for drawing shapes to blue.
27        pg.fill_color(Color::BLUE);
28        // Draw a circle in the middle of the window (width/2, height/2) with a radius of 64 pixels.
29        pg.circle((pg.width() / 2., pg.height() / 2.), 64.);
30
31        {
32            let pos = (
33                0.5 * pg.width() + 128. * pg.time().cos(),
34                0.5 * pg.height() + 128. * pg.time().sin(),
35            );
36
37            // Set the fill color to #CCCCCC, i.e. a light grey.
38            pg.fill_color((0xCC, 0xCC, 0xCC));
39            // Draw a circle at position `pos` with a radius of 16 pixels.
40            pg.circle(pos, 16.);
41        }
42
43        // Draw our texture at position `(10, 10)` with a width of 100 pixels.
44        // The texture's height will be deduced from the texture's aspect ratio.
45        // I.e. the texture is not distorted.
46        // If we want to set the height ourselves, we have to use `pg.texture(...)` (Note the missing `_`)
47        pg.texture_((10., 10.), texture, 150.);
48
49        // React to the the left mouse button being pressed.
50        // As long as the button is held down, the code inside the `if` is executed.
51        if pg.mouse_pressed(Button::LEFT) {
52            // Set the fill color to blue, but with it's alpha set to 127, i.e. half-transparent.
53            pg.fill_color(Color::GREEN.with_alpha(127));
54            pg.circle(pg.mouse_position(), 16.);
55        }
56
57        // React to the `SPACE` key having just been pressed this very frame.
58        // The code inside the if will only be executed once every time you press the key.
59        // I.e. Holding the key down will _not_ cause "Action!" to be printed every frame.
60        // If we want to react to a held down key, we have to use `pg.key_pressed(...)`
61        if pg.key_just_pressed(Key::SPACE) {
62            println!("Action!");
63        }
64
65        // React to the mouse wheel having been scrolled.
66        // `pg.mouse_wheel()` will give you a cumulative amount of scrolling across frames.
67        // If you want to know whether the scroll wheel has been scrolled in the current frame,
68        // use `pg.mouse_wheel_delta()` instead.
69        if pg.mouse_wheel() > 0. {
70            pg.fill_color(Color::GREEN);
71        } else {
72            pg.fill_color(Color::RED);
73        }
74
75        pg.font_color(Color::GREEN);
76        pg.font_size(40);
77        pg.font(Some(font));
78        pg.text((300., 50.), "Salutations!");
79
80        // Draw a square with side lengths of 25 pixels.
81        // Depending on what fill color has last been set above,
82        // this square will either be green or red.
83        pg.square((50., 300.), 25.);
84
85        {
86            let pos = (
87                pg.width() - 200.,
88                50. + ((50. * pg.time()) % (pg.height() - 130.)),
89            );
90
91            // Set the font color to a lovely pink of #EE4488.
92            pg.font_color((0xEE, 0x44, 0x88));
93            // Set the font size to 30 pixels.
94            pg.font_size(30);
95            // Set the font to the default font.
96            pg.font(None);
97            // Draw the string "Greetings!" as position `pos`.
98            pg.text(pos, "Greetings!");
99        }
100
101        // Set the line color from HSB, with the hue rotating through the colors.
102        pg.line_color(Color::from_hsb((60. * pg.time()) % 360., 1., 1.));
103
104        // Draw a bunch of lines.
105        pg.line((50., 500.), (50., 450.));
106        pg.line((50., 450.), (100., 500.));
107        pg.line((100., 475. + 25. * (5. * pg.time()).sin()), (150., 450.));
108
109        // At the end of our frame, we have to update the window
110        // for our drawings to appear on the screen and for the keyboard/mouse
111        // states to be updated.
112        // If your window ever is blank or looks glitchy, make sure you haven't forgotten
113        // to call `pg.update()` at the end of your `loop`.
114        pg.update();
115    }
116}
Source

pub fn red(&self) -> u8

The red component of the color.

Source

pub fn green(&self) -> u8

The green component of the color.

Source

pub fn blue(&self) -> u8

The blue component of the color.

Source

pub fn alpha(&self) -> u8

The alpha component of the color.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<(u8, u8, u8)> for Color

Source§

fn from(rgb: (u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl From<(u8, u8, u8, u8)> for Color

Source§

fn from(rgb: (u8, u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Color

Source§

fn from(color: Color) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Color

Source§

impl Eq for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.