From 49715e64798f1d42394dc84daf170dd890575ae1 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 9 Jul 2013 21:28:23 +1000 Subject: [PATCH] Merge some color modules --- src/color/color.rs | 20 +++++------- src/color/hsv.rs | 59 +++++++++++++++++++++++++++++++++-- src/color/hsva.rs | 76 ---------------------------------------------- src/color/rgb.rs | 53 +++++++++++++++++++++++++++++++- src/color/rgba.rs | 72 ------------------------------------------- src/color/srgb.rs | 10 ++++++ src/color/srgba.rs | 24 --------------- 7 files changed, 125 insertions(+), 189 deletions(-) delete mode 100644 src/color/hsva.rs delete mode 100644 src/color/rgba.rs delete mode 100644 src/color/srgba.rs diff --git a/src/color/color.rs b/src/color/color.rs index cf691c7..e73a5bb 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -15,22 +15,16 @@ pub use self::channel::{Channel, IntChannel, FloatChannel}; pub use self::channel::{ToChannel, ToIntChannel, ToFloatChannel}; -pub use self::hsv::{HSV, ToHSV}; -pub use self::hsva::{HSVA, ToHSVA}; -pub use self::rgb::{RGB, ToRGB}; -pub use self::rgba::{RGBA, ToRGBA}; -pub use self::srgb::SRGB; -pub use self::srgba::SRGBA; +pub use self::hsv::{HSV, ToHSV, HSVA, ToHSVA}; +pub use self::rgb::{RGB, ToRGB, RGBA, ToRGBA}; +pub use self::srgb::{SRGB, SRGBA}; pub mod channel; pub mod hsv; -pub mod hsva; pub mod rgb; -pub mod rgba; pub mod srgb; -pub mod srgba; -// pub trait Color: Eq { -// pub fn inverse(&self) -> Self; -// pub fn invert_self(&mut self); -// } +pub trait Color { + pub fn inverse(&self) -> Self; + pub fn invert_self(&mut self); +} diff --git a/src/color/hsv.rs b/src/color/hsv.rs index 677aaee..af3d7c8 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -14,10 +14,10 @@ // limitations under the License. use std::num; +use std::cast; -use color::{Channel, ToChannel}; -use color::{FloatChannel, ToFloatChannel}; -use color::{RGB, ToRGB}; +use color::{Channel, ToChannel, FloatChannel, ToFloatChannel}; +use color::{RGB, ToRGB, RGBA, ToRGBA}; #[path = "../num_macros.rs"] mod num_macros; @@ -75,3 +75,56 @@ impl ToRGB for HSV { rgb.to_rgb::() } } + +#[deriving(Clone, Eq)] +pub struct HSVA { h: T, s: T, v: T, a: T } + +impl HSVA { + #[inline] + pub fn new(h: T, s: T, v: T, a: T) -> HSVA { + HSVA { h: h, s: s, v: v, a: a } + } + + #[inline] + pub fn from_hsv_a(hsv: HSV, a: T) -> HSVA { + unsafe { cast::transmute((hsv, a)) } + } + + #[inline] + pub fn hsv<'a>(&'a self) -> &'a HSV { + unsafe { cast::transmute(self) } + } + + #[inline] + pub fn hsv_mut<'a>(&'a mut self) -> &'a mut HSV { + unsafe { cast::transmute(self) } + } +} + +pub trait ToHSVA { + pub fn to_hsva(&self) -> HSVA; +} + +impl ToHSVA for (C, T) { + #[inline] + pub fn to_hsva(&self) -> HSVA { + match *self { + (ref hsv, ref a) => { + HSVA::from_hsv_a( + hsv.to_hsv(), + FloatChannel::from(a.clone()) + ) + } + } + } +} + +impl ToRGBA for HSVA { + #[inline] + pub fn to_rgba(&self) -> RGBA { + RGBA::from_rgb_a( + self.hsv().to_rgb(), + Channel::from((*self).a.clone()) + ) + } +} diff --git a/src/color/hsva.rs b/src/color/hsva.rs deleted file mode 100644 index c0eb46b..0000000 --- a/src/color/hsva.rs +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::cast; - -use color::{Channel, ToChannel}; -use color::{FloatChannel, ToFloatChannel}; -use color::{HSV, ToHSV, ToRGB, RGBA, ToRGBA}; - -#[path = "../num_macros.rs"] -mod num_macros; - -#[deriving(Clone, Eq)] -pub struct HSVA { h: T, s: T, v: T, a: T } - -impl HSVA { - #[inline] - pub fn new(h: T, s: T, v: T, a: T) -> HSVA { - HSVA { h: h, s: s, v: v, a: a } - } - - #[inline] - pub fn from_hsv_a(hsv: HSV, a: T) -> HSVA { - unsafe { cast::transmute((hsv, a)) } - } - - #[inline] - pub fn hsv<'a>(&'a self) -> &'a HSV { - unsafe { cast::transmute(self) } - } - - #[inline] - pub fn hsv_mut<'a>(&'a mut self) -> &'a mut HSV { - unsafe { cast::transmute(self) } - } -} - -pub trait ToHSVA { - pub fn to_hsva(&self) -> HSVA; -} - -impl ToHSVA for (C, T) { - #[inline] - pub fn to_hsva(&self) -> HSVA { - match *self { - (ref hsv, ref a) => { - HSVA::from_hsv_a( - hsv.to_hsv(), - FloatChannel::from(a.clone()) - ) - } - } - } -} - -impl ToRGBA for HSVA { - #[inline] - pub fn to_rgba(&self) -> RGBA { - RGBA::from_rgb_a( - self.hsv().to_rgb(), - Channel::from((*self).a.clone()) - ) - } -} diff --git a/src/color/rgb.rs b/src/color/rgb.rs index 46a5337..240b0e7 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -14,9 +14,10 @@ // limitations under the License. use std::num; +use std::cast; use color::{Channel, ToChannel, FloatChannel}; -use color::{HSV, ToHSV}; +use color::{HSV, ToHSV, HSVA, ToHSVA}; #[path = "../num_macros.rs"] mod num_macros; @@ -72,3 +73,53 @@ impl ToHSV for RGB { } } } + +#[deriving(Clone, Eq)] +pub struct RGBA { r: T, g: T, b: T, a: T } + +impl RGBA { + #[inline] + pub fn new(r: T, g: T, b: T, a: T) -> RGBA { + RGBA { r: r, g: g, b: b, a: a } + } + + #[inline] + pub fn from_rgb_a(rgb: RGB, a: T) -> RGBA { + unsafe { cast::transmute((rgb, a)) } + } + + #[inline] + pub fn rgb<'a>(&'a self) -> &'a RGB { + unsafe { cast::transmute(self) } + } + + #[inline] + pub fn rgb_mut<'a>(&'a mut self) -> &'a mut RGB { + unsafe { cast::transmute(self) } + } +} + +pub trait ToRGBA { + pub fn to_rgba(&self) -> RGBA; +} + +impl ToRGBA for (C, T) { + #[inline] + pub fn to_rgba(&self) -> RGBA { + match *self { + (ref rgb, ref a) => { + RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone())) + } + } + } +} + +impl ToHSVA for RGBA { + #[inline] + pub fn to_hsva(&self) -> HSVA { + HSVA::from_hsv_a( + self.rgb().to_hsv(), + Channel::from((*self).a.clone()) + ) + } +} diff --git a/src/color/rgba.rs b/src/color/rgba.rs deleted file mode 100644 index 3450ff9..0000000 --- a/src/color/rgba.rs +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::cast; - -use color::{Channel, ToChannel, FloatChannel}; -use color::{RGB, ToRGB, ToHSV, HSVA, ToHSVA}; - -#[path = "../num_macros.rs"] -mod num_macros; - -#[deriving(Clone, Eq)] -pub struct RGBA { r: T, g: T, b: T, a: T } - -impl RGBA { - #[inline] - pub fn new(r: T, g: T, b: T, a: T) -> RGBA { - RGBA { r: r, g: g, b: b, a: a } - } - - #[inline] - pub fn from_rgb_a(rgb: RGB, a: T) -> RGBA { - unsafe { cast::transmute((rgb, a)) } - } - - #[inline] - pub fn rgb<'a>(&'a self) -> &'a RGB { - unsafe { cast::transmute(self) } - } - - #[inline] - pub fn rgb_mut<'a>(&'a mut self) -> &'a mut RGB { - unsafe { cast::transmute(self) } - } -} - -pub trait ToRGBA { - pub fn to_rgba(&self) -> RGBA; -} - -impl ToRGBA for (C, T) { - #[inline] - pub fn to_rgba(&self) -> RGBA { - match *self { - (ref rgb, ref a) => { - RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone())) - } - } - } -} - -impl ToHSVA for RGBA { - #[inline] - pub fn to_hsva(&self) -> HSVA { - HSVA::from_hsv_a( - self.rgb().to_hsv(), - Channel::from((*self).a.clone()) - ) - } -} diff --git a/src/color/srgb.rs b/src/color/srgb.rs index 1ab8dc7..6702a6f 100644 --- a/src/color/srgb.rs +++ b/src/color/srgb.rs @@ -22,3 +22,13 @@ impl SRGB { SRGB { r: r, g: g, b: b } } } + +#[deriving(Clone, Eq)] +pub struct SRGBA { r: T, g: T, b: T, a: T } + +impl SRGBA { + #[inline] + pub fn new(r: T, g: T, b: T, a: T) -> SRGBA { + SRGBA { r: r, g: g, b: b, a: a } + } +} diff --git a/src/color/srgba.rs b/src/color/srgba.rs deleted file mode 100644 index 490e17f..0000000 --- a/src/color/srgba.rs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#[deriving(Clone, Eq)] -pub struct SRGBA { r: T, g: T, b: T, a: T } - -impl SRGBA { - #[inline] - pub fn new(r: T, g: T, b: T, a: T) -> SRGBA { - SRGBA { r: r, g: g, b: b, a: a } - } -}