3 * Copyright 2005 Sabre Airline Solutions
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the
11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12 * either express or implied. See the License for the specific language governing permissions
13 * and limitations under the License.
16 //-------------------- ricoColor.js
17 Rico.Color = Class.create();
19 Rico.Color.prototype = {
21 initialize: function(red, green, blue) {
22 this.rgb = { r: red, g : green, b : blue };
29 setGreen: function(g) {
33 setBlue: function(b) {
39 // get an HSB model, and set the new hue...
40 var hsb = this.asHSB();
43 // convert back to RGB...
44 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
47 setSaturation: function(s) {
48 // get an HSB model, and set the new hue...
49 var hsb = this.asHSB();
52 // convert back to RGB and set values...
53 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
56 setBrightness: function(b) {
57 // get an HSB model, and set the new hue...
58 var hsb = this.asHSB();
61 // convert back to RGB and set values...
62 this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
65 darken: function(percent) {
66 var hsb = this.asHSB();
67 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
70 brighten: function(percent) {
71 var hsb = this.asHSB();
72 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
75 blend: function(other) {
76 this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
77 this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
78 this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
81 isBright: function() {
82 var hsb = this.asHSB();
83 return this.asHSB().b > 0.5;
87 return ! this.isBright();
91 return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
95 return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart();
99 return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
102 toString: function() {
108 Rico.Color.createFromHex = function(hexCode) {
109 if(hexCode.length==4) {
110 var shortHexCode = hexCode;
113 hexCode += (shortHexCode.charAt(i) + shortHexCode.charAt(i));
115 if ( hexCode.indexOf('#') == 0 )
116 hexCode = hexCode.substring(1);
117 var red = hexCode.substring(0,2);
118 var green = hexCode.substring(2,4);
119 var blue = hexCode.substring(4,6);
120 return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
124 * Factory method for creating a color from the background of
127 Rico.Color.createColorFromBackground = function(elem) {
129 var actualColor = Element.getStyle(elem, "background-color");
131 // if color is tranparent, check parent
132 // Safari returns "rgba(0, 0, 0, 0)", which means transparent
133 if ( actualColor.match(/^(transparent|rgba\(0,\s*0,\s*0,\s*0\))$/i) && elem.parentNode )
134 return Rico.Color.createColorFromBackground(elem.parentNode);
136 if ( actualColor == null )
137 return new Rico.Color(255,255,255);
139 if ( actualColor.indexOf("rgb(") == 0 ) {
140 var colors = actualColor.substring(4, actualColor.length - 1 );
141 var colorArray = colors.split(",");
142 return new Rico.Color( parseInt( colorArray[0] ),
143 parseInt( colorArray[1] ),
144 parseInt( colorArray[2] ) );
147 else if ( actualColor.indexOf("#") == 0 ) {
148 return Rico.Color.createFromHex(actualColor);
151 return new Rico.Color(255,255,255);
154 Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {
160 if (saturation == 0) {
161 red = parseInt(brightness * 255.0 + 0.5);
166 var h = (hue - Math.floor(hue)) * 6.0;
167 var f = h - Math.floor(h);
168 var p = brightness * (1.0 - saturation);
169 var q = brightness * (1.0 - saturation * f);
170 var t = brightness * (1.0 - (saturation * (1.0 - f)));
172 switch (parseInt(h)) {
174 red = (brightness * 255.0 + 0.5);
175 green = (t * 255.0 + 0.5);
176 blue = (p * 255.0 + 0.5);
179 red = (q * 255.0 + 0.5);
180 green = (brightness * 255.0 + 0.5);
181 blue = (p * 255.0 + 0.5);
184 red = (p * 255.0 + 0.5);
185 green = (brightness * 255.0 + 0.5);
186 blue = (t * 255.0 + 0.5);
189 red = (p * 255.0 + 0.5);
190 green = (q * 255.0 + 0.5);
191 blue = (brightness * 255.0 + 0.5);
194 red = (t * 255.0 + 0.5);
195 green = (p * 255.0 + 0.5);
196 blue = (brightness * 255.0 + 0.5);
199 red = (brightness * 255.0 + 0.5);
200 green = (p * 255.0 + 0.5);
201 blue = (q * 255.0 + 0.5);
206 return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) };
209 Rico.Color.RGBtoHSB = function(r, g, b) {
215 var cmax = (r > g) ? r : g;
219 var cmin = (r < g) ? r : g;
223 brightness = cmax / 255.0;
225 saturation = (cmax - cmin)/cmax;
232 var redc = (cmax - r)/(cmax - cmin);
233 var greenc = (cmax - g)/(cmax - cmin);
234 var bluec = (cmax - b)/(cmax - cmin);
237 hue = bluec - greenc;
239 hue = 2.0 + redc - bluec;
241 hue = 4.0 + greenc - redc;
248 return { h : hue, s : saturation, b : brightness };
251 //-------------------- ricoCorner.js
254 round: function(e, options) {
256 this._setOptions(options);
257 var color = this.options.color == "fromElement" ? this._background(e) : this.options.color;
258 var bgColor = this.options.bgColor == "fromParent" ? this._background(e.parentNode) : this.options.bgColor;
259 this._roundCornersImpl(e, color, bgColor);
262 _roundCornersImpl: function(e, color, bgColor) {
263 if(this.options.border)
264 this._renderBorder(e,bgColor);
265 if(this._isTopRounded())
266 this._roundTopCorners(e,color,bgColor);
267 if(this._isBottomRounded())
268 this._roundBottomCorners(e,color,bgColor);
271 _renderBorder: function(el,bgColor) {
272 var borderValue = "1px solid " + this._borderColor(bgColor);
273 var borderL = "border-left: " + borderValue;
274 var borderR = "border-right: " + borderValue;
275 var style = "style='" + borderL + ";" + borderR + "'";
276 el.innerHTML = "<div " + style + ">" + el.innerHTML + "</div>"
279 _roundTopCorners: function(el, color, bgColor) {
280 var corner = this._createCorner(bgColor);
281 for(var i=0 ; i < this.options.numSlices ; i++ )
282 corner.appendChild(this._createCornerSlice(color,bgColor,i,"top"));
283 el.style.paddingTop = '0px';
284 el.insertBefore(corner,el.firstChild);
287 _roundBottomCorners: function(el, color, bgColor) {
288 var corner = this._createCorner(bgColor);
289 for(var i=(this.options.numSlices-1) ; i >= 0 ; i-- )
290 corner.appendChild(this._createCornerSlice(color,bgColor,i,"bottom"));
291 el.style.paddingBottom = 0;
292 el.appendChild(corner);
295 _createCorner: function(bgColor) {
296 var corner = document.createElement("div");
297 corner.style.backgroundColor = (this._isTransparent() ? "transparent" : bgColor);
301 _createCornerSlice: function(color,bgColor, n, position) {
302 var slice = document.createElement("span");
304 var inStyle = slice.style;
305 inStyle.backgroundColor = color;
306 inStyle.display = "block";
307 inStyle.height = "1px";
308 inStyle.overflow = "hidden";
309 inStyle.fontSize = "1px";
311 var borderColor = this._borderColor(color,bgColor);
312 if ( this.options.border && n == 0 ) {
313 inStyle.borderTopStyle = "solid";
314 inStyle.borderTopWidth = "1px";
315 inStyle.borderLeftWidth = "0px";
316 inStyle.borderRightWidth = "0px";
317 inStyle.borderBottomWidth = "0px";
318 inStyle.height = "0px"; // assumes css compliant box model
319 inStyle.borderColor = borderColor;
321 else if(borderColor) {
322 inStyle.borderColor = borderColor;
323 inStyle.borderStyle = "solid";
324 inStyle.borderWidth = "0px 1px";
327 if ( !this.options.compact && (n == (this.options.numSlices-1)) )
328 inStyle.height = "2px";
330 this._setMargin(slice, n, position);
331 this._setBorder(slice, n, position);
335 _setOptions: function(options) {
338 color : "fromElement",
339 bgColor : "fromParent",
344 Object.extend(this.options, options || {});
346 this.options.numSlices = this.options.compact ? 2 : 4;
347 if ( this._isTransparent() )
348 this.options.blend = false;
351 _whichSideTop: function() {
352 if ( this._hasString(this.options.corners, "all", "top") )
355 if ( this.options.corners.indexOf("tl") >= 0 && this.options.corners.indexOf("tr") >= 0 )
358 if (this.options.corners.indexOf("tl") >= 0)
360 else if (this.options.corners.indexOf("tr") >= 0)
365 _whichSideBottom: function() {
366 if ( this._hasString(this.options.corners, "all", "bottom") )
369 if ( this.options.corners.indexOf("bl")>=0 && this.options.corners.indexOf("br")>=0 )
372 if(this.options.corners.indexOf("bl") >=0)
374 else if(this.options.corners.indexOf("br")>=0)
379 _borderColor : function(color,bgColor) {
380 if ( color == "transparent" )
382 else if ( this.options.border )
383 return this.options.border;
384 else if ( this.options.blend )
385 return this._blend( bgColor, color );
391 _setMargin: function(el, n, corners) {
392 var marginSize = this._marginSize(n);
393 var whichSide = corners == "top" ? this._whichSideTop() : this._whichSideBottom();
395 if ( whichSide == "left" ) {
396 el.style.marginLeft = marginSize + "px"; el.style.marginRight = "0px";
398 else if ( whichSide == "right" ) {
399 el.style.marginRight = marginSize + "px"; el.style.marginLeft = "0px";
402 el.style.marginLeft = marginSize + "px"; el.style.marginRight = marginSize + "px";
406 _setBorder: function(el,n,corners) {
407 var borderSize = this._borderSize(n);
408 var whichSide = corners == "top" ? this._whichSideTop() : this._whichSideBottom();
409 if ( whichSide == "left" ) {
410 el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = "0px";
412 else if ( whichSide == "right" ) {
413 el.style.borderRightWidth = borderSize + "px"; el.style.borderLeftWidth = "0px";
416 el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = borderSize + "px";
418 if (this.options.border != false)
419 el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = borderSize + "px";
422 _marginSize: function(n) {
423 if ( this._isTransparent() )
426 var marginSizes = [ 5, 3, 2, 1 ];
427 var blendedMarginSizes = [ 3, 2, 1, 0 ];
428 var compactMarginSizes = [ 2, 1 ];
429 var smBlendedMarginSizes = [ 1, 0 ];
431 if ( this.options.compact && this.options.blend )
432 return smBlendedMarginSizes[n];
433 else if ( this.options.compact )
434 return compactMarginSizes[n];
435 else if ( this.options.blend )
436 return blendedMarginSizes[n];
438 return marginSizes[n];
441 _borderSize: function(n) {
442 var transparentBorderSizes = [ 5, 3, 2, 1 ];
443 var blendedBorderSizes = [ 2, 1, 1, 1 ];
444 var compactBorderSizes = [ 1, 0 ];
445 var actualBorderSizes = [ 0, 2, 0, 0 ];
447 if ( this.options.compact && (this.options.blend || this._isTransparent()) )
449 else if ( this.options.compact )
450 return compactBorderSizes[n];
451 else if ( this.options.blend )
452 return blendedBorderSizes[n];
453 else if ( this.options.border )
454 return actualBorderSizes[n];
455 else if ( this._isTransparent() )
456 return transparentBorderSizes[n];
460 _hasString: function(str) { for(var i=1 ; i<arguments.length ; i++) if (str.indexOf(arguments[i]) >= 0) return true; return false; },
461 _blend: function(c1, c2) { var cc1 = Rico.Color.createFromHex(c1); cc1.blend(Rico.Color.createFromHex(c2)); return cc1; },
462 _background: function(el) { try { return Rico.Color.createColorFromBackground(el).asHex(); } catch(err) { return "#ffffff"; } },
463 _isTransparent: function() { return this.options.color == "transparent"; },
464 _isTopRounded: function() { return this._hasString(this.options.corners, "all", "top", "tl", "tr"); },
465 _isBottomRounded: function() { return this._hasString(this.options.corners, "all", "bottom", "bl", "br"); },
466 _hasSingleTextChild: function(el) { return el.childNodes.length == 1 && el.childNodes[0].nodeType == 3; }
469 Rico.includeLoaded('ricoStyles.js');