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