module Graphics.Caramia.Color
(
Color()
, rgba
, rgbaL
, redL
, greenL
, blueL
, alphaL
, viewRgba
, viewRed
, viewGreen
, viewBlue
, viewAlpha )
where
import Graphics.Caramia.Prelude
import Graphics.Caramia.Internal.Lens
import Foreign.Storable
import Foreign.Ptr
data Color = Color
{ viewRed :: !Float
, viewGreen :: !Float
, viewBlue :: !Float
, viewAlpha :: !Float
}
deriving ( Eq, Ord, Show, Read, Typeable )
rgba :: Float -> Float -> Float -> Float -> Color
rgba = Color
viewRgba :: Color -> (Float, Float, Float, Float)
viewRgba (Color r g b a) = (r, g, b, a)
rgbaL :: Lens' Color (Float, Float, Float, Float)
rgbaL = lens viewRgba (\old (r, g, b, a) ->
old { viewRed = r
, viewGreen = g
, viewBlue = b
, viewAlpha = a })
redL :: Lens' Color Float
redL = lens viewRed (\old new -> old { viewRed = new })
greenL :: Lens' Color Float
greenL = lens viewGreen (\old new -> old { viewGreen = new })
blueL :: Lens' Color Float
blueL = lens viewBlue (\old new -> old { viewBlue = new })
alphaL :: Lens' Color Float
alphaL = lens viewAlpha (\old new -> old { viewAlpha = new })
instance Storable Color where
sizeOf _ = sizeOf (undefined :: Float) * 4
alignment _ = alignment (undefined :: Float) * 4
peek ptr = do
r <- peekElemOff cptr 0 :: IO Float
g <- peekElemOff cptr 1 :: IO Float
b <- peekElemOff cptr 2 :: IO Float
a <- peekElemOff cptr 3 :: IO Float
return $ Color r g b a
where
cptr = castPtr ptr :: Ptr Float
poke ptr (Color r g b a) = do
pokeElemOff cptr 0 r
pokeElemOff cptr 1 g
pokeElemOff cptr 2 b
pokeElemOff cptr 3 a
where
cptr = castPtr ptr :: Ptr Float