1///Section: g
2///Intro: Implementation of Color. Automatically included when including "g".
3// Definition of the type Color and some example of Color-s
4
5include "obj"
6
7///Intro: Color allows to define new colors.
8///Desc: A color object is made of four real numbers, one for each component
9//  of the color: red, green, blue and the alpha channel. These components are
10//  initialized by default to zero, unless specified otherwise. See the
11//  examples for possible ways of creating a Color.
12///Where: red, green, blue, alpha are Real numbers
13///Example: ``red = Color[.r=1.0]`` for red color.
14// ``grey = Color[.r=.g=.b=0.5]`` for setting red = green = blue = 0.5,
15// ``grey = Color[(0.5, 0.5, 0.5)]`` does the same,
16// ``grey = Color[0.5]`` does the same, too.
17Color = ++(Real r, g, b, a)
18
19([)@Color[$$.r = $$.g = $$.b = 0.0, $$.a = 1.0]
20
21///Intro: set from another color.
22Color@Color[$$.r = $.r, $$.g = $.g, $$.b = $.b, $$.a = $.a]
23
24///Intro: set to the given level of grey (0 for black, 1 for white).
25Real@Color[$$.r = $$.g = $$.b = $]
26
27(Real r, g, b)@Color[$$.r = $.r, $$.g = $.g, $$.b = $.b]
28(Real r, g, b, a)@Color[$$.r = $.r, $$.g = $.g, $$.b = $.b, $$.a = $.a]
29
30Color@Str["Color[.r=", $.r, ", .g=", $.g, ", .b=", $.b, ", .a=", $.a, "]"]
31Color@Print[Str[$]]
32
33///Intro: create the pattern from the color.
34Color@PATTERN[
35  $$.cmdstream.Empty[]
36  \ $$.cmdstream[Obj[const.raw.pattern_create_rgba, $.r, $.g, $.b, $.a]]
37]
38
39///Intro: a color given in terms of Hue, Saturation and Value.
40HSV = ++(Real h, s, v, a)
41Hsv = HSV
42
43(.[)@HSV[.h = 0.0, .s = .v = .a = 1.0]
44
45///Intro: set the HSV object from the given HSV object.
46HSV@HSV[.h = $.h, .s = $.s, .v = $.v, .a = $.a]
47
48Real@HSV[$$.h = $, $$.s = $$.v = $$.a = 1.0]
49
50///Intro: set the HSV object from a triple ``(hue, saturation, value)``.
51(Real h, s, v)@HSV[$$.h = $.h, $$.s = $.s, $$.v = $.v]
52
53///Intro: set the HSV object from a quadruple
54// ``(hue, saturation, value, alpha)``.
55(Real h, s, v, a)@HSV[$$.h = $.h, $$.s = $.s, $$.v = $.v, $$.a = $.a]
56
57///Intro: print the HSV object.
58HSV@Print["HSV[.h=", $.h, ", .s=", $.s, ", .v=", $.v, ", .a=", $.a, "]"]
59
60///Intro: get the color corresponding to the given HSV object.
61Color@HSV "hsv_color" ?
62
63///Intro: get the HSV value for the given color.
64HSV@Color "color_hsv" ?
65
66// To make a color darker
67Color.Darker = Void
68Real@Color.Darker[
69  c = Color[HSV[$$$, .v *= Max[0, $]]]
70  $$$.r = c.r, $$$.g = c.g, $$$.b = c.b
71]
72
73///Intro: darken a color.
74///Example: ``Dark[color.green, 0.5]`` returns a dark green color.
75Dark = Color
76
77///Intro: provide the color which should be made darker.
78Color@Dark[$$ = $]
79
80///Intro: darken the previously given color. The value should range between
81// 0.0 and 1.0. The value 1.0 returns the original color, while the value 0.0
82// darkens the color to a point where it becomes black.
83Real@Dark[c = Color[$$], c.Darker[$], c]
84
85///Intro: convert a Color to an Obj.
86Color@Obj[Obj[Point[.x=$.r, .y=$.g], Point[.x=$.b, .y=$.a]]]
87
88///Intro: make a Color out of an Obj.
89Obj@Color[
90  rg_comp = Point[$.Get[0]]
91  ba_comp = Point[$.Get[1]]
92  .r = rg_comp.x, .g = rg_comp.y
93  .b = ba_comp.x, .a = ba_comp.y
94]
95
96color = (Color black, red, green, yellow, blue, magenta, cyan, white
97               dark_red, dark_green, dark_yellow, dark_blue, dark_magenta
98               dark_cyan, grey, none)[
99  on  = 1.0 // Maximum intensity
100  mid = 0.5 // Medium intensity
101  off = 0.0 // Minimum intensity
102
103  .black.r    = off, .black.g    = off, .black.b    = off, .black.a    = on
104  .red.r      =  on, .red.g      = off, .red.b      = off, .red.a      = on
105  .green.r    = off, .green.g    =  on, .green.b    = off, .green.a    = on
106  .yellow.r   =  on, .yellow.g   =  on, .yellow.b   = off, .yellow.a   = on
107  .blue.r     = off, .blue.g     = off, .blue.b     =  on, .blue.a     = on
108  .magenta.r  =  on, .magenta.g  = off, .magenta.b  =  on, .magenta.a  = on
109  .cyan.r     = off, .cyan.g     =  on, .cyan.b     =  on, .cyan.a     = on
110  .white.r    =  on, .white.g    =  on, .white.b    =  on, .white.a    = on
111
112  .dark_red.r    = mid, .dark_red.g    = off, .dark_red.b    = off, .dark_red.a    = on
113  .dark_green.r  = off, .dark_green.g  = mid, .dark_green.b  = off, .dark_green.a  = on
114  .dark_yellow.r = mid, .dark_yellow.g = mid, .dark_yellow.b = off, .dark_yellow.a = on
115  .dark_blue.r   = off, .dark_blue.g   = off, .dark_blue.b   = mid, .dark_blue.a   = on
116  .dark_magenta.r= mid, .dark_magenta.g= off, .dark_magenta.b= mid, .dark_magenta.a= on
117  .dark_cyan.r   = off, .dark_cyan.g   = mid, .dark_cyan.b   = mid, .dark_cyan.a   = on
118  .grey.r        = mid, .grey.g        = mid, .grey.b        = mid, .grey.a        = on
119
120  // Just to unset the local default color for the subobjects of Window
121  .none.r = -1.0
122  .none.g = .none.b = .none.a = 0.0
123]
124