Add check_rgba fn to clamp float values to [0,1]
This commit is contained in:
parent
70081c9649
commit
62fddce2e6
1 changed files with 23 additions and 3 deletions
|
|
@ -2,12 +2,17 @@
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub struct Color {
|
pub struct Color {
|
||||||
|
/// Red component, 0.0 - 1.0
|
||||||
pub r: f32,
|
pub r: f32,
|
||||||
|
/// Green component, 0.0 - 1.0
|
||||||
pub g: f32,
|
pub g: f32,
|
||||||
|
/// Blue component, 0.0 - 1.0
|
||||||
pub b: f32,
|
pub b: f32,
|
||||||
|
/// Transparency, 0.0 - 1.0
|
||||||
pub a: f32,
|
pub a: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
/// The black color.
|
/// The black color.
|
||||||
pub const BLACK: Color = Color {
|
pub const BLACK: Color = Color {
|
||||||
|
|
@ -33,11 +38,26 @@ impl Color {
|
||||||
a: 0.0,
|
a: 0.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Calmps a float value to the range [0.0, 1.0]
|
||||||
|
pub fn clamp(v: f32) -> f32 {
|
||||||
|
v.max(0.0f32).min(1.0f32)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensures RGBA values on the range [0.0, 1.0]
|
||||||
|
pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color {
|
||||||
|
Color {
|
||||||
|
r: Color::clamp(r),
|
||||||
|
g: Color::clamp(g),
|
||||||
|
b: Color::clamp(b),
|
||||||
|
a: Color::clamp(a),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a [`Color`] from its RGB components.
|
/// Creates a [`Color`] from its RGB components.
|
||||||
///
|
///
|
||||||
/// [`Color`]: struct.Color.html
|
/// [`Color`]: struct.Color.html
|
||||||
pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color {
|
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
|
||||||
Color { r, g, b, a: 1.0 }
|
Color::check_rgba(r, g, b, 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Color`] from its RGB8 components.
|
/// Creates a [`Color`] from its RGB8 components.
|
||||||
|
|
@ -55,7 +75,7 @@ impl Color {
|
||||||
r: f32::from(r) / 255.0,
|
r: f32::from(r) / 255.0,
|
||||||
g: f32::from(g) / 255.0,
|
g: f32::from(g) / 255.0,
|
||||||
b: f32::from(b) / 255.0,
|
b: f32::from(b) / 255.0,
|
||||||
a,
|
a: Color::clamp(a),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue