Hooked up decimal system
[rgbconverter.git] / 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 var lastinput = 'rgb';
20
21 // ###################################################################
22 // watches the three RGB fields to make sure they don't go over the limites
23 function rgbwatcher(colour)
24 {
25 field = document.getElementById(colour + "inputf");
26
27 // sanitize the number
28 var newval = field.value.replace(/[^0-9\-\.]*/g, "");
29 newval = Math.floor(newval);
30
31 // make sure we don't go over 255
32 if (newval > 255)
33 {
34 newval = 255;
35 }
36 else if (newval < 0)
37 {
38 newval = 0;
39 }
40
41 // update the text field
42 field.value = newval;
43
44 // set fields[]
45 fields[colour] = newval;
46
47 // set hex
48 update_hex();
49
50 // redraw the swatch
51 lastinput = 'rgb';
52 update_swatch();
53 }
54
55 // ###################################################################
56 // update the hex value
57 function update_hex()
58 {
59 var hexstr = dec2hex(fields['red']) + dec2hex(fields['green']) + dec2hex(fields['blue']);
60 fields['hex'] = hexstr;
61 }
62
63 // ###################################################################
64 // update the colour swatch
65 function update_swatch()
66 {
67
68 }
69
70 // ###################################################################
71 // convert a decimal to a hexidecimal
72 function dec2hex(dec)
73 {
74 var hexstr = "0123456789ABCDEF";
75 var low = dec % 16;
76 var high = (dec - low) / 16;
77 hex = "" + hexstr.charAt(high) + hexstr.charAt(low);
78
79 return hex.toString();
80 }
81
82 // ###################################################################
83 // converts a hexidecimal to a decimal
84 function hex2dec(hex)
85 {
86 return parseInt(hex, 16);
87 }
88
89 // ###################################################################
90 // ###################################################################
91 // ###################################################################
92
93 // flip data
94
95 function show_back()
96 {
97 var front = document.getElementById("front");
98 var back = document.getElementById("back");
99
100 if (window.widget)
101 {
102 widget.prepareForTransition("ToBack");
103 }
104
105 front.style.display = "none";
106 back.style.display = "block";
107
108 if (window.widget)
109 {
110 setTimeout("widget.performTransition();", 0);
111 }
112
113 document.getElementById("fliprollie").style.display = "none";
114 }
115
116 function hide_back()
117 {
118 var front = document.getElementById("front");
119 var back = document.getElementById("back");
120
121 if (window.widget)
122 {
123 widget.prepareForTransition("ToFront");
124 }
125
126 back.style.display = "none";
127 front.style.display = "block";
128
129 if (window.widget)
130 {
131 setTimeout("widget.performTransition();", 0);
132 }
133 }
134
135 var flipShown = false;
136
137 var animation = {
138 duration : 0,
139 starttime : 0,
140 to : 1.0,
141 now : 0.0,
142 from : 0.0,
143 firstElement : null,
144 timer : null
145 };
146
147 function mousemove(event)
148 {
149 if (!flipShown)
150 {
151 if (animation.timer != null)
152 {
153 clearInterval(animation.timer);
154 animation.timer = null;
155 }
156
157 var starttime = (new Date).getTime() - 13;
158
159 animation.duration = 500;
160 animation.starttime = starttime;
161 animation.firstElement = document.getElementById("flip");
162 animation.timer = setInterval("animate();", 13);
163 animation.from = animation.now;
164 animation.to = 1.0;
165 animate();
166 flipShown = true;
167 }
168 }
169
170 function mouseexit(event)
171 {
172 if (flipShown)
173 {
174 // fade in the flip widget
175 if (animation.timer != null)
176 {
177 clearInterval (animation.timer);
178 animation.timer = null;
179 }
180
181 var starttime = (new Date).getTime() - 13;
182
183 animation.duration = 500;
184 animation.starttime = starttime;
185 animation.firstElement = document.getElementById("flip");
186 animation.timer = setInterval("animate();", 13);
187 animation.from = animation.now;
188 animation.to = 0.0;
189 animate();
190 flipShown = false;
191 }
192 }
193
194
195 function animate()
196 {
197 var T;
198 var ease;
199 var time = (new Date).getTime();
200
201 T = limit_3(time - animation.starttime, 0, animation.duration);
202
203 if (T >= animation.duration)
204 {
205 clearInterval(animation.timer);
206 animation.timer = null;
207 animation.now = animation.to;
208 }
209 else
210 {
211 ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
212 animation.now = compute_next_float(animation.from, animation.to, ease);
213 }
214
215 animation.firstElement.style.opacity = animation.now;
216 }
217
218 function limit_3 (a, b, c)
219 {
220 return a < b ? b : (a > c ? c : a);
221 }
222
223 function compute_next_float(from, to, ease)
224 {
225 return from + (to - from) * ease;
226 }
227
228 function enterflip(event)
229 {
230 document.getElementById("fliprollie").style.display = "block";
231 }
232
233 function exitflip(event)
234 {
235 document.getElementById("fliprollie").style.display = "none";
236 }
237
238 /*=====================================================================*\
239 || ###################################################################
240 || # $HeadURL$
241 || # $Id$
242 || ###################################################################
243 \*=====================================================================*/