Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color improvements #77

Merged
merged 6 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
46 changes: 46 additions & 0 deletions src/graphics/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,42 @@ 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
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");
PvdBerg1998 marked this conversation as resolved.
Show resolved Hide resolved
Color { r, g, b, a }
}

Expand All @@ -50,6 +82,20 @@ impl Color {
}
}

/// Creates a new [`Color`] from its RGB representation (0xRRGGBB).
///
/// [`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;
Color::from_rgb(r, g, b)
}

/// Returns the [`Color`] components in the [0, 255] range.
///
/// [`Color`]: struct.Color.html
Expand Down