{-# OPTIONS_GHC #-}
module Vis.GlossColor
(
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 (Int -> Color -> ShowS
[Color] -> ShowS
Color -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Color] -> ShowS
$cshowList :: [Color] -> ShowS
show :: Color -> String
$cshow :: Color -> String
showsPrec :: Int -> Color -> ShowS
$cshowsPrec :: Int -> Color -> ShowS
Show, Color -> Color -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Color -> Color -> Bool
$c/= :: Color -> Color -> Bool
== :: Color -> Color -> Bool
$c== :: Color -> Color -> Bool
Eq)
instance Num Color where
{-# INLINE (+) #-}
+ :: Color -> Color -> Color
(+) (RGBA Float
r1 Float
g1 Float
b1 Float
_) (RGBA Float
r2 Float
g2 Float
b2 Float
_)
= Float -> Float -> Float -> Float -> Color
RGBA (Float
r1 forall a. Num a => a -> a -> a
+ Float
r2) (Float
g1 forall a. Num a => a -> a -> a
+ Float
g2) (Float
b1 forall a. Num a => a -> a -> a
+ Float
b2) Float
1
{-# INLINE (-) #-}
(-) (RGBA Float
r1 Float
g1 Float
b1 Float
_) (RGBA Float
r2 Float
g2 Float
b2 Float
_)
= Float -> Float -> Float -> Float -> Color
RGBA (Float
r1 forall a. Num a => a -> a -> a
- Float
r2) (Float
g1 forall a. Num a => a -> a -> a
- Float
g2) (Float
b1 forall a. Num a => a -> a -> a
- Float
b2) Float
1
{-# INLINE (*) #-}
* :: Color -> Color -> Color
(*) (RGBA Float
r1 Float
g1 Float
b1 Float
_) (RGBA Float
r2 Float
g2 Float
b2 Float
_)
= Float -> Float -> Float -> Float -> Color
RGBA (Float
r1 forall a. Num a => a -> a -> a
* Float
r2) (Float
g1 forall a. Num a => a -> a -> a
* Float
g2) (Float
b1 forall a. Num a => a -> a -> a
* Float
b2) Float
1
{-# INLINE abs #-}
abs :: Color -> Color
abs (RGBA Float
r1 Float
g1 Float
b1 Float
_)
= Float -> Float -> Float -> Float -> Color
RGBA (forall a. Num a => a -> a
abs Float
r1) (forall a. Num a => a -> a
abs Float
g1) (forall a. Num a => a -> a
abs Float
b1) Float
1
{-# INLINE signum #-}
signum :: Color -> Color
signum (RGBA Float
r1 Float
g1 Float
b1 Float
_)
= Float -> Float -> Float -> Float -> Color
RGBA (forall a. Num a => a -> a
signum Float
r1) (forall a. Num a => a -> a
signum Float
g1) (forall a. Num a => a -> a
signum Float
b1) Float
1
{-# INLINE fromInteger #-}
fromInteger :: Integer -> Color
fromInteger Integer
i
= let f :: Float
f = forall a. Num a => Integer -> a
fromInteger Integer
i
in Float -> Float -> Float -> Float -> Color
RGBA Float
f Float
f Float
f Float
1
makeColor
:: Float
-> Float
-> Float
-> Float
-> Color
makeColor :: Float -> Float -> Float -> Float -> Color
makeColor Float
r Float
g Float
b Float
a
= Color -> Color
clampColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA Float
r Float
g Float
b Float
a
{-# INLINE makeColor #-}
makeColor' :: Float -> Float -> Float -> Float -> Color
makeColor' :: Float -> Float -> Float -> Float -> Color
makeColor' Float
r Float
g Float
b Float
a
= Float -> Float -> Float -> Float -> Color
RGBA Float
r Float
g Float
b Float
a
{-# INLINE makeColor' #-}
makeColor8
:: Int
-> Int
-> Int
-> Int
-> Color
makeColor8 :: Int -> Int -> Int -> Int -> Color
makeColor8 Int
r Int
g Int
b Int
a
= Color -> Color
clampColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
r forall a. Fractional a => a -> a -> a
/ Float
255)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
g forall a. Fractional a => a -> a -> a
/ Float
255)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b forall a. Fractional a => a -> a -> a
/ Float
255)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
a forall a. Fractional a => a -> a -> a
/ Float
255)
{-# INLINE makeColor8 #-}
rgbaOfColor :: Color -> (Float, Float, Float, Float)
rgbaOfColor :: Color -> (Float, Float, Float, Float)
rgbaOfColor (RGBA Float
r Float
g Float
b Float
a) = (Float
r, Float
g, Float
b, Float
a)
{-# INLINE rgbaOfColor #-}
rawColor
:: Float
-> Float
-> Float
-> Float
-> Color
rawColor :: Float -> Float -> Float -> Float -> Color
rawColor = Float -> Float -> Float -> Float -> Color
RGBA
{-# INLINE rawColor #-}
clampColor :: Color -> Color
clampColor :: Color -> Color
clampColor Color
cc
= let (Float
r, Float
g, Float
b, Float
a) = Color -> (Float, Float, Float, Float)
rgbaOfColor Color
cc
in Float -> Float -> Float -> Float -> Color
RGBA (forall a. Ord a => a -> a -> a
min Float
1 Float
r) (forall a. Ord a => a -> a -> a
min Float
1 Float
g) (forall a. Ord a => a -> a -> a
min Float
1 Float
b) (forall a. Ord a => a -> a -> a
min Float
1 Float
a)
normaliseColor :: Color -> Color
normaliseColor :: Color -> Color
normaliseColor Color
cc
= let (Float
r, Float
g, Float
b, Float
a) = Color -> (Float, Float, Float, Float)
rgbaOfColor Color
cc
m :: Float
m = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Float
r, Float
g, Float
b]
in Float -> Float -> Float -> Float -> Color
RGBA (Float
r forall a. Fractional a => a -> a -> a
/ Float
m) (Float
g forall a. Fractional a => a -> a -> a
/ Float
m) (Float
b forall a. Fractional a => a -> a -> a
/ Float
m) Float
a
mixColors
:: Float
-> Float
-> Color
-> Color
-> Color
mixColors :: Float -> Float -> Color -> Color -> Color
mixColors Float
ratio1 Float
ratio2 Color
c1 Color
c2
= let RGBA Float
r1 Float
g1 Float
b1 Float
a1 = Color
c1
RGBA Float
r2 Float
g2 Float
b2 Float
a2 = Color
c2
total :: Float
total = Float
ratio1 forall a. Num a => a -> a -> a
+ Float
ratio2
m1 :: Float
m1 = Float
ratio1 forall a. Fractional a => a -> a -> a
/ Float
total
m2 :: Float
m2 = Float
ratio2 forall a. Fractional a => a -> a -> a
/ Float
total
in Float -> Float -> Float -> Float -> Color
RGBA (Float
m1 forall a. Num a => a -> a -> a
* Float
r1 forall a. Num a => a -> a -> a
+ Float
m2 forall a. Num a => a -> a -> a
* Float
r2)
(Float
m1 forall a. Num a => a -> a -> a
* Float
g1 forall a. Num a => a -> a -> a
+ Float
m2 forall a. Num a => a -> a -> a
* Float
g2)
(Float
m1 forall a. Num a => a -> a -> a
* Float
b1 forall a. Num a => a -> a -> a
+ Float
m2 forall a. Num a => a -> a -> a
* Float
b2)
(Float
m1 forall a. Num a => a -> a -> a
* Float
a1 forall a. Num a => a -> a -> a
+ Float
m2 forall a. Num a => a -> a -> a
* Float
a2)
addColors :: Color -> Color -> Color
addColors :: Color -> Color -> Color
addColors Color
c1 Color
c2
= let RGBA Float
r1 Float
g1 Float
b1 Float
a1 = Color
c1
RGBA Float
r2 Float
g2 Float
b2 Float
a2 = Color
c2
in Color -> Color
normaliseColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA (Float
r1 forall a. Num a => a -> a -> a
+ Float
r2)
(Float
g1 forall a. Num a => a -> a -> a
+ Float
g2)
(Float
b1 forall a. Num a => a -> a -> a
+ Float
b2)
((Float
a1 forall a. Num a => a -> a -> a
+ Float
a2) forall a. Fractional a => a -> a -> a
/ Float
2)
dim :: Color -> Color
dim :: Color -> Color
dim (RGBA Float
r Float
g Float
b Float
a)
= Float -> Float -> Float -> Float -> Color
RGBA (Float
r forall a. Fractional a => a -> a -> a
/ Float
1.2) (Float
g forall a. Fractional a => a -> a -> a
/ Float
1.2) (Float
b forall a. Fractional a => a -> a -> a
/ Float
1.2) Float
a
bright :: Color -> Color
bright :: Color -> Color
bright (RGBA Float
r Float
g Float
b Float
a)
= Color -> Color
clampColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA (Float
r forall a. Num a => a -> a -> a
* Float
1.2) (Float
g forall a. Num a => a -> a -> a
* Float
1.2) (Float
b forall a. Num a => a -> a -> a
* Float
1.2) Float
a
light :: Color -> Color
light :: Color -> Color
light (RGBA Float
r Float
g Float
b Float
a)
= Color -> Color
clampColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA (Float
r forall a. Num a => a -> a -> a
+ Float
0.2) (Float
g forall a. Num a => a -> a -> a
+ Float
0.2) (Float
b forall a. Num a => a -> a -> a
+ Float
0.2) Float
a
dark :: Color -> Color
dark :: Color -> Color
dark (RGBA Float
r Float
g Float
b Float
a)
= Color -> Color
clampColor
forall a b. (a -> b) -> a -> b
$ Float -> Float -> Float -> Float -> Color
RGBA (Float
r forall a. Num a => a -> a -> a
- Float
0.2) (Float
g forall a. Num a => a -> a -> a
- Float
0.2) (Float
b forall a. Num a => a -> a -> a
- Float
0.2) Float
a
greyN :: Float
-> Color
greyN :: Float -> Color
greyN Float
n = Float -> Float -> Float -> Float -> Color
RGBA Float
n Float
n Float
n Float
1.0
black, white :: Color
black :: Color
black = Float -> Float -> Float -> Float -> Color
RGBA Float
0.0 Float
0.0 Float
0.0 Float
1.0
white :: Color
white = Float -> Float -> Float -> Float -> Color
RGBA Float
1.0 Float
1.0 Float
1.0 Float
1.0
red, green, blue :: Color
red :: Color
red = Float -> Float -> Float -> Float -> Color
RGBA Float
1.0 Float
0.0 Float
0.0 Float
1.0
green :: Color
green = Float -> Float -> Float -> Float -> Color
RGBA Float
0.0 Float
1.0 Float
0.0 Float
1.0
blue :: Color
blue = Float -> Float -> Float -> Float -> Color
RGBA Float
0.0 Float
0.0 Float
1.0 Float
1.0
yellow, cyan, magenta :: Color
yellow :: Color
yellow = Color -> Color -> Color
addColors Color
red Color
green
cyan :: Color
cyan = Color -> Color -> Color
addColors Color
green Color
blue
magenta :: Color
magenta = Color -> Color -> Color
addColors Color
red Color
blue
rose, violet, azure, aquamarine, chartreuse, orange :: Color
rose :: Color
rose = Color -> Color -> Color
addColors Color
red Color
magenta
violet :: Color
violet = Color -> Color -> Color
addColors Color
magenta Color
blue
azure :: Color
azure = Color -> Color -> Color
addColors Color
blue Color
cyan
aquamarine :: Color
aquamarine = Color -> Color -> Color
addColors Color
cyan Color
green
chartreuse :: Color
chartreuse = Color -> Color -> Color
addColors Color
green Color
yellow
orange :: Color
orange = Color -> Color -> Color
addColors Color
yellow Color
red