module Data.Prizm.Color where
import Control.Applicative
import Data.Prizm.Types
pct :: Integer -> Double
pct = (/100) . fromIntegral . (max 0) . (min 100)
pctClamp :: Integer -> Integer
pctClamp i = max (min i 100) 0
clamp :: Double -> Double -> Double
clamp i clmp = max (min i clmp) 0.0
(<|>) :: (CIELCH Double, CIELCH Double) -> CIELCH Double
(<|>) = interpolate 50
shade :: CIELCH Double -> Percent -> CIELCH Double
shade c w = interpolate (pctClamp w) (c, CIELCH 0.0 0.0 360.0)
tint :: CIELCH Double -> Percent -> CIELCH Double
tint c w = interpolate (pctClamp w) (c, CIELCH 100.0 0.0 360.0)
darken :: CIELCH Double -> Percent -> CIELCH Double
darken (CIELCH l c h) w = (CIELCH (l (l*(pct (pctClamp w)))) c h)
lighten :: CIELCH Double -> Percent -> CIELCH Double
lighten (CIELCH l c h) w = (CIELCH (l + (l*(pct (pctClamp w)))) c h)
interpolate :: Percent -> (CIELCH Double, CIELCH Double) -> CIELCH Double
interpolate w (a,b) =
let w' = (pct (pctClamp w))
(CIELCH l c h) = () <$> b <*> a
a' = (*w') <$> (CIELCH l c (shortestPath h))
in (+) <$> a' <*> a
shortestPath :: Double -> Double
shortestPath h | h > 180 = h 360
| h < (180) = h + 360
| otherwise = h