From e12f5619301cae0d05ec2d76de39dd51a1880d74 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 10 Jul 2013 14:16:16 +1000 Subject: [PATCH] Add channel and color normalization methods --- src/color/channel.rs | 7 +++++-- src/color/color.rs | 4 ++++ src/color/hsv.rs | 21 ++++++++++++++++++++- src/color/rgb.rs | 21 ++++++++++++++++++++- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/color/channel.rs b/src/color/channel.rs index e604c98..c879795 100644 --- a/src/color/channel.rs +++ b/src/color/channel.rs @@ -70,14 +70,17 @@ impl Channel for f64 { pub trait FloatChannel: Float + Channel { pub fn invert_degrees(&self) -> Self; + pub fn normalize_degrees(&self) -> Self; } impl FloatChannel for f32 { - #[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0) % 360.0 } + #[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0).normalize_degrees() } + #[inline] pub fn normalize_degrees(&self) -> f32 { (*self) % 360.0 } } impl FloatChannel for f64 { - #[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0) % 360.0 } + #[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0).normalize_degrees() } + #[inline] pub fn normalize_degrees(&self) -> f64 { (*self) % 360.0 } } #[cfg(test)] diff --git a/src/color/color.rs b/src/color/color.rs index 5b47894..0abc932 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -27,3 +27,7 @@ pub trait Color { pub fn clamp(&self, lo: T, hi: T) -> Self; pub fn inverse(&self) -> Self; } + +pub trait FloatColor: Color { + pub fn normalize(&self) -> Self; +} diff --git a/src/color/hsv.rs b/src/color/hsv.rs index 86b4851..b3e6a7e 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -16,7 +16,7 @@ use std::num; use std::cast; -use color::Color; +use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{RGB, ToRGB, RGBA, ToRGBA}; @@ -48,6 +48,15 @@ impl Color for HSV { } } +impl FloatColor for HSV { + #[inline] + pub fn normalize(&self) -> HSV { + HSV::new((*self).h.normalize_degrees(), + (*self).s.clamp(&zero!(T), &one!(T)), + (*self).v.clamp(&zero!(T), &one!(T))) + } +} + pub trait ToHSV { pub fn to_hsv(&self) -> HSV; } @@ -150,6 +159,16 @@ impl Color for HSVA { } } +impl FloatColor for HSVA { + #[inline] + pub fn normalize(&self) -> HSVA { + HSVA::new((*self).h.normalize_degrees(), + (*self).s.clamp(&zero!(T), &one!(T)), + (*self).v.clamp(&zero!(T), &one!(T)), + (*self).a.clamp(&zero!(T), &one!(T))) + } +} + pub trait ToHSVA { pub fn to_hsva(&self) -> HSVA; } diff --git a/src/color/rgb.rs b/src/color/rgb.rs index f7d69ff..dfe260a 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -16,7 +16,7 @@ use std::num; use std::cast; -use color::Color; +use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{HSV, ToHSV, HSVA, ToHSVA}; @@ -49,6 +49,15 @@ impl Color for RGB { } } +impl FloatColor for RGB { + #[inline] + pub fn normalize(&self) -> RGB { + RGB::new((*self).r.clamp(&zero!(T), &one!(T)), + (*self).g.clamp(&zero!(T), &one!(T)), + (*self).b.clamp(&zero!(T), &one!(T))) + } +} + pub trait ToRGB { pub fn to_rgb(&self) -> RGB; } @@ -148,6 +157,16 @@ impl Color for RGBA { } } +impl FloatColor for RGBA { + #[inline] + pub fn normalize(&self) -> RGBA { + RGBA::new((*self).r.clamp(&zero!(T), &one!(T)), + (*self).g.clamp(&zero!(T), &one!(T)), + (*self).b.clamp(&zero!(T), &one!(T)), + (*self).a.clamp(&zero!(T), &one!(T))) + } +} + pub trait ToRGBA { pub fn to_rgba(&self) -> RGBA; }