1 /************************************************************************************************************<br>
4 Rounded corners class<br>
5 (C) www.dhtmlgoodies.com, September 2006<br>
7 This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website. <br>
10 Look at the terms of use at http://www.dhtmlgoodies.com/index.html?page=termsOfUse<br>
14 www.dhtmlgoodies.com<br>
15 Alf Magne Kalleland<br>
17 ************************************************************************************************************/
20 function DHTMLgoodies_roundedCorners()
22 var roundedCornerTargets;
24 this.roundedCornerTargets = new Array();
29 DHTMLgoodies_roundedCorners.prototype = {
35 * Add rounded corners to an element
37 * @param String divId = Id of element on page. Example "leftColumn" for <div id="leftColumn">
38 * @param Int xRadius = Y radius of rounded corners, example 10
39 * @param Int yRadius = Y radius of rounded corners, example 10
40 * @param String color = Background color of element, example #FFF or #AABBCC
41 * @param String color = backgroundColor color of element "behind", example #FFF or #AABBCC
42 * @param Int padding = Padding of content - This will be added as left and right padding(not top and bottom)
43 * @param String heightOfContent = Optional argument. You can specify a fixed height of your content. example "15" which means pixels, or "50%".
44 * @param String whichCorners = Optional argument. Commaseparated list of corners, example "top_left,top_right,bottom_left"
48 addTarget : function(divId,xRadius,yRadius,color,backgroundColor,padding,heightOfContent,whichCorners)
50 var index = this.roundedCornerTargets.length;
51 this.roundedCornerTargets[index] = new Array();
52 this.roundedCornerTargets[index]['divId'] = divId;
53 this.roundedCornerTargets[index]['xRadius'] = xRadius;
54 this.roundedCornerTargets[index]['yRadius'] = yRadius;
55 this.roundedCornerTargets[index]['color'] = color;
56 this.roundedCornerTargets[index]['backgroundColor'] = backgroundColor;
57 this.roundedCornerTargets[index]['padding'] = padding;
58 this.roundedCornerTargets[index]['heightOfContent'] = heightOfContent;
59 this.roundedCornerTargets[index]['whichCorners'] = whichCorners;
68 * Initializes the script
76 for(var targetCounter=0;targetCounter < this.roundedCornerTargets.length;targetCounter++){
78 // Creating local variables of each option
79 whichCorners = this.roundedCornerTargets[targetCounter]['whichCorners'];
80 divId = this.roundedCornerTargets[targetCounter]['divId'];
81 xRadius = this.roundedCornerTargets[targetCounter]['xRadius'];
82 yRadius = this.roundedCornerTargets[targetCounter]['yRadius'];
83 color = this.roundedCornerTargets[targetCounter]['color'];
84 backgroundColor = this.roundedCornerTargets[targetCounter]['backgroundColor'];
85 padding = this.roundedCornerTargets[targetCounter]['padding'];
86 heightOfContent = this.roundedCornerTargets[targetCounter]['heightOfContent'];
87 whichCorners = this.roundedCornerTargets[targetCounter]['whichCorners'];
89 // Which corners should we add rounded corners to?
90 var cornerArray = new Array();
91 if(!whichCorners || whichCorners=='all'){
92 cornerArray['top_left'] = true;
93 cornerArray['top_right'] = true;
94 cornerArray['bottom_left'] = true;
95 cornerArray['bottom_right'] = true;
97 cornerArray = whichCorners.split(/,/gi);
98 for(var prop in cornerArray)cornerArray[cornerArray[prop]] = true;
102 var factorX = xRadius/yRadius; // How big is x radius compared to y radius
104 var obj = document.getElementById(divId); // Creating reference to element
105 obj.style.backgroundColor=null; // Setting background color blank
106 obj.style.backgroundColor='transparent';
107 var content = obj.innerHTML; // Saving HTML content of this element
108 obj.innerHTML = ''; // Setting HTML content of element blank-
113 // Adding top corner div.
115 if(cornerArray['top_left'] || cornerArray['top_right']){
116 var topBar_container = document.createElement('DIV');
117 topBar_container.style.height = yRadius + 'px';
118 topBar_container.style.overflow = 'hidden';
120 obj.appendChild(topBar_container);
121 var currentAntialiasSize = 0;
122 var savedRestValue = 0;
124 for(no=1;no<=yRadius;no++){
125 var marginSize = (xRadius - (this.getY((yRadius - no),yRadius,factorX)));
126 var marginSize_decimals = (xRadius - (this.getY_withDecimals((yRadius - no),yRadius,factorX)));
127 var restValue = xRadius - marginSize_decimals;
128 var antialiasSize = xRadius - marginSize - Math.floor(savedRestValue)
129 var foregroundSize = xRadius - (marginSize + antialiasSize);
131 var el = document.createElement('DIV');
132 el.style.overflow='hidden';
133 el.style.height = '1px';
134 if(cornerArray['top_left'])el.style.marginLeft = marginSize + 'px';
135 if(cornerArray['top_right'])el.style.marginRight = marginSize + 'px';
136 topBar_container.appendChild(el);
137 var y = topBar_container;
139 for(var no2=1;no2<=antialiasSize;no2++){
142 if (no2 == antialiasSize)
143 blendMode = ((restValue + savedRestValue) /2) - foregroundSize;
145 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2),xRadius,1/factorX);
146 blendMode = (restValue - foregroundSize - antialiasSize + 1) * (tmpValue - (yRadius - no)) /2;
150 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2 + 1),xRadius,1/factorX);
151 blendMode = 1 - (1 - (tmpValue - (yRadius - no))) * (1 - (savedRestValue - foregroundSize)) /2;
154 var tmpValue2 = this.getY_withDecimals((xRadius - marginSize - no2),xRadius,1/factorX);
155 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2 + 1),xRadius,1/factorX);
156 blendMode = ((tmpValue + tmpValue2) / 2) - (yRadius - no);
159 el.style.backgroundColor = this.__blendColors(backgroundColor,color,blendMode);
162 var el = document.createElement('DIV');
163 el.style.height = '1px';
164 el.style.overflow='hidden';
165 if(cornerArray['top_left'])el.style.marginLeft = '1px';
166 if(cornerArray['top_right'])el.style.marginRight = '1px';
167 el.style.backgroundColor=color;
171 savedRestValue = restValue;
176 var contentDiv = document.createElement('DIV');
177 contentDiv.className = obj.className;
178 contentDiv.style.border='1px solid ' + color;
179 contentDiv.innerHTML = content;
180 contentDiv.style.backgroundColor=color;
181 contentDiv.style.paddingLeft = padding + 'px';
182 contentDiv.style.paddingRight = padding + 'px';
184 if(!heightOfContent)heightOfContent = '';
185 heightOfContent = heightOfContent + '';
186 if(heightOfContent.length>0 && heightOfContent.indexOf('%')==-1)heightOfContent = heightOfContent + 'px';
187 if(heightOfContent.length>0)contentDiv.style.height = heightOfContent;
189 obj.appendChild(contentDiv);
192 if(cornerArray['bottom_left'] || cornerArray['bottom_right']){
193 var bottomBar_container = document.createElement('DIV');
194 bottomBar_container.style.height = yRadius + 'px';
195 bottomBar_container.style.overflow = 'hidden';
197 obj.appendChild(bottomBar_container);
198 var currentAntialiasSize = 0;
199 var savedRestValue = 0;
201 var errorOccured = false;
202 var arrayOfDivs = new Array();
203 for(no=1;no<=yRadius;no++){
205 var marginSize = (xRadius - (this.getY((yRadius - no),yRadius,factorX)));
206 var marginSize_decimals = (xRadius - (this.getY_withDecimals((yRadius - no),yRadius,factorX)));
208 var restValue = (xRadius - marginSize_decimals);
209 var antialiasSize = xRadius - marginSize - Math.floor(savedRestValue)
210 var foregroundSize = xRadius - (marginSize + antialiasSize);
212 var el = document.createElement('DIV');
213 el.style.overflow='hidden';
214 el.style.height = '1px';
215 if(cornerArray['bottom_left'])el.style.marginLeft = marginSize + 'px';
216 if(cornerArray['bottom_right'])el.style.marginRight = marginSize + 'px';
217 bottomBar_container.insertBefore(el,bottomBar_container.firstChild);
219 var y = bottomBar_container;
221 for(var no2=1;no2<=antialiasSize;no2++){
224 if (no2 == antialiasSize)
225 blendMode = ((restValue + savedRestValue) /2) - foregroundSize;
227 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2),xRadius,1/factorX);
228 blendMode = (restValue - foregroundSize - antialiasSize + 1) * (tmpValue - (yRadius - no)) /2;
232 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2 + 1),xRadius,1/factorX);
233 blendMode = 1 - (1 - (tmpValue - (yRadius - no))) * (1 - (savedRestValue - foregroundSize)) /2;
236 var tmpValue2 = this.getY_withDecimals((xRadius - marginSize - no2),xRadius,1/factorX);
237 var tmpValue = this.getY_withDecimals((xRadius - marginSize - no2 + 1),xRadius,1/factorX);
238 blendMode = ((tmpValue + tmpValue2) / 2) - (yRadius - no);
241 el.style.backgroundColor = this.__blendColors(backgroundColor,color,blendMode);
243 if(y==bottomBar_container)arrayOfDivs[arrayOfDivs.length] = el;
245 try{ // Need to look closer at this problem which occures in Opera.
246 var firstChild = y.getElementsByTagName('DIV')[0];
247 y.insertBefore(el,y.firstChild);
254 var el = document.createElement('DIV');
255 el.style.height = '1px';
256 el.style.overflow='hidden';
257 if(cornerArray['bottom_left'])el.style.marginLeft = '1px';
258 if(cornerArray['bottom_right'])el.style.marginRight = '1px';
262 if(errorOccured){ // Opera fix
263 for(var divCounter=arrayOfDivs.length-1;divCounter>=0;divCounter--){
264 bottomBar_container.appendChild(arrayOfDivs[divCounter]);
268 el.style.backgroundColor=color;
270 savedRestValue = restValue;
282 * Add rounded corners to an element
284 * @param Int x = x Coordinate
285 * @param Int maxX = Size of rounded corners
290 getY : function(x,maxX,factorX){
291 // y = sqrt(100 - x^2)
292 // Y = 0.5 * ((100 - x^2)^0.5);
293 return Math.max(0,Math.ceil(factorX * Math.sqrt( (maxX * maxX) - (x*x)) ));
298 // {{{ getY_withDecimals()
302 * Add rounded corners to an element
304 * @param Int x = x Coordinate
305 * @param Int maxX = Size of rounded corners
310 getY_withDecimals : function(x,maxX,factorX){
311 // y = sqrt(100 - x^2)
312 // Y = 0.5 * ((100 - x^2)^0.5);
313 return Math.max(0,factorX * Math.sqrt( (maxX * maxX) - (x*x)) );
320 // {{{ __blendColors()
324 * Simply blending two colors by extracting red, green and blue and subtracting difference between colors from them.
325 * Finally, we multiply it with the blendMode value
327 * @param String colorA = RGB color
328 * @param String colorB = RGB color
329 * @param Float blendMode
334 __blendColors : function (colorA, colorB, blendMode) {
335 if(colorA.length=='4'){ // In case we are dealing with colors like #FFF
336 colorA = '#' + colorA.substring(1,1) + colorA.substring(1,1) + colorA.substring(2,1) + colorA.substring(2,1) + colorA.substring(3,1) + colorA.substring(3,1);
338 if(colorB.length=='4'){ // In case we are dealing with colors like #FFF
339 colorB = '#' + colorB.substring(1,1) + colorB.substring(1,1) + colorB.substring(2,1) + colorB.substring(2,1) + colorB.substring(3,1) + colorB.substring(3,1);
341 var colorArrayA = [parseInt('0x' + colorA.substring(1,3)), parseInt('0x' + colorA.substring(3, 5)), parseInt('0x' + colorA.substring(5, 7))]; // Create array of Red, Green and Blue ( 0-255)
342 var colorArrayB = [parseInt('0x' + colorB.substring(1,3)), parseInt('0x' + colorB.substring(3, 5)), parseInt('0x' + colorB.substring(5, 7))]; // Create array of Red, Green and Blue ( 0-255)
343 var red = Math.round(colorArrayA[0] + (colorArrayB[0] - colorArrayA[0])*blendMode).toString(16); // Create new Red color ( Hex )
344 var green = Math.round(colorArrayA[1] + (colorArrayB[1] - colorArrayA[1])*blendMode).toString(16); // Create new Green color ( Hex )
345 var blue = Math.round(colorArrayA[2] + (colorArrayB[2] - colorArrayA[2])*blendMode).toString(16); // Create new Blue color ( Hex )
347 if(red.length==1)red = '0' + red;
348 if(green.length==1)green = '0' + green;
349 if(blue.length==1)blue = '0' + blue;
351 return '#' + red + green+ blue; // Return new RGB color