Add inversion functions, rename check_rgba -> new
This commit is contained in:
parent
62fddce2e6
commit
27a4cbccea
1 changed files with 14 additions and 4 deletions
|
|
@ -1,6 +1,5 @@
|
|||
/// A color in the sRGB color space.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[allow(missing_docs)]
|
||||
pub struct Color {
|
||||
/// Red component, 0.0 - 1.0
|
||||
pub r: f32,
|
||||
|
|
@ -12,7 +11,6 @@ pub struct Color {
|
|||
pub a: f32,
|
||||
}
|
||||
|
||||
|
||||
impl Color {
|
||||
/// The black color.
|
||||
pub const BLACK: Color = Color {
|
||||
|
|
@ -44,7 +42,7 @@ impl Color {
|
|||
}
|
||||
|
||||
/// Ensures RGBA values on the range [0.0, 1.0]
|
||||
pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color {
|
||||
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
|
||||
Color {
|
||||
r: Color::clamp(r),
|
||||
g: Color::clamp(g),
|
||||
|
|
@ -57,7 +55,7 @@ impl Color {
|
|||
///
|
||||
/// [`Color`]: struct.Color.html
|
||||
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
|
||||
Color::check_rgba(r, g, b, 1.0)
|
||||
Color::new(r, g, b, 1.0)
|
||||
}
|
||||
|
||||
/// Creates a [`Color`] from its RGB8 components.
|
||||
|
|
@ -100,6 +98,18 @@ impl Color {
|
|||
self.a,
|
||||
]
|
||||
}
|
||||
|
||||
/// Invert the Color in-place
|
||||
pub fn invert(&mut self) {
|
||||
self.r = Color::clamp(1.0f32 - self.r);
|
||||
self.b = Color::clamp(1.0f32 - self.g);
|
||||
self.g = Color::clamp(1.0f32 - self.b);
|
||||
}
|
||||
|
||||
/// Return an inverted Color
|
||||
pub fn inverse(self) -> Color {
|
||||
Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[f32; 3]> for Color {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue