module Graphics.Gloss.Data.Color
(
Color
, makeColor
, makeColor'
, makeColor8
, rawColor
, rgbaOfColor
, mixColors
, addColors
, dim, bright
, light, dark
, greyN, black, white
, red, green, blue
, yellow, cyan, magenta
, rose, violet, azure, aquamarine, chartreuse, orange
)
where
data Color
= RGBA !Float !Float !Float !Float
deriving (Show, Eq)
instance Num Color where
(+) (RGBA r1 g1 b1 _) (RGBA r2 g2 b2 _)
= RGBA (r1 + r2) (g1 + g2) (b1 + b2) 1
() (RGBA r1 g1 b1 _) (RGBA r2 g2 b2 _)
= RGBA (r1 r2) (g1 g2) (b1 b2) 1
(*) (RGBA r1 g1 b1 _) (RGBA r2 g2 b2 _)
= RGBA (r1 * r2) (g1 * g2) (b1 * b2) 1
abs (RGBA r1 g1 b1 _)
= RGBA (abs r1) (abs g1) (abs b1) 1
signum (RGBA r1 g1 b1 _)
= RGBA (signum r1) (signum g1) (signum b1) 1
fromInteger i
= let f = fromInteger i
in RGBA f f f 1
makeColor
:: Float
-> Float
-> Float
-> Float
-> Color
makeColor r g b a
= clampColor
$ RGBA r g b a
makeColor' :: Float -> Float -> Float -> Float -> Color
makeColor' r g b a
= RGBA r g b a
makeColor8
:: Int
-> Int
-> Int
-> Int
-> Color
makeColor8 r g b a
= clampColor
$ RGBA (fromIntegral r / 255)
(fromIntegral g / 255)
(fromIntegral b / 255)
(fromIntegral a / 255)
rgbaOfColor :: Color -> (Float, Float, Float, Float)
rgbaOfColor (RGBA r g b a) = (r, g, b, a)
rawColor
:: Float
-> Float
-> Float
-> Float
-> Color
rawColor = RGBA
clampColor :: Color -> Color
clampColor cc
= let (r, g, b, a) = rgbaOfColor cc
in RGBA (min 1 r) (min 1 g) (min 1 b) (min 1 a)
normaliseColor :: Color -> Color
normaliseColor cc
= let (r, g, b, a) = rgbaOfColor cc
m = maximum [r, g, b]
in RGBA (r / m) (g / m) (b / m) a
mixColors
:: Float
-> Float
-> Color
-> Color
-> Color
mixColors ratio1 ratio2 c1 c2
= let RGBA r1 g1 b1 a1 = c1
RGBA r2 g2 b2 a2 = c2
total = ratio1 + ratio2
m1 = ratio1 / total
m2 = ratio2 / total
in RGBA (m1 * r1 + m2 * r2)
(m1 * g1 + m2 * g2)
(m1 * b1 + m2 * b2)
(m1 * a1 + m2 * a2)
addColors :: Color -> Color -> Color
addColors c1 c2
= let RGBA r1 g1 b1 a1 = c1
RGBA r2 g2 b2 a2 = c2
in normaliseColor
$ RGBA (r1 + r2)
(g1 + g2)
(b1 + b2)
((a1 + a2) / 2)
dim :: Color -> Color
dim (RGBA r g b a)
= RGBA (r / 1.2) (g / 1.2) (b / 1.2) a
bright :: Color -> Color
bright (RGBA r g b a)
= clampColor
$ RGBA (r * 1.2) (g * 1.2) (b * 1.2) a
light :: Color -> Color
light (RGBA r g b a)
= clampColor
$ RGBA (r + 0.2) (g + 0.2) (b + 0.2) a
dark :: Color -> Color
dark (RGBA r g b a)
= clampColor
$ RGBA (r 0.2) (g 0.2) (b 0.2) a
greyN :: Float
-> Color
greyN n = RGBA n n n 1.0
black, white :: Color
black = RGBA 0.0 0.0 0.0 1.0
white = RGBA 1.0 1.0 1.0 1.0
red, green, blue :: Color
red = RGBA 1.0 0.0 0.0 1.0
green = RGBA 0.0 1.0 0.0 1.0
blue = RGBA 0.0 0.0 1.0 1.0
yellow, cyan, magenta :: Color
yellow = addColors red green
cyan = addColors green blue
magenta = addColors red blue
rose, violet, azure, aquamarine, chartreuse, orange :: Color
rose = addColors red magenta
violet = addColors magenta blue
azure = addColors blue cyan
aquamarine = addColors cyan green
chartreuse = addColors green yellow
orange = addColors yellow red