Skip to main content

Color

Struct Color 

Source
#[repr(transparent)]
pub struct Color { pub data: u32, }
Expand description

A color

Fields§

§data: u32

Implementations§

Source§

impl Color

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Create a new color from RGB

Examples found in repository?
examples/rect_bench.rs (line 23)
20fn main() {
21    let mut window = Window::new(10, 10, 800, 600, "RECTANGLE BENCHMARK").unwrap();
22
23    time!("set", { window.set(Color::rgb(255, 255, 255)) });
24
25    time!("rect 400x400", {
26        window.rect(0, 0, 400, 400, Color::rgb(0, 0, 255))
27    });
28
29    time!("rect 200x200", {
30        window.rect(0, 0, 200, 200, Color::rgb(0, 255, 0))
31    });
32
33    time!("rect 100x100", {
34        window.rect(0, 0, 100, 100, Color::rgb(255, 0, 0))
35    });
36
37    time!("sync", {
38        window.update();
39    });
40
41    'events: loop {
42        for event in window.events() {
43            #[allow(clippy::single_match)]
44            match event.to_option() {
45                EventOption::Quit(_quit_event) => break 'events,
46                _ => (),
47            }
48        }
49    }
50}
More examples
Hide additional examples
examples/image_bench.rs (line 27)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
examples/simple.rs (line 30)
5fn main() {
6    let (width, height) = orbclient::get_display_size().unwrap();
7
8    let mut window = Window::new_flags(
9        (width as i32) / 4,
10        (height as i32) / 4,
11        width / 2,
12        height / 2,
13        "TITLE",
14        &[WindowFlag::Transparent],
15    )
16    .unwrap();
17
18    let (win_w, win_h) = (width / 2, height / 2);
19
20    // top left -> bottom rigth
21    window.linear_gradient(
22        0,
23        0,
24        win_w / 3,
25        win_h,
26        0,
27        0,
28        (win_w / 3) as i32,
29        (win_h / 2) as i32,
30        Color::rgb(128, 128, 128),
31        Color::rgb(255, 255, 255),
32    );
33    // horizontal gradient
34    window.linear_gradient(
35        (win_w / 3) as i32,
36        0,
37        win_w / 3,
38        win_h,
39        (win_w / 3) as i32,
40        0,
41        (2 * win_w / 3) as i32,
42        0,
43        Color::rgb(128, 255, 255),
44        Color::rgb(255, 255, 255),
45    );
46    // vertical gradient
47    window.linear_gradient(
48        (2 * win_w / 3) as i32,
49        0,
50        win_w / 3,
51        win_h,
52        (2 * win_w / 3) as i32,
53        0,
54        (2 * win_w / 3) as i32,
55        win_h as i32,
56        Color::rgb(0, 128, 0),
57        Color::rgb(255, 255, 255),
58    );
59    window.arc(100, 100, -25, 1 << 0 | 1 << 2, Color::rgb(0, 0, 255));
60    window.arc(100, 100, -25, 1 << 1 | 1 << 3, Color::rgb(0, 255, 255));
61    window.arc(100, 100, -25, 1 << 4 | 1 << 6, Color::rgb(255, 0, 255));
62    window.arc(100, 100, -25, 1 << 5 | 1 << 7, Color::rgb(255, 255, 0));
63    window.circle(100, 100, 25, Color::rgb(0, 0, 0));
64    window.circle(100, 101, -25, Color::rgb(0, 255, 0));
65    window.circle(220, 220, -100, Color::rgba(128, 128, 128, 80));
66    window.wu_circle(150, 220, 100, Color::rgba(255, 0, 0, 255));
67    window.line(0, 0, 200, 200, Color::rgb(255, 0, 0));
68    window.line(0, 200, 200, 0, Color::rgb(128, 255, 0));
69    // vertical and horizontal line test
70    window.line(100, 0, 100, 200, Color::rgb(0, 0, 255));
71    window.line(0, 100, 200, 100, Color::rgb(255, 255, 0));
72    window.wu_line(100, 220, 400, 250, Color::rgba(255, 0, 0, 255));
73    window.line(100, 230, 400, 260, Color::rgba(255, 0, 0, 255));
74
75    // path and bezier curve example draw a cloud
76    let mut cloud_path = GraphicsPath::new();
77    cloud_path.move_to(170, 80);
78    cloud_path.bezier_curve_to(130, 100, 130, 150, 230, 150);
79    cloud_path.bezier_curve_to(250, 180, 320, 180, 340, 150);
80    cloud_path.bezier_curve_to(420, 150, 420, 120, 390, 100);
81    cloud_path.bezier_curve_to(430, 40, 370, 30, 340, 50);
82    cloud_path.bezier_curve_to(320, 5, 250, 20, 250, 50);
83    cloud_path.bezier_curve_to(200, 5, 150, 20, 170, 80);
84    window.draw_path_stroke(cloud_path, Color::rgb(0, 0, 255));
85
86    // path and quadratic curve example draw a balloon
87    let mut balloon_path = GraphicsPath::new();
88    balloon_path.move_to(75, 25);
89    balloon_path.quadratic_curve_to(25, 25, 25, 62);
90    balloon_path.quadratic_curve_to(25, 100, 50, 100);
91    balloon_path.quadratic_curve_to(50, 120, 30, 125);
92    balloon_path.quadratic_curve_to(60, 120, 65, 100);
93    balloon_path.quadratic_curve_to(125, 100, 125, 62);
94    balloon_path.quadratic_curve_to(125, 25, 75, 25);
95    window.draw_path_stroke(balloon_path, Color::rgb(0, 0, 255));
96
97    window.char(200, 200, '═', Color::rgb(0, 0, 0));
98    window.char(208, 200, '═', Color::rgb(0, 0, 0));
99
100    // testing for non existent x,y position : does not panic but returns Color(0,0,0,0)
101    let _non_existent_pixel = window.getpixel(width as i32 + 10, height as i32 + 10);
102
103    // testing PartialEq for Color
104    if Color::rgb(11, 2, 3) == Color::rgba(1, 2, 3, 100) {
105        println!("Testing colors: they are the same!")
106    } else {
107        println!("Testing colors: they are NOT the same!")
108    }
109
110    //Draw a transparent rectangle over window content
111    // default mode is Blend
112    window.rect(250, 200, 80, 80, Color::rgba(100, 100, 100, 100));
113
114    //Draw an opaque rectangle replacing window content
115    window.mode().set(Mode::Overwrite); // set window drawing mode to Overwrite from now on
116    window.rect(300, 220, 80, 80, Color::rgb(100, 100, 100));
117
118    //Draw a hole in the window replacing alpha channel (Only in Orbital, not in SDL2)
119    window.rect(300, 100, 80, 80, Color::rgba(10, 10, 10, 1));
120
121    //Draw a transparent rectangle over window content
122    window.mode().set(Mode::Blend); //set mode to Blend fron now on
123    window.rect(200, 230, 80, 80, Color::rgba(100, 100, 100, 100));
124
125    //Draw a blured box over window content
126    window.box_blur(170, 100, 150, 150, 10);
127
128    //Draw a shadow around a box
129    window.box_shadow(170, 100, 150, 150, 0, 0, 20, Color::rgba(0, 0, 0, 255));
130
131    window.sync();
132
133    'events: loop {
134        for event in window.events() {
135            match event.to_option() {
136                EventOption::Quit(_quit_event) => break 'events,
137                EventOption::Mouse(evt) => println!(
138                    "At position {:?} pixel color is : {:?}",
139                    (evt.x, evt.y),
140                    window.getpixel(evt.x, evt.y)
141                ),
142                event_option => println!("{:?}", event_option),
143            }
144        }
145    }
146}
Source

pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self

Set the alpha

Examples found in repository?
examples/image_bench.rs (line 30)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
More examples
Hide additional examples
examples/simple.rs (line 65)
5fn main() {
6    let (width, height) = orbclient::get_display_size().unwrap();
7
8    let mut window = Window::new_flags(
9        (width as i32) / 4,
10        (height as i32) / 4,
11        width / 2,
12        height / 2,
13        "TITLE",
14        &[WindowFlag::Transparent],
15    )
16    .unwrap();
17
18    let (win_w, win_h) = (width / 2, height / 2);
19
20    // top left -> bottom rigth
21    window.linear_gradient(
22        0,
23        0,
24        win_w / 3,
25        win_h,
26        0,
27        0,
28        (win_w / 3) as i32,
29        (win_h / 2) as i32,
30        Color::rgb(128, 128, 128),
31        Color::rgb(255, 255, 255),
32    );
33    // horizontal gradient
34    window.linear_gradient(
35        (win_w / 3) as i32,
36        0,
37        win_w / 3,
38        win_h,
39        (win_w / 3) as i32,
40        0,
41        (2 * win_w / 3) as i32,
42        0,
43        Color::rgb(128, 255, 255),
44        Color::rgb(255, 255, 255),
45    );
46    // vertical gradient
47    window.linear_gradient(
48        (2 * win_w / 3) as i32,
49        0,
50        win_w / 3,
51        win_h,
52        (2 * win_w / 3) as i32,
53        0,
54        (2 * win_w / 3) as i32,
55        win_h as i32,
56        Color::rgb(0, 128, 0),
57        Color::rgb(255, 255, 255),
58    );
59    window.arc(100, 100, -25, 1 << 0 | 1 << 2, Color::rgb(0, 0, 255));
60    window.arc(100, 100, -25, 1 << 1 | 1 << 3, Color::rgb(0, 255, 255));
61    window.arc(100, 100, -25, 1 << 4 | 1 << 6, Color::rgb(255, 0, 255));
62    window.arc(100, 100, -25, 1 << 5 | 1 << 7, Color::rgb(255, 255, 0));
63    window.circle(100, 100, 25, Color::rgb(0, 0, 0));
64    window.circle(100, 101, -25, Color::rgb(0, 255, 0));
65    window.circle(220, 220, -100, Color::rgba(128, 128, 128, 80));
66    window.wu_circle(150, 220, 100, Color::rgba(255, 0, 0, 255));
67    window.line(0, 0, 200, 200, Color::rgb(255, 0, 0));
68    window.line(0, 200, 200, 0, Color::rgb(128, 255, 0));
69    // vertical and horizontal line test
70    window.line(100, 0, 100, 200, Color::rgb(0, 0, 255));
71    window.line(0, 100, 200, 100, Color::rgb(255, 255, 0));
72    window.wu_line(100, 220, 400, 250, Color::rgba(255, 0, 0, 255));
73    window.line(100, 230, 400, 260, Color::rgba(255, 0, 0, 255));
74
75    // path and bezier curve example draw a cloud
76    let mut cloud_path = GraphicsPath::new();
77    cloud_path.move_to(170, 80);
78    cloud_path.bezier_curve_to(130, 100, 130, 150, 230, 150);
79    cloud_path.bezier_curve_to(250, 180, 320, 180, 340, 150);
80    cloud_path.bezier_curve_to(420, 150, 420, 120, 390, 100);
81    cloud_path.bezier_curve_to(430, 40, 370, 30, 340, 50);
82    cloud_path.bezier_curve_to(320, 5, 250, 20, 250, 50);
83    cloud_path.bezier_curve_to(200, 5, 150, 20, 170, 80);
84    window.draw_path_stroke(cloud_path, Color::rgb(0, 0, 255));
85
86    // path and quadratic curve example draw a balloon
87    let mut balloon_path = GraphicsPath::new();
88    balloon_path.move_to(75, 25);
89    balloon_path.quadratic_curve_to(25, 25, 25, 62);
90    balloon_path.quadratic_curve_to(25, 100, 50, 100);
91    balloon_path.quadratic_curve_to(50, 120, 30, 125);
92    balloon_path.quadratic_curve_to(60, 120, 65, 100);
93    balloon_path.quadratic_curve_to(125, 100, 125, 62);
94    balloon_path.quadratic_curve_to(125, 25, 75, 25);
95    window.draw_path_stroke(balloon_path, Color::rgb(0, 0, 255));
96
97    window.char(200, 200, '═', Color::rgb(0, 0, 0));
98    window.char(208, 200, '═', Color::rgb(0, 0, 0));
99
100    // testing for non existent x,y position : does not panic but returns Color(0,0,0,0)
101    let _non_existent_pixel = window.getpixel(width as i32 + 10, height as i32 + 10);
102
103    // testing PartialEq for Color
104    if Color::rgb(11, 2, 3) == Color::rgba(1, 2, 3, 100) {
105        println!("Testing colors: they are the same!")
106    } else {
107        println!("Testing colors: they are NOT the same!")
108    }
109
110    //Draw a transparent rectangle over window content
111    // default mode is Blend
112    window.rect(250, 200, 80, 80, Color::rgba(100, 100, 100, 100));
113
114    //Draw an opaque rectangle replacing window content
115    window.mode().set(Mode::Overwrite); // set window drawing mode to Overwrite from now on
116    window.rect(300, 220, 80, 80, Color::rgb(100, 100, 100));
117
118    //Draw a hole in the window replacing alpha channel (Only in Orbital, not in SDL2)
119    window.rect(300, 100, 80, 80, Color::rgba(10, 10, 10, 1));
120
121    //Draw a transparent rectangle over window content
122    window.mode().set(Mode::Blend); //set mode to Blend fron now on
123    window.rect(200, 230, 80, 80, Color::rgba(100, 100, 100, 100));
124
125    //Draw a blured box over window content
126    window.box_blur(170, 100, 150, 150, 10);
127
128    //Draw a shadow around a box
129    window.box_shadow(170, 100, 150, 150, 0, 0, 20, Color::rgba(0, 0, 0, 255));
130
131    window.sync();
132
133    'events: loop {
134        for event in window.events() {
135            match event.to_option() {
136                EventOption::Quit(_quit_event) => break 'events,
137                EventOption::Mouse(evt) => println!(
138                    "At position {:?} pixel color is : {:?}",
139                    (evt.x, evt.y),
140                    window.getpixel(evt.x, evt.y)
141                ),
142                event_option => println!("{:?}", event_option),
143            }
144        }
145    }
146}
Source

pub fn r(&self) -> u8

Get the r value

Source

pub fn g(&self) -> u8

Get the g value

Source

pub fn b(&self) -> u8

Get the b value

Source

pub fn a(&self) -> u8

Get the alpha value

Source

pub fn interpolate(start_color: Color, end_color: Color, scale: f64) -> Color

Interpolate between two colors

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Color

Compare two colors (Do not take care of alpha)

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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.

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin 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> 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.