{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
-- |
-- Module      : Graphics.Color.Space.RGB.Alternative.HSL
-- Copyright   : (c) Alexey Kuleshevich 2019-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Graphics.Color.Space.RGB.Alternative.HSL
  ( pattern ColorHSL
  , pattern ColorHSLA
  , pattern ColorH360SL
  , HSL
  , Color(HSL)
  ) where

import Data.Coerce
import Data.Proxy
import Foreign.Storable
import qualified Graphics.Color.Model.HSL as CM
import Graphics.Color.Model.Internal
import Graphics.Color.Space.Internal
import Graphics.Color.Space.RGB.Internal

-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
data HSL cs

-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
newtype instance Color (HSL cs) e = HSL (Color CM.HSL e)

-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Eq e => Eq (Color (HSL cs) e)
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Ord e => Ord (Color (HSL cs) e)
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Functor (Color (HSL cs))
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Applicative (Color (HSL cs))
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Foldable (Color (HSL cs))
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Traversable (Color (HSL cs))
-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
deriving instance Storable e => Storable (Color (HSL cs) e)

-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
instance ColorModel cs e => Show (Color (HSL cs) e) where
  showsPrec :: Int -> Color (HSL cs) e -> ShowS
showsPrec Int
_ = Color (HSL cs) e -> ShowS
forall cs e. ColorModel cs e => Color cs e -> ShowS
showsColorModel

-- | Constructor for an RGB color space in an alternative HSL color model
pattern ColorHSL :: e -> e -> e -> Color (HSL cs) e
pattern $bColorHSL :: e -> e -> e -> Color (HSL cs) e
$mColorHSL :: forall r k e (cs :: k).
Color (HSL cs) e -> (e -> e -> e -> r) -> (Void# -> r) -> r
ColorHSL h s i = HSL (CM.ColorHSL h s i)
{-# COMPLETE ColorHSL #-}

-- | Constructor for @HSL@ with alpha channel.
pattern ColorHSLA :: e -> e -> e -> e -> Color (Alpha (HSL cs)) e
pattern $bColorHSLA :: e -> e -> e -> e -> Color (Alpha (HSL cs)) e
$mColorHSLA :: forall r k e (cs :: k).
Color (Alpha (HSL cs)) e
-> (e -> e -> e -> e -> r) -> (Void# -> r) -> r
ColorHSLA h s i a = Alpha (HSL (CM.ColorHSL h s i)) a
{-# COMPLETE ColorHSLA #-}


-- | Constructor for an RGB color space in an alternative HSL color model. Difference from
-- `ColorHSL` is that the hue is specified in 0 to 360 degree range, rather than 0 to
-- 1. Note, that this is not checked.
pattern ColorH360SL :: Fractional e => e -> e -> e -> Color (HSL cs) e
pattern $bColorH360SL :: e -> e -> e -> Color (HSL cs) e
$mColorH360SL :: forall r k e (cs :: k).
Fractional e =>
Color (HSL cs) e -> (e -> e -> e -> r) -> (Void# -> r) -> r
ColorH360SL h s i <- ColorHSL ((* 360) -> h) s i where
        ColorH360SL e
h e
s e
i = e -> e -> e -> Color (HSL cs) e
forall k e (cs :: k). e -> e -> e -> Color (HSL cs) e
ColorHSL (e
h e -> e -> e
forall a. Fractional a => a -> a -> a
/ e
360) e
s e
i
{-# COMPLETE ColorH360SL #-}

-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
instance ColorModel cs e => ColorModel (HSL cs) e where
  type Components (HSL cs) e = (e, e, e)
  toComponents :: Color (HSL cs) e -> Components (HSL cs) e
toComponents = Color HSL e -> (e, e, e)
forall cs e. ColorModel cs e => Color cs e -> Components cs e
toComponents (Color HSL e -> (e, e, e))
-> (Color (HSL cs) e -> Color HSL e)
-> Color (HSL cs) e
-> (e, e, e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSL cs) e -> Color HSL e
coerce
  {-# INLINE toComponents #-}
  fromComponents :: Components (HSL cs) e -> Color (HSL cs) e
fromComponents = Color HSL e -> Color (HSL cs) e
coerce (Color HSL e -> Color (HSL cs) e)
-> ((e, e, e) -> Color HSL e) -> (e, e, e) -> Color (HSL cs) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e, e, e) -> Color HSL e
forall cs e. ColorModel cs e => Components cs e -> Color cs e
fromComponents
  {-# INLINE fromComponents #-}
  showsColorModelName :: Proxy (Color (HSL cs) e) -> ShowS
showsColorModelName Proxy (Color (HSL cs) e)
_ = (String
"HSL-" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy (Color cs e) -> ShowS
forall cs e. ColorModel cs e => Proxy (Color cs e) -> ShowS
showsColorModelName (Proxy (Color cs e)
forall k (t :: k). Proxy t
Proxy :: Proxy (Color cs e))


-- | `HSL` representation for some (@`RedGreenBlue` cs i@) color space
instance (ColorSpace (cs l) i e, RedGreenBlue cs i) => ColorSpace (HSL (cs l)) i e where
  type BaseModel (HSL (cs l)) = CM.HSL
  type BaseSpace (HSL (cs l)) = cs l
  toBaseSpace :: Color (HSL (cs l)) e -> Color (BaseSpace (HSL (cs l))) e
toBaseSpace = Color RGB e -> Color (cs l) e
forall k (cs :: Linearity -> *) (i :: k) e (l :: Linearity).
RedGreenBlue cs i =>
Color RGB e -> Color (cs l) e
mkColorRGB (Color RGB e -> Color (cs l) e)
-> (Color (HSL (cs l)) e -> Color RGB e)
-> Color (HSL (cs l)) e
-> Color (cs l) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> e) -> Color RGB Double -> Color RGB e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> e
forall e. Elevator e => Double -> e
fromDouble (Color RGB Double -> Color RGB e)
-> (Color (HSL (cs l)) e -> Color RGB Double)
-> Color (HSL (cs l)) e
-> Color RGB e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color HSL Double -> Color RGB Double
forall e. RealFrac e => Color HSL e -> Color RGB e
CM.hsl2rgb (Color HSL Double -> Color RGB Double)
-> (Color (HSL (cs l)) e -> Color HSL Double)
-> Color (HSL (cs l)) e
-> Color RGB Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> Double) -> Color HSL e -> Color HSL Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap e -> Double
forall e. Elevator e => e -> Double
toDouble (Color HSL e -> Color HSL Double)
-> (Color (HSL (cs l)) e -> Color HSL e)
-> Color (HSL (cs l)) e
-> Color HSL Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSL (cs l)) e -> Color HSL e
coerce
  {-# INLINE toBaseSpace #-}
  fromBaseSpace :: Color (BaseSpace (HSL (cs l))) e -> Color (HSL (cs l)) e
fromBaseSpace = Color HSL e -> Color (HSL (cs l)) e
coerce (Color HSL e -> Color (HSL (cs l)) e)
-> (Color (cs l) e -> Color HSL e)
-> Color (cs l) e
-> Color (HSL (cs l)) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> e) -> Color HSL Double -> Color HSL e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> e
forall e. Elevator e => Double -> e
fromDouble (Color HSL Double -> Color HSL e)
-> (Color (cs l) e -> Color HSL Double)
-> Color (cs l) e
-> Color HSL e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color RGB Double -> Color HSL Double
forall e. (Ord e, Floating e) => Color RGB e -> Color HSL e
CM.rgb2hsl (Color RGB Double -> Color HSL Double)
-> (Color (cs l) e -> Color RGB Double)
-> Color (cs l) e
-> Color HSL Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> Double) -> Color RGB e -> Color RGB Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap e -> Double
forall e. Elevator e => e -> Double
toDouble (Color RGB e -> Color RGB Double)
-> (Color (cs l) e -> Color RGB e)
-> Color (cs l) e
-> Color RGB Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (cs l) e -> Color RGB e
forall k (cs :: Linearity -> *) (i :: k) (l :: Linearity) e.
RedGreenBlue cs i =>
Color (cs l) e -> Color RGB e
unColorRGB
  {-# INLINE fromBaseSpace #-}
  luminance :: Color (HSL (cs l)) e -> Color (Y i) a
luminance = Color (cs l) e -> Color (Y i) a
forall k cs (i :: k) e a.
(ColorSpace cs i e, Elevator a, RealFloat a) =>
Color cs e -> Color (Y i) a
luminance (Color (cs l) e -> Color (Y i) a)
-> (Color (HSL (cs l)) e -> Color (cs l) e)
-> Color (HSL (cs l)) e
-> Color (Y i) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSL (cs l)) e -> Color (cs l) e
forall k cs (i :: k) e.
(ColorSpace cs i e, ColorSpace (BaseSpace cs) i e) =>
Color cs e -> Color (BaseSpace cs) e
toBaseSpace
  {-# INLINE luminance #-}