{-
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.HSL
 (RGB
 ,hslView
 ,hue, saturation, lightness
 ,hsl
 )
where

import Data.Colour.RGB

-- |Returns the HSL (hue-saturation-lightness) coordinates of an 'RGB' triple.
-- See 'hue', 'saturation', and 'lightness'.
hslView :: (Fractional a, Ord a) => RGB a -> (a,a,a)
hslView :: forall a. (Fractional a, Ord a) => RGB a -> (a, a, a)
hslView RGB a
rgb = (a
h,a
s,a
l)
 where
  (a
h,a
s,a
l,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 HSL
-- (hue-saturation-lightness) system.
-- Note: This is different from 'Data.Colour.RGBSpace.HSV.saturation' for
-- the "Data.Colour.RGBSpace.HSV"
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
s,a
_,a
_,a
_) = forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv RGB a
rgb

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

-- |Convert HSL (hue-saturation-lightness) coordinates to an 'RGB' value.
-- Hue is expected to be measured in degrees [0,360], while saturation and
-- lightness are expected to be in the closed range [0,1].
hsl :: (RealFrac a, Ord a) => a -> a -> a -> RGB a
hsl :: forall a. (RealFrac a, Ord a) => a -> a -> a -> RGB a
hsl a
h a
s a
l = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
component RGB a
t
 where
  hk :: a
hk = a
hforall a. Fractional a => a -> a -> a
/a
360
  tr :: a
tr = forall {a}. RealFrac a => a -> a
mod1 (a
hk forall a. Num a => a -> a -> a
+ a
1forall a. Fractional a => a -> a -> a
/a
3)
  tg :: a
tg = forall {a}. RealFrac a => a -> a
mod1 a
hk
  tb :: a
tb = forall {a}. RealFrac a => a -> a
mod1 (a
hk forall a. Num a => a -> a -> a
- a
1forall a. Fractional a => a -> a -> a
/a
3)
  t :: RGB a
t = forall a. a -> a -> a -> RGB a
RGB a
tr a
tg a
tb
  q :: a
q | a
l forall a. Ord a => a -> a -> Bool
< a
0.5 = a
lforall a. Num a => a -> a -> a
*(a
1forall a. Num a => a -> a -> a
+a
s)
    | Bool
otherwise = a
l forall a. Num a => a -> a -> a
+ a
s forall a. Num a => a -> a -> a
- a
lforall a. Num a => a -> a -> a
*a
s
  p :: a
p = a
2forall a. Num a => a -> a -> a
*a
l forall a. Num a => a -> a -> a
- a
q
  component :: a -> a
component a
t | a
t forall a. Ord a => a -> a -> Bool
< a
1forall a. Fractional a => a -> a -> a
/a
6 = a
p forall a. Num a => a -> a -> a
+ ((a
qforall a. Num a => a -> a -> a
-a
p)forall a. Num a => a -> a -> a
*a
6forall a. Num a => a -> a -> a
*a
t)
              | a
t forall a. Ord a => a -> a -> Bool
< a
1forall a. Fractional a => a -> a -> a
/a
2 = a
q
              | a
t forall a. Ord a => a -> a -> Bool
< a
2forall a. Fractional a => a -> a -> a
/a
3 = a
p forall a. Num a => a -> a -> a
+ ((a
qforall a. Num a => a -> a -> a
-a
p)forall a. Num a => a -> a -> a
*a
6forall a. Num a => a -> a -> a
*(a
2forall a. Fractional a => a -> a -> a
/a
3forall a. Num a => a -> a -> a
-a
t))
              | Bool
otherwise = a
p