Merge some color modules
This commit is contained in:
parent
29a99cea51
commit
49715e6479
7 changed files with 125 additions and 189 deletions
|
@ -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<T>: Eq {
|
||||
// pub fn inverse(&self) -> Self;
|
||||
// pub fn invert_self(&mut self);
|
||||
// }
|
||||
pub trait Color<T> {
|
||||
pub fn inverse(&self) -> Self;
|
||||
pub fn invert_self(&mut self);
|
||||
}
|
||||
|
|
|
@ -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<T:Clone + Float + ToChannel> ToRGB for HSV<T> {
|
|||
rgb.to_rgb::<U>()
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct HSVA<T> { h: T, s: T, v: T, a: T }
|
||||
|
||||
impl<T> HSVA<T> {
|
||||
#[inline]
|
||||
pub fn new(h: T, s: T, v: T, a: T) -> HSVA<T> {
|
||||
HSVA { h: h, s: s, v: v, a: a }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_hsv_a(hsv: HSV<T>, a: T) -> HSVA<T> {
|
||||
unsafe { cast::transmute((hsv, a)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hsv<'a>(&'a self) -> &'a HSV<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hsv_mut<'a>(&'a mut self) -> &'a mut HSV<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToHSVA {
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U>;
|
||||
}
|
||||
|
||||
impl<C: ToHSV, T:Clone + ToFloatChannel> ToHSVA for (C, T) {
|
||||
#[inline]
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
|
||||
match *self {
|
||||
(ref hsv, ref a) => {
|
||||
HSVA::from_hsv_a(
|
||||
hsv.to_hsv(),
|
||||
FloatChannel::from(a.clone())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Clone + Float + ToChannel> ToRGBA for HSVA<T> {
|
||||
#[inline]
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
|
||||
RGBA::from_rgb_a(
|
||||
self.hsv().to_rgb(),
|
||||
Channel::from((*self).a.clone())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> { h: T, s: T, v: T, a: T }
|
||||
|
||||
impl<T> HSVA<T> {
|
||||
#[inline]
|
||||
pub fn new(h: T, s: T, v: T, a: T) -> HSVA<T> {
|
||||
HSVA { h: h, s: s, v: v, a: a }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_hsv_a(hsv: HSV<T>, a: T) -> HSVA<T> {
|
||||
unsafe { cast::transmute((hsv, a)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hsv<'a>(&'a self) -> &'a HSV<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hsv_mut<'a>(&'a mut self) -> &'a mut HSV<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToHSVA {
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U>;
|
||||
}
|
||||
|
||||
impl<C: ToHSV, T:Clone + ToFloatChannel> ToHSVA for (C, T) {
|
||||
#[inline]
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
|
||||
match *self {
|
||||
(ref hsv, ref a) => {
|
||||
HSVA::from_hsv_a(
|
||||
hsv.to_hsv(),
|
||||
FloatChannel::from(a.clone())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Clone + Float + ToChannel> ToRGBA for HSVA<T> {
|
||||
#[inline]
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
|
||||
RGBA::from_rgb_a(
|
||||
self.hsv().to_rgb(),
|
||||
Channel::from((*self).a.clone())
|
||||
)
|
||||
}
|
||||
}
|
|
@ -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<T:Clone + ToChannel> ToHSV for RGB<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct RGBA<T> { r: T, g: T, b: T, a: T }
|
||||
|
||||
impl<T> RGBA<T> {
|
||||
#[inline]
|
||||
pub fn new(r: T, g: T, b: T, a: T) -> RGBA<T> {
|
||||
RGBA { r: r, g: g, b: b, a: a }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_rgb_a(rgb: RGB<T>, a: T) -> RGBA<T> {
|
||||
unsafe { cast::transmute((rgb, a)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rgb<'a>(&'a self) -> &'a RGB<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rgb_mut<'a>(&'a mut self) -> &'a mut RGB<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToRGBA {
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U>;
|
||||
}
|
||||
|
||||
impl<C: ToRGB, T:Clone + ToChannel> ToRGBA for (C, T) {
|
||||
#[inline]
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
|
||||
match *self {
|
||||
(ref rgb, ref a) => {
|
||||
RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Clone + ToChannel> ToHSVA for RGBA<T> {
|
||||
#[inline]
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
|
||||
HSVA::from_hsv_a(
|
||||
self.rgb().to_hsv(),
|
||||
Channel::from((*self).a.clone())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> { r: T, g: T, b: T, a: T }
|
||||
|
||||
impl<T> RGBA<T> {
|
||||
#[inline]
|
||||
pub fn new(r: T, g: T, b: T, a: T) -> RGBA<T> {
|
||||
RGBA { r: r, g: g, b: b, a: a }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_rgb_a(rgb: RGB<T>, a: T) -> RGBA<T> {
|
||||
unsafe { cast::transmute((rgb, a)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rgb<'a>(&'a self) -> &'a RGB<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rgb_mut<'a>(&'a mut self) -> &'a mut RGB<T> {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToRGBA {
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U>;
|
||||
}
|
||||
|
||||
impl<C: ToRGB, T:Clone + ToChannel> ToRGBA for (C, T) {
|
||||
#[inline]
|
||||
pub fn to_rgba<U:Clone + Channel>(&self) -> RGBA<U> {
|
||||
match *self {
|
||||
(ref rgb, ref a) => {
|
||||
RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Clone + ToChannel> ToHSVA for RGBA<T> {
|
||||
#[inline]
|
||||
pub fn to_hsva<U:Clone + FloatChannel>(&self) -> HSVA<U> {
|
||||
HSVA::from_hsv_a(
|
||||
self.rgb().to_hsv(),
|
||||
Channel::from((*self).a.clone())
|
||||
)
|
||||
}
|
||||
}
|
|
@ -22,3 +22,13 @@ impl<T> SRGB<T> {
|
|||
SRGB { r: r, g: g, b: b }
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct SRGBA<T> { r: T, g: T, b: T, a: T }
|
||||
|
||||
impl<T> SRGBA<T> {
|
||||
#[inline]
|
||||
pub fn new(r: T, g: T, b: T, a: T) -> SRGBA<T> {
|
||||
SRGBA { r: r, g: g, b: b, a: a }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> { r: T, g: T, b: T, a: T }
|
||||
|
||||
impl<T> SRGBA<T> {
|
||||
#[inline]
|
||||
pub fn new(r: T, g: T, b: T, a: T) -> SRGBA<T> {
|
||||
SRGBA { r: r, g: g, b: b, a: a }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue