{-# 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.HSV
-- 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.HSV
  ( pattern ColorHSV
  , pattern ColorHSVA
  , pattern ColorH360SV
  , HSV
  , Color(HSV)
  ) where

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

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

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

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

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

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

-- | Constructor for @HSV@ with alpha channel.
pattern ColorHSVA :: e -> e -> e -> e -> Color (Alpha (HSV cs)) e
pattern $bColorHSVA :: e -> e -> e -> e -> Color (Alpha (HSV cs)) e
$mColorHSVA :: forall r k e (cs :: k).
Color (Alpha (HSV cs)) e
-> (e -> e -> e -> e -> r) -> (Void# -> r) -> r
ColorHSVA h s i a = Alpha (HSV (CM.ColorHSV h s i)) a
{-# COMPLETE ColorHSVA #-}


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

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


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