cgmath/src/color/hsv.rs

131 lines
3.7 KiB
Rust
Raw Normal View History

2013-07-08 07:39:33 +00:00
// 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::num;
2013-07-09 11:28:23 +00:00
use std::cast;
2013-07-08 07:39:33 +00:00
2013-07-09 12:50:20 +00:00
use color::Channel;
2013-07-09 11:28:23 +00:00
use color::{RGB, ToRGB, RGBA, ToRGBA};
2013-07-08 07:39:33 +00:00
#[path = "../num_macros.rs"]
mod num_macros;
#[deriving(Clone, Eq)]
pub struct HSV<T> { h: T, s: T, v: T }
2013-07-09 13:10:59 +00:00
impl<T:Channel + Float> HSV<T> {
2013-07-08 07:39:33 +00:00
pub fn new(h: T, s: T, v: T) -> HSV<T> {
HSV { h: h, s: s, v: v }
}
}
2013-07-09 06:42:19 +00:00
pub trait ToHSV {
2013-07-09 13:10:59 +00:00
pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U>;
2013-07-08 07:39:33 +00:00
}
2013-07-09 13:10:59 +00:00
impl<T:Clone + Channel + Float> ToHSV for HSV<T> {
2013-07-09 06:42:19 +00:00
#[inline]
2013-07-09 13:10:59 +00:00
pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U> {
2013-07-09 12:50:20 +00:00
HSV::new(Channel::from((*self).h.clone()),
Channel::from((*self).s.clone()),
Channel::from((*self).v.clone()))
2013-07-09 06:42:19 +00:00
}
}
2013-07-08 07:39:33 +00:00
2013-07-09 13:10:59 +00:00
impl<T:Clone + Channel + Float> ToRGB for HSV<T> {
pub fn to_rgb<U:Channel>(&self) -> RGB<U> {
// Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
2013-07-08 07:39:33 +00:00
let chr = (*self).v * (*self).s;
let h = (*self).h / num::cast(60);
2013-07-08 07:39:33 +00:00
// the 2nd largest component
let x = chr * (one!(T) - ((h % two!(T)) - one!(T)).abs());
2013-07-08 07:39:33 +00:00
let mut rgb = cond! (
(h < num::cast(1)) { RGB::new(chr.clone(), x, zero!(T)) }
(h < num::cast(2)) { RGB::new(x, chr.clone(), zero!(T)) }
(h < num::cast(3)) { RGB::new(zero!(T), chr.clone(), x) }
(h < num::cast(4)) { RGB::new(zero!(T), x, chr.clone()) }
(h < num::cast(5)) { RGB::new(x, zero!(T), chr.clone()) }
(h < num::cast(6)) { RGB::new(chr.clone(), zero!(T), x) }
_ { RGB::new(zero!(T), zero!(T), zero!(T)) }
);
2013-07-08 07:39:33 +00:00
// match the value by adding the same amount to each component
let mn = (*self).v - chr;
2013-07-08 07:39:33 +00:00
rgb.r = rgb.r + mn;
rgb.g = rgb.g + mn;
rgb.b = rgb.b + mn;
2013-07-09 06:42:19 +00:00
rgb.to_rgb::<U>()
}
2013-07-08 07:39:33 +00:00
}
2013-07-09 11:28:23 +00:00
#[deriving(Clone, Eq)]
pub struct HSVA<T> { h: T, s: T, v: T, a: T }
2013-07-09 13:10:59 +00:00
impl<T:Channel + Float> HSVA<T> {
2013-07-09 11:28:23 +00:00
#[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 {
2013-07-09 13:10:59 +00:00
pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U>;
2013-07-09 11:28:23 +00:00
}
2013-07-09 13:10:59 +00:00
impl<C: ToHSV, T:Clone + Channel + Float> ToHSVA for (C, T) {
2013-07-09 11:28:23 +00:00
#[inline]
2013-07-09 13:10:59 +00:00
pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U> {
2013-07-09 11:28:23 +00:00
match *self {
(ref hsv, ref a) => {
HSVA::from_hsv_a(
hsv.to_hsv(),
2013-07-09 12:50:20 +00:00
Channel::from(a.clone())
2013-07-09 11:28:23 +00:00
)
}
}
}
}
2013-07-09 13:10:59 +00:00
impl<T:Clone + Channel + Float> ToRGBA for HSVA<T> {
2013-07-09 11:28:23 +00:00
#[inline]
2013-07-09 13:10:59 +00:00
pub fn to_rgba<U:Channel>(&self) -> RGBA<U> {
2013-07-09 11:28:23 +00:00
RGBA::from_rgb_a(
self.hsv().to_rgb(),
Channel::from((*self).a.clone())
)
}
}