{-# LANGUAGE UnicodeSyntax, LambdaCase #-}
module DMenu.Color (
Color(..), showColorAsHex,
) where
import Numeric (showHex)
data Color
= HexColor Int
| RGBColor Int Int Int
| RGBColorF Float Float Float
deriving (Eq, Ord, Read, Show)
showColorAsHex :: Color → String
showColorAsHex = \case
HexColor i → "#" ++ fillLeft '0' 6 (showHex i "")
RGBColor r g b → showColorAsHex $ HexColor $ r*256*256 + g*256 + b
RGBColorF r g b → showColorAsHex $ RGBColor (f r) (f g) (f b)
where
f = floor . (* 255)
fillLeft c i s = replicate (i - length s) c ++ s