module Util.Color (Color (..),fromWXColor,toWXColor,toHexColor) where import qualified Graphics.UI.WX as WX import Util.Sexp (Sexp (..)) import Util.Sexpable (Sexpable (..), unexpected) import Numeric (showHex) newtype Color = Color (Int,Int,Int) instance Sexpable Color where toSexp (Color (r,g,b)) = List [toSexp r,toSexp g,toSexp b] fromSexp (List [red,green,blue]) = do r <- fromSexp red g <- fromSexp green b <- fromSexp blue return $ Color (r,g,b) fromSexp sexp = unexpected sexp fromWXColor :: WX.Color -> Color fromWXColor c = Color (WX.colorRed c, WX.colorGreen c, WX.colorBlue c) toWXColor :: Color -> WX.Color toWXColor (Color (r,g,b)) = WX.rgb r g b toHexColor :: Color -> String toHexColor (Color (r,g,b)) = let hex x = case showHex x "" of [y] -> ['0',y] y -> y in concat ["#", hex r, hex g, hex b]