forked from concord-consortium/agentscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolormixin.html
214 lines (143 loc) · 7.88 KB
/
colormixin.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html>
<head>
<title>colormixin.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>colormixin.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>Experimental: A function performing a dynamic mixin for a new color.
To add a new color to a class, like “labelColor”, the following is created:</p>
<ul>
<li>labelColor: A defineProperty which calls a setter/getter method pair:</li>
<li>setLableColor/getLabelColor: (will not be overridden if present)</li>
<li>labelColorProp: the property used by set/get method, default to colorDefault</li>
<li>colorType: the type associated with labelColor, private within the closure</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">colorMixin</span> = <span class="hljs-params">(obj, colorName, colorDefault, colorType=<span class="hljs-string">"typed"</span>)</span> -></span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>If obj is a class, use its prototype</p>
</div>
<div class="content"><div class='highlight'><pre> proto = obj.prototype ? obj</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Capitolize 1st char of colorName for creating property names</p>
</div>
<div class="content"><div class='highlight'><pre> colorTitle = u.upperCamelCase colorName</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Names we’re adding to the prototype.
We don’t add colorType, its in this closure.</p>
</div>
<div class="content"><div class='highlight'><pre> colorPropName = colorName + <span class="hljs-string">"Prop"</span>
getterName = <span class="hljs-string">"get<span class="hljs-subst">#{colorTitle}</span>"</span>
setterName = <span class="hljs-string">"set<span class="hljs-subst">#{colorTitle}</span>"</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Add names to proto.</p>
</div>
<div class="content"><div class='highlight'><pre> proto[colorPropName] =
<span class="hljs-keyword">if</span> colorDefault <span class="hljs-keyword">then</span> Color.convertColor colorDefault, colorType <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Add setter if not already there:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> proto[setterName]?
proto[setterName] = <span class="hljs-function"><span class="hljs-params">(r,g,b,a=<span class="hljs-number">255</span>)</span> -></span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Setter: If a single argument given, convert to a valid color</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> g <span class="hljs-keyword">is</span> <span class="hljs-literal">undefined</span>
color = Color.convertColor r, colorType <span class="hljs-comment"># type check/conversion</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>If own, non colormap color, use setter</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@hasOwnProperty</span>(colorPropName) <span class="hljs-keyword">and</span>
colorType <span class="hljs-keyword">is</span> <span class="hljs-string">"typed"</span> <span class="hljs-keyword">and</span>
<span class="hljs-keyword">not</span> (color = @[colorPropName]).map?
color.setColor r,g,b,a</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>.. otherwise create a new color</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"new color"</span>
color = Color.rgbaToColor r, g, b, a, colorType
@[colorPropName] = color
<span class="hljs-keyword">unless</span> proto[getterName]?</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Getter: return the colorPropName’s value</p>
</div>
<div class="content"><div class='highlight'><pre> proto[getterName] = <span class="hljs-function">-></span> @[colorPropName]</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>define the color property</p>
</div>
<div class="content"><div class='highlight'><pre> Object.defineProperty proto, colorName,
<span class="hljs-attribute">get</span>: <span class="hljs-function">-></span> @[getterName]()
<span class="hljs-attribute">set</span>: <span class="hljs-function"><span class="hljs-params">(val)</span> -></span> @[setterName](val)
proto</pre></div></div>
</li>
</ul>
</div>
</body>
</html>