module Graphics.Image.ColorSpace.HSI (
HSI(..), HSIA(..), Pixel(..),
ToHSI(..), ToHSIA(..)
) where
import Prelude hiding (map)
import Graphics.Image.Interface
import Data.Typeable (Typeable)
import qualified Data.Monoid as M (mappend)
import qualified Data.Colour as C
import qualified Data.Colour.Names as C
data HSI = HueHSI
| SatHSI
| IntHSI
deriving (Eq, Enum, Typeable)
data HSIA = HueHSIA
| SatHSIA
| IntHSIA
| AlphaHSIA
deriving (Eq, Enum, Typeable)
class ColorSpace cs => ToHSI cs where
toPixelHSI :: Pixel cs Double -> Pixel HSI Double
toImageHSI :: (Array arr cs Double, Array arr HSI Double) =>
Image arr cs Double
-> Image arr HSI Double
toImageHSI = map toPixelHSI
class (ToHSI (Opaque cs), Alpha cs) => ToHSIA cs where
toPixelHSIA :: Pixel cs Double -> Pixel HSIA Double
toPixelHSIA px = addAlpha (getAlpha px) (toPixelHSI (dropAlpha px))
toImageHSIA :: (Array arr cs Double, Array arr HSIA Double) =>
Image arr cs Double
-> Image arr HSIA Double
toImageHSIA = map toPixelHSIA
instance ColorSpace HSI where
type PixelElt HSI e = (e, e, e)
data Pixel HSI e = PixelHSI !e !e !e deriving Eq
fromChannel !e = PixelHSI e e e
fromElt !(h, s, i) = PixelHSI h s i
toElt (PixelHSI h s i) = (h, s, i)
getPxCh (PixelHSI h _ _) HueHSI = h
getPxCh (PixelHSI _ s _) SatHSI = s
getPxCh (PixelHSI _ _ i) IntHSI = i
chOp !f (PixelHSI h s i) = PixelHSI (f HueHSI h) (f SatHSI s) (f IntHSI i)
pxOp !f (PixelHSI h s i) = PixelHSI (f h) (f s) (f i)
chApp (PixelHSI fh fs fi) (PixelHSI h s i) = PixelHSI (fh h) (fs s) (fi i)
pxFoldMap f (PixelHSI h s i) = f h `M.mappend` f s `M.mappend` f i
csColour HueHSI = C.opaque C.purple
csColour SatHSI = C.opaque C.orange
csColour IntHSI = C.opaque C.darkblue
instance ColorSpace HSIA where
type PixelElt HSIA e = (e, e, e, e)
data Pixel HSIA e = PixelHSIA !e !e !e !e deriving Eq
fromChannel !e = PixelHSIA e e e e
fromElt (h, s, i, a) = PixelHSIA h s i a
toElt (PixelHSIA h s i a) = (h, s, i, a)
getPxCh (PixelHSIA r _ _ _) HueHSIA = r
getPxCh (PixelHSIA _ g _ _) SatHSIA = g
getPxCh (PixelHSIA _ _ b _) IntHSIA = b
getPxCh (PixelHSIA _ _ _ a) AlphaHSIA = a
chOp !f (PixelHSIA h s i a) =
PixelHSIA (f HueHSIA h) (f SatHSIA s) (f IntHSIA i) (f AlphaHSIA a)
pxOp !f (PixelHSIA h s i a) = PixelHSIA (f h) (f s) (f i) (f a)
chApp (PixelHSIA fh fs fi fa) (PixelHSIA h s i a) = PixelHSIA (fh h) (fs s) (fi i) (fa a)
pxFoldMap f (PixelHSIA h s i a) = f h `M.mappend` f s `M.mappend` f i `M.mappend` f a
csColour AlphaHSIA = C.opaque C.gray
csColour ch = csColour $ opaque ch
instance Alpha HSIA where
type Opaque HSIA = HSI
getAlpha (PixelHSIA _ _ _ a) = a
addAlpha !a (PixelHSI h s i) = PixelHSIA h s i a
dropAlpha (PixelHSIA h s i _) = PixelHSI h s i
opaque HueHSIA = HueHSI
opaque SatHSIA = SatHSI
opaque IntHSIA = IntHSI
opaque _ = error "Data.Image.ColorSpace.HSI (Alpha.opaque)"
instance Show HSI where
show HueHSI = "Hue"
show SatHSI = "Saturation"
show IntHSI = "Intensity"
instance Show HSIA where
show AlphaHSIA = "Alpha"
show ch = show $ opaque ch
instance Show e => Show (Pixel HSI e) where
show (PixelHSI h s i) = "<HSI:("++show h++"|"++show s++"|"++show i++")>"
instance Show e => Show (Pixel HSIA e) where
show (PixelHSIA h s i a) = "<HSIA:("++show h++"|"++show s++"|"++show i++"|"++show a++")>"