0 | {- Tian Z (ecburx@burx.vip) -}
 1 |
 2 | ||| Predefined and custom colors.
 3 | module IdrisGL.Color
 4 |
 5 | ||| Make a custom color. All components are clamped to the range [0..255].
 6 | public export
 7 | data Color : Type where
 8 |     ||| RGBA color.
 9 |     ||| @ r Red component.
10 |     ||| @ g Green component.
11 |     ||| @ b Blue component.
12 |     ||| @ a Alpha component.
13 |     MkRGBA : (r : Int) -> (g : Int) -> (b : Int) -> (a : Int) -> Color
14 |     ||| RGB color.
15 |     ||| @ r Red component.
16 |     ||| @ g Green component.
17 |     ||| @ b Blue component.
18 |     MkRGB  : (r : Int) -> (g : Int) -> (b : Int) -> Color
19 |
20 | ||| Take the RGBA components of a color.
21 | ||| @ color Color.
22 | export
23 | rgbaOfColor : (color : Color) -> (Int,Int,Int,Int)
24 | rgbaOfColor (MkRGB  r g b)   = (r,g,b,255)
25 | rgbaOfColor (MkRGBA r g b a) = (r,g,b,a)
26 |
27 | public export
28 | Eq Color where
29 |     a == b = rgbaOfColor a == rgbaOfColor b
30 |     a /= b = not (a == b)
31 |     
32 | ||| Add RGB components of a color component-wise, then normalise them to the highest resulting one. 
33 | ||| The alpha components are averaged.
34 | ||| @ c1 First color.
35 | ||| @ c2 Second color.
36 | export
37 | addColors : (c1 : Color) -> (c2 : Color) -> Color
38 | addColors c1 c2 =
39 |     let (r1, g1, b1, a1) = rgbaOfColor c1
40 |         (r2, g2, b2, a2) = rgbaOfColor c2
41 |     in  MkRGBA (r1 + r2) (g1 + g2) (b1 + b2) ((a1 + a2) `div` 2)
42 |
43 | {- Predefined colors. -}
44 |
45 | export partial
46 | transparent : Color
47 | transparent = MkRGBA 0 0 0 0
48 |
49 | export partial
50 | white   : Color
51 | white   = MkRGB 255 255 255
52 |
53 | export partial
54 | black   : Color
55 | black   = MkRGB   0   0   0
56 |
57 | export partial
58 | red     : Color
59 | red     = MkRGB 255   0   0
60 |
61 | export partial
62 | green   : Color
63 | green   = MkRGB   0 255   0
64 |
65 | export partial
66 | blue    : Color
67 | blue    = MkRGB   0   0 255
68 |
69 | export partial
70 | yellow  : Color
71 | yellow  = MkRGB 255 255   0
72 |
73 | export partial
74 | magenta : Color
75 | magenta = MkRGB 255   0 255
76 |
77 | export partial
78 | cyan    : Color
79 | cyan    = MkRGB   0 255 255