{-
Copyright (c) 2008,2009
Russell O'Connor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-}

module Data.Colour.RGBSpace.HSV
 (RGB
 ,hsvView
 ,hue, saturation, value
 ,hsv
 )
where

import Data.Colour.RGB

-- |Returns the HSV (hue-saturation-value) coordinates of an 'RGB' triple.
-- See 'hue', 'saturation', and 'value'.
hsvView :: (Fractional a, Ord a) => RGB a -> (a,a,a)
hsvView :: forall a. (Fractional a, Ord a) => RGB a -> (a, a, a)
hsvView RGB a
rgb = (a
h,a
s,a
v)
 where
  (a
h,a
_,a
_,a
s,a
v) = RGB a -> (a, a, a, a, a)
forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv RGB a
rgb

-- |Returns the saturation coordinate (range [0,1]) of an 'RGB' triple for the HSV
-- (hue-saturation-value) system.
-- Note: This is different from 'Data.Colour.RGBSpace.HSL.saturation' for
-- the "Data.Colour.RGBSpace.HSL"
saturation :: (Fractional a, Ord a) => RGB a -> a
saturation :: forall a. (Fractional a, Ord a) => RGB a -> a
saturation RGB a
rgb = a
s
 where
  (a
_,a
_,a
_,a
s,a
_) = RGB a -> (a, a, a, a, a)
forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv RGB a
rgb

-- |Returns the value coordinate (raonge [0,1]) of an 'RGB' triple for the HSV
-- (hue-saturation-value) system.
value :: (Fractional a, Ord a) => RGB a -> a
value :: forall a. (Fractional a, Ord a) => RGB a -> a
value RGB a
rgb = a
v
 where
  (a
_,a
_,a
_,a
_,a
v) = RGB a -> (a, a, a, a, a)
forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv RGB a
rgb

-- |Convert HSV (hue-saturation-value) coordinates to an 'RGB' value.
-- Hue is expected to be measured in degrees [0,360], while saturation and
-- value are expected to be in the closed range [0,1].
hsv :: (RealFrac a, Ord a) => a -> a -> a -> RGB a
hsv :: forall a. (RealFrac a, Ord a) => a -> a -> a -> RGB a
hsv a
h a
s a
v = case Integer
hi of
    Integer
0 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
v a
t a
p
    Integer
1 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
q a
v a
p
    Integer
2 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
p a
v a
t
    Integer
3 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
p a
q a
v
    Integer
4 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
t a
p a
v
    Integer
5 -> a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
v a
p a
q
 where
  hi :: Integer
hi = a -> Integer
forall b. Integral b => a -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (a
ha -> a -> a
forall a. Fractional a => a -> a -> a
/a
60) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
6
  f :: a
f = a -> a
forall {a}. RealFrac a => a -> a
mod1 (a
ha -> a -> a
forall a. Fractional a => a -> a -> a
/a
60)
  p :: a
p = a
va -> a -> a
forall a. Num a => a -> a -> a
*(a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
s)
  q :: a
q = a
va -> a -> a
forall a. Num a => a -> a -> a
*(a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
fa -> a -> a
forall a. Num a => a -> a -> a
*a
s)
  t :: a
t = a
va -> a -> a
forall a. Num a => a -> a -> a
*(a
1a -> a -> a
forall a. Num a => a -> a -> a
-(a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
f)a -> a -> a
forall a. Num a => a -> a -> a
*a
s)