{-# 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.HSI
-- 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.HSI
  ( pattern ColorHSI
  , pattern ColorHSIA
  , pattern ColorH360SI
  , HSI
  , Color(HSI)
  ) where

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


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

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

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

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

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

-- | Constructor for @HSI@ with alpha channel.
pattern ColorHSIA :: e -> e -> e -> e -> Color (Alpha (HSI cs)) e
pattern $bColorHSIA :: e -> e -> e -> e -> Color (Alpha (HSI cs)) e
$mColorHSIA :: forall r k e (cs :: k).
Color (Alpha (HSI cs)) e
-> (e -> e -> e -> e -> r) -> (Void# -> r) -> r
ColorHSIA h s i a = Alpha (HSI (CM.ColorHSI h s i)) a
{-# COMPLETE ColorHSIA #-}


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

-- | `HSI` representation for some (@`RedGreenBlue` cs i@) color space
instance ColorModel cs e => ColorModel (HSI cs) e where
  type Components (HSI cs) e = (e, e, e)
  toComponents :: Color (HSI cs) e -> Components (HSI cs) e
toComponents = Color HSI e -> (e, e, e)
forall cs e. ColorModel cs e => Color cs e -> Components cs e
toComponents (Color HSI e -> (e, e, e))
-> (Color (HSI cs) e -> Color HSI e)
-> Color (HSI cs) e
-> (e, e, e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSI cs) e -> Color HSI e
coerce
  {-# INLINE toComponents #-}
  fromComponents :: Components (HSI cs) e -> Color (HSI cs) e
fromComponents = Color HSI e -> Color (HSI cs) e
coerce (Color HSI e -> Color (HSI cs) e)
-> ((e, e, e) -> Color HSI e) -> (e, e, e) -> Color (HSI cs) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e, e, e) -> Color HSI e
forall cs e. ColorModel cs e => Components cs e -> Color cs e
fromComponents
  {-# INLINE fromComponents #-}
  showsColorModelName :: Proxy (Color (HSI cs) e) -> ShowS
showsColorModelName Proxy (Color (HSI cs) e)
_ = (String
"HSI-" 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))


-- | `HSI` representation for some (@`RedGreenBlue` cs i@) color space
instance (ColorSpace (cs l) i e, RedGreenBlue cs i) => ColorSpace (HSI (cs l)) i e where
  type BaseModel (HSI (cs l)) = CM.HSI
  type BaseSpace (HSI (cs l)) = cs l
  toBaseSpace :: Color (HSI (cs l)) e -> Color (BaseSpace (HSI (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 (HSI (cs l)) e -> Color RGB e)
-> Color (HSI (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 (HSI (cs l)) e -> Color RGB Double)
-> Color (HSI (cs l)) e
-> Color RGB e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color HSI Double -> Color RGB Double
forall e. (Ord e, Floating e) => Color HSI e -> Color RGB e
CM.hsi2rgb (Color HSI Double -> Color RGB Double)
-> (Color (HSI (cs l)) e -> Color HSI Double)
-> Color (HSI (cs l)) e
-> Color RGB Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> Double) -> Color HSI e -> Color HSI Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap e -> Double
forall e. Elevator e => e -> Double
toDouble (Color HSI e -> Color HSI Double)
-> (Color (HSI (cs l)) e -> Color HSI e)
-> Color (HSI (cs l)) e
-> Color HSI Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSI (cs l)) e -> Color HSI e
coerce
  {-# INLINE toBaseSpace #-}
  fromBaseSpace :: Color (BaseSpace (HSI (cs l))) e -> Color (HSI (cs l)) e
fromBaseSpace = Color HSI e -> Color (HSI (cs l)) e
coerce (Color HSI e -> Color (HSI (cs l)) e)
-> (Color (cs l) e -> Color HSI e)
-> Color (cs l) e
-> Color (HSI (cs l)) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> e) -> Color HSI Double -> Color HSI e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> e
forall e. Elevator e => Double -> e
fromDouble (Color HSI Double -> Color HSI e)
-> (Color (cs l) e -> Color HSI Double)
-> Color (cs l) e
-> Color HSI e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color RGB Double -> Color HSI Double
forall e. RealFloat e => Color RGB e -> Color HSI e
CM.rgb2hsi (Color RGB Double -> Color HSI Double)
-> (Color (cs l) e -> Color RGB Double)
-> Color (cs l) e
-> Color HSI 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 (HSI (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 (HSI (cs l)) e -> Color (cs l) e)
-> Color (HSI (cs l)) e
-> Color (Y i) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (HSI (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 #-}