Moving to a project-based layout. Step 1.
[rgbconverter.git] / 1.0 / RGB_Converter.wdgt / Widget.js
1 /*=====================================================================*\
2 || ################################################################### ||
3 || # RGB Converter Widget [#]version[#]
4 || # --------------------------------------------------------------- # ||
5 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
6 || # This file may not be reproduced in any way without permission. # ||
7 || # --------------------------------------------------------------- # ||
8 || # User License Agreement at http://www.iris-studios.com/license/ # ||
9 || ################################################################### ||
10 \*=====================================================================*/
11
12 var fields = {
13 red : 0,
14 green : 0,
15 blue : 0,
16 hex : ''
17 };
18
19 // ###################################################################
20 // widget init
21 function setup()
22 {
23 createGenericButton(document.getElementById("backbutton"), "Done", hide_back, 0);
24 }
25
26 // ###################################################################
27 // watches the three RGB fields to make sure they don't go over the limites
28 function rgbwatcher(colour)
29 {
30 field = document.getElementById(colour + "inputf");
31
32 // sanitize the number
33 var newval = field.value.replace(/[^0-9\-\.]*/g, "");
34 newval = Math.floor(newval);
35
36 // make sure we don't go over 255
37 if (newval > 255)
38 {
39 newval = 255;
40 }
41 else if (newval < 0)
42 {
43 newval = 0;
44 }
45
46 // update the text field
47 field.value = newval;
48
49 // set fields[]
50 fields[colour] = newval;
51
52 // set hex
53 update_hex();
54
55 // redraw the swatch
56 update_swatch();
57 }
58
59 // ###################################################################
60 // watches the hex field for updates
61 function hexwatcher()
62 {
63 field = document.getElementById("hexinputf");
64
65 // sanitize the hex
66 var newval = field.value.replace(/[^0-9a-f]*/gi, "");
67
68 // get the length
69 var length = newval.length;
70
71 // make sure we're always 6
72 if (length > 6)
73 {
74 newval = newval.substr(0, 6);
75 }
76 else if (length < 6)
77 {
78 for (var i = length; i <= 6; i++)
79 {
80 newval = "" + newval + "0";
81 }
82 }
83
84 // update the field
85 field.value = newval;
86
87 // update fields[]
88 fields['hex'] = newval;
89
90 // set RGB
91 update_rgb();
92
93 // redraw the swatch
94 update_swatch();
95 }
96
97 // ###################################################################
98 // update the hex value
99 function update_hex()
100 {
101 var hexstr = dec2hex(fields['red']) + dec2hex(fields['green']) + dec2hex(fields['blue']);
102 fields['hex'] = hexstr;
103 document.getElementById("hexinputf").value = hexstr;
104 }
105
106 // ###################################################################
107 // update the RGB values
108 function update_rgb()
109 {
110 // regex match the bits
111 var bits = fields['hex'].match(/(..)(..)(..)/);
112
113 fields['red'] = hex2dec(bits[1]);
114 fields['green'] = hex2dec(bits[2]);
115 fields['blue'] = hex2dec(bits[3]);
116
117 // construct the hex values
118 document.getElementById("redinputf").value = fields['red'];
119 document.getElementById("greeninputf").value = fields['green'];
120 document.getElementById("blueinputf").value = fields['blue'];
121 }
122
123 // ###################################################################
124 // update the colour swatch
125 function update_swatch()
126 {
127 document.getElementById("swatch").style.backgroundColor = "rgb(" + fields['red'] + ", " + fields['green'] + ", " + fields['blue'] + ")";
128 }
129
130 // ###################################################################
131 // convert a decimal to a hexidecimal
132 function dec2hex(dec)
133 {
134 var hexstr = "0123456789ABCDEF";
135 var low = dec % 16;
136 var high = (dec - low) / 16;
137 hex = "" + hexstr.charAt(high) + hexstr.charAt(low);
138
139 return hex.toString();
140 }
141
142 // ###################################################################
143 // converts a hexidecimal to a decimal
144 function hex2dec(hex)
145 {
146 return parseInt(hex, 16);
147 }
148
149 // ###################################################################
150 // ###################################################################
151 // ###################################################################
152
153 // flip data
154
155 function show_back()
156 {
157 var front = document.getElementById("front");
158 var back = document.getElementById("back");
159
160 if (window.widget)
161 {
162 widget.prepareForTransition("ToBack");
163 }
164
165 front.style.display = "none";
166 back.style.display = "block";
167
168 if (window.widget)
169 {
170 setTimeout("widget.performTransition();", 0);
171 }
172
173 document.getElementById("fliprollie").style.display = "none";
174 }
175
176 function hide_back()
177 {
178 var front = document.getElementById("front");
179 var back = document.getElementById("back");
180
181 if (window.widget)
182 {
183 widget.prepareForTransition("ToFront");
184 }
185
186 back.style.display = "none";
187 front.style.display = "block";
188
189 if (window.widget)
190 {
191 setTimeout("widget.performTransition();", 0);
192 }
193 }
194
195 var flipShown = false;
196
197 var animation = {
198 duration : 0,
199 starttime : 0,
200 to : 1.0,
201 now : 0.0,
202 from : 0.0,
203 firstElement : null,
204 timer : null
205 };
206
207 function mousemove(event)
208 {
209 if (!flipShown)
210 {
211 if (animation.timer != null)
212 {
213 clearInterval(animation.timer);
214 animation.timer = null;
215 }
216
217 var starttime = (new Date).getTime() - 13;
218
219 animation.duration = 500;
220 animation.starttime = starttime;
221 animation.firstElement = document.getElementById("flip");
222 animation.timer = setInterval("animate();", 13);
223 animation.from = animation.now;
224 animation.to = 1.0;
225 animate();
226 flipShown = true;
227 }
228 }
229
230 function mouseexit(event)
231 {
232 if (flipShown)
233 {
234 // fade in the flip widget
235 if (animation.timer != null)
236 {
237 clearInterval (animation.timer);
238 animation.timer = null;
239 }
240
241 var starttime = (new Date).getTime() - 13;
242
243 animation.duration = 500;
244 animation.starttime = starttime;
245 animation.firstElement = document.getElementById("flip");
246 animation.timer = setInterval("animate();", 13);
247 animation.from = animation.now;
248 animation.to = 0.0;
249 animate();
250 flipShown = false;
251 }
252 }
253
254
255 function animate()
256 {
257 var T;
258 var ease;
259 var time = (new Date).getTime();
260
261 T = limit_3(time - animation.starttime, 0, animation.duration);
262
263 if (T >= animation.duration)
264 {
265 clearInterval(animation.timer);
266 animation.timer = null;
267 animation.now = animation.to;
268 }
269 else
270 {
271 ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
272 animation.now = compute_next_float(animation.from, animation.to, ease);
273 }
274
275 animation.firstElement.style.opacity = animation.now;
276 }
277
278 function limit_3 (a, b, c)
279 {
280 return a < b ? b : (a > c ? c : a);
281 }
282
283 function compute_next_float(from, to, ease)
284 {
285 return from + (to - from) * ease;
286 }
287
288 function enterflip(event)
289 {
290 document.getElementById("fliprollie").style.display = "block";
291 }
292
293 function exitflip(event)
294 {
295 document.getElementById("fliprollie").style.display = "none";
296 }
297
298 /*=====================================================================*\
299 || ###################################################################
300 || # $HeadURL$
301 || # $Id$
302 || ###################################################################
303 \*=====================================================================*/