From d613c6fa21ea5c0c51defb517d9f914e82f94457 Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 15:08:06 +0200 Subject: [PATCH 1/6] Add Color::from_rgb_u32 --- src/graphics/color.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/graphics/color.rs b/src/graphics/color.rs index 0c1a96d..ff42bce 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -50,6 +50,16 @@ impl Color { } } + /// Creates a new [`Color`] from its RGB representation (0xRRGGBB). + /// + /// [`Color`]: struct.Color.html + pub fn from_rgb_u32(color: u32) -> Color { + let r = ((color & 0xFF0000) >> 16) as u8; + let g = ((color & 0x00FF00) >> 8) as u8; + let b = ((color & 0x0000FF) >> 0) as u8; + Color::from_rgb(r, g, b) + } + /// Returns the [`Color`] components in the [0, 255] range. /// /// [`Color`]: struct.Color.html From d07640c57a9723882eb3bbcac6043f5612a42b90 Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 15:09:56 +0200 Subject: [PATCH 2/6] Add additional color constants --- src/graphics/color.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/graphics/color.rs b/src/graphics/color.rs index ff42bce..38478e7 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -31,6 +31,30 @@ impl Color { a: 1.0, }; + /// Red color. + pub const RED: Self = Self { + r: 1.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; + + /// Green color. + pub const GREEN: Self = Self { + r: 0.0, + g: 1.0, + b: 0.0, + a: 1.0, + }; + + /// Blue color. + pub const BLUE: Self = Self { + r: 0.0, + g: 0.0, + b: 1.0, + a: 1.0, + }; + /// Creates a new [`Color`] from components in the [0, 1.0] range. /// /// [`Color`]: struct.Color.html From c610a41f18ae3b37ec7dde98ccf9660bb241d1da Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 19:19:09 +0200 Subject: [PATCH 3/6] Add debug assertions --- src/graphics/color.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/graphics/color.rs b/src/graphics/color.rs index 38478e7..a471667 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -59,6 +59,14 @@ impl Color { /// /// [`Color`]: struct.Color.html pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { + debug_assert!(r >= 0.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(r <= 1.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(g >= 0.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(g <= 1.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(b >= 0.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(b <= 1.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(a >= 0.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(a <= 1.0, "Color values should be in the [0, 1.0] range"); Color { r, g, b, a } } @@ -78,6 +86,7 @@ impl Color { /// /// [`Color`]: struct.Color.html pub fn from_rgb_u32(color: u32) -> Color { + debug_assert!(color <= 0xFFFFFF, "Color contains value higher than 0xFFFFFF"); let r = ((color & 0xFF0000) >> 16) as u8; let g = ((color & 0x00FF00) >> 8) as u8; let b = ((color & 0x0000FF) >> 0) as u8; From 19cdc75e680812a7c44053869718776a78be64cb Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 20:55:38 +0200 Subject: [PATCH 4/6] Add notes to CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bc8490..71983f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Default` implementation for `ui::widget::slider::State`. - `Default`, `Clone`, `Copy`, `PartialEq`, and `Eq` implementations for `ui::widget::button::State`. +- `Color::RED`, `Color::GREEN`, `Color::BLUE` constants. +- `Color::from_rgb_u32`: Constructs a `Color` from `0xRRGGBB`. ### Changed - `Mesh::stroke` now takes an `f32` as `line_width` instead of a `u16`. From 3e317eec1a803e2249bb03c8536ffa13313cdb93 Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 21:00:45 +0200 Subject: [PATCH 5/6] Run rustfmt --- src/graphics/color.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/graphics/color.rs b/src/graphics/color.rs index a471667..c88d7e0 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -86,7 +86,10 @@ impl Color { /// /// [`Color`]: struct.Color.html pub fn from_rgb_u32(color: u32) -> Color { - debug_assert!(color <= 0xFFFFFF, "Color contains value higher than 0xFFFFFF"); + debug_assert!( + color <= 0xFFFFFF, + "Color contains value higher than 0xFFFFFF" + ); let r = ((color & 0xFF0000) >> 16) as u8; let g = ((color & 0x00FF00) >> 8) as u8; let b = ((color & 0x0000FF) >> 0) as u8; From a9f9bec709ae7e179abc330e9dedf3427e47a966 Mon Sep 17 00:00:00 2001 From: PvdBerg1998 Date: Sun, 28 Jul 2019 22:21:23 +0200 Subject: [PATCH 6/6] Improve color constructor assertion messages --- src/graphics/color.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/graphics/color.rs b/src/graphics/color.rs index c88d7e0..57bd285 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -59,14 +59,14 @@ impl Color { /// /// [`Color`]: struct.Color.html pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { - debug_assert!(r >= 0.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(r <= 1.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(g >= 0.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(g <= 1.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(b >= 0.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(b <= 1.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(a >= 0.0, "Color values should be in the [0, 1.0] range"); - debug_assert!(a <= 1.0, "Color values should be in the [0, 1.0] range"); + debug_assert!(r >= 0.0, "Red component is < 0.0"); + debug_assert!(r <= 1.0, "Red component is > 1.0"); + debug_assert!(g >= 0.0, "Green component is < 0.0"); + debug_assert!(g <= 1.0, "Green component is > 1.0"); + debug_assert!(b >= 0.0, "Blue component is < 0.0"); + debug_assert!(b <= 1.0, "Blue component is > 1.0"); + debug_assert!(a >= 0.0, "Alpha component is < 0.0"); + debug_assert!(a <= 1.0, "Alpha component is > 1.0"); Color { r, g, b, a } }