Changeset 1776 for branches/3.0
- Timestamp:
- 03/26/2007 02:17:29 AM (20 months ago)
- Location:
- branches/3.0/framework/Web/Javascripts
- Files:
-
- 3 modified
-
js/compressed/colorpicker.js (modified) (1 diff)
-
js/debug/colorpicker.js (modified) (31 diffs)
-
prado/colorpicker/colorpicker.js (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/framework/Web/Javascripts/js/compressed/colorpicker.js
r1775 r1776 1 1 2 if(typeof(Rico)=="undefined")Rico={};Rico.Color=Class.create();Rico.Color.prototype={initialize:function(red,green,blue){this.rgb={r:red,g:green,b:blue};},setRed:function(r){this.rgb.r=r;},setGreen:function(g){this.rgb.g=g;},setBlue:function(b){this.rgb.b=b;},setHue:function(h){var hsb=this.asHSB();hsb.h=h;this.rgb=Rico.Color.HSBtoRGB(hsb.h,hsb.s,hsb.b);},setSaturation:function(s){var hsb=this.asHSB();hsb.s=s;this.rgb=Rico.Color.HSBtoRGB(hsb.h,hsb.s,hsb.b);},setBrightness:function(b){var hsb=this.asHSB();hsb.b=b;this.rgb=Rico.Color.HSBtoRGB(hsb.h,hsb.s,hsb.b);},darken:function(percent){var hsb=this.asHSB();this.rgb=Rico.Color.HSBtoRGB(hsb.h,hsb.s,Math.max(hsb.b-percent,0));},brighten:function(percent){var hsb=this.asHSB();this.rgb=Rico.Color.HSBtoRGB(hsb.h,hsb.s,Math.min(hsb.b+percent,1));},blend:function(other){this.rgb.r=Math.floor((this.rgb.r+other.rgb.r)/2);this.rgb.g=Math.floor((this.rgb.g+other.rgb.g)/2);this.rgb.b=Math.floor((this.rgb.b+other.rgb.b)/2);},isBright:function(){var hsb=this.asHSB();return this.asHSB().b>0.5;},isDark:function(){return!this.isBright();},asRGB:function(){return"rgb("+this.rgb.r+","+this.rgb.g+","+this.rgb.b+")";},asHex:function(){return"#"+this.rgb.r.toColorPart()+this.rgb.g.toColorPart()+this.rgb.b.toColorPart();},asHSB:function(){return Rico.Color.RGBtoHSB(this.rgb.r,this.rgb.g,this.rgb.b);},toString:function(){return this.asHex();}};Rico.Color.createFromHex=function(hexCode){if(hexCode.indexOf('#')==0) 3 hexCode=hexCode.substring(1);var red="ff",green="ff",blue="ff";if(hexCode.length>4) 4 {red=hexCode.substring(0,2);green=hexCode.substring(2,4);blue=hexCode.substring(4,6);} 5 else if(hexCode.length>0&hexCode.length<4) 6 {var r=hexCode.substring(0,1);var g=hexCode.substring(1,2);var b=hexCode.substring(2);red=r+r;green=g+g;blue=b+b;} 7 return new Rico.Color(parseInt(red,16),parseInt(green,16),parseInt(blue,16));} 8 Rico.Color.createColorFromBackground=function(elem){var actualColor=Element.getStyle($(elem),"background-color");if(actualColor=="transparent"&&elem.parent) 9 return Rico.Color.createColorFromBackground(elem.parent);if(actualColor==null) 10 return new Rico.Color(255,255,255);if(actualColor.indexOf("rgb(")==0){var colors=actualColor.substring(4,actualColor.length-1);var colorArray=colors.split(",");return new Rico.Color(parseInt(colorArray[0]),parseInt(colorArray[1]),parseInt(colorArray[2]));} 11 else if(actualColor.indexOf("#")==0){return Rico.Color.createFromHex(actualColor);} 12 else 13 return new Rico.Color(255,255,255);} 14 Rico.Color.HSBtoRGB=function(hue,saturation,brightness){var red=0;var green=0;var blue=0;if(saturation==0){red=parseInt(brightness*255.0+0.5);green=red;blue=red;} 15 else{var h=(hue-Math.floor(hue))*6.0;var f=h-Math.floor(h);var p=brightness*(1.0-saturation);var q=brightness*(1.0-saturation*f);var t=brightness*(1.0-(saturation*(1.0-f)));switch(parseInt(h)){case 0:red=(brightness*255.0+0.5);green=(t*255.0+0.5);blue=(p*255.0+0.5);break;case 1:red=(q*255.0+0.5);green=(brightness*255.0+0.5);blue=(p*255.0+0.5);break;case 2:red=(p*255.0+0.5);green=(brightness*255.0+0.5);blue=(t*255.0+0.5);break;case 3:red=(p*255.0+0.5);green=(q*255.0+0.5);blue=(brightness*255.0+0.5);break;case 4:red=(t*255.0+0.5);green=(p*255.0+0.5);blue=(brightness*255.0+0.5);break;case 5:red=(brightness*255.0+0.5);green=(p*255.0+0.5);blue=(q*255.0+0.5);break;}} 16 return{r:parseInt(red),g:parseInt(green),b:parseInt(blue)};} 17 Rico.Color.RGBtoHSB=function(r,g,b){var hue;var saturaton;var brightness;var cmax=(r>g)?r:g;if(b>cmax) 18 cmax=b;var cmin=(r<g)?r:g;if(b<cmin) 19 cmin=b;brightness=cmax/255.0;if(cmax!=0) 20 saturation=(cmax-cmin)/cmax;else 21 saturation=0;if(saturation==0) 22 hue=0;else{var redc=(cmax-r)/(cmax-cmin);var greenc=(cmax-g)/(cmax-cmin);var bluec=(cmax-b)/(cmax-cmin);if(r==cmax) 23 hue=bluec-greenc;else if(g==cmax) 24 hue=2.0+redc-bluec;else 25 hue=4.0+greenc-redc;hue=hue/6.0;if(hue<0) 26 hue=hue+1.0;} 27 return{h:hue,s:saturation,b:brightness};} 2 28 Prado.WebUI.TColorPicker=Class.create();Object.extend(Prado.WebUI.TColorPicker,{palettes:{Small:[["fff","fcc","fc9","ff9","ffc","9f9","9ff","cff","ccf","fcf"],["ccc","f66","f96","ff6","ff3","6f9","3ff","6ff","99f","f9f"],["c0c0c0","f00","f90","fc6","ff0","3f3","6cc","3cf","66c","c6c"],["999","c00","f60","fc3","fc0","3c0","0cc","36f","63f","c3c"],["666","900","c60","c93","990","090","399","33f","60c","939"],["333","600","930","963","660","060","366","009","339","636"],["000","300","630","633","330","030","033","006","309","303"]],Tiny:[["ffffff","00ff00","008000","0000ff"],["c0c0c0","ffff00","ff00ff","000080"],["808080","ff0000","800080","000000"]]},UIImages:{'button.gif':'button.gif','background.png':'background.png'}});Object.extend(Prado.WebUI.TColorPicker.prototype,{initialize:function(options) 3 29 {var basics={Palette:'Small',ClassName:'TColorPicker',Mode:'Basic',OKButtonText:'OK',CancelButtonText:'Cancel',ShowColorPicker:true} -
branches/3.0/framework/Web/Javascripts/js/debug/colorpicker.js
r1775 r1776 1 //-------------------- ricoColor.js 2 if(typeof(Rico) == "undefined") Rico = {}; 3 4 Rico.Color = Class.create(); 5 6 Rico.Color.prototype = { 7 8 initialize: function(red, green, blue) { 9 this.rgb = { r: red, g : green, b : blue }; 10 }, 11 12 setRed: function(r) { 13 this.rgb.r = r; 14 }, 15 16 setGreen: function(g) { 17 this.rgb.g = g; 18 }, 19 20 setBlue: function(b) { 21 this.rgb.b = b; 22 }, 23 24 setHue: function(h) { 25 26 // get an HSB model, and set the new hue... 27 var hsb = this.asHSB(); 28 hsb.h = h; 29 30 // convert back to RGB... 31 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); 32 }, 33 34 setSaturation: function(s) { 35 // get an HSB model, and set the new hue... 36 var hsb = this.asHSB(); 37 hsb.s = s; 38 39 // convert back to RGB and set values... 40 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); 41 }, 42 43 setBrightness: function(b) { 44 // get an HSB model, and set the new hue... 45 var hsb = this.asHSB(); 46 hsb.b = b; 47 48 // convert back to RGB and set values... 49 this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b ); 50 }, 51 52 darken: function(percent) { 53 var hsb = this.asHSB(); 54 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0)); 55 }, 56 57 brighten: function(percent) { 58 var hsb = this.asHSB(); 59 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1)); 60 }, 61 62 blend: function(other) { 63 this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2); 64 this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2); 65 this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2); 66 }, 67 68 isBright: function() { 69 var hsb = this.asHSB(); 70 return this.asHSB().b > 0.5; 71 }, 72 73 isDark: function() { 74 return ! this.isBright(); 75 }, 76 77 asRGB: function() { 78 return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")"; 79 }, 80 81 asHex: function() { 82 return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart(); 83 }, 84 85 asHSB: function() { 86 return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b); 87 }, 88 89 toString: function() { 90 return this.asHex(); 91 } 92 93 }; 94 95 Rico.Color.createFromHex = function(hexCode) { 96 97 if ( hexCode.indexOf('#') == 0 ) 98 hexCode = hexCode.substring(1); 99 100 var red = "ff", green = "ff", blue="ff"; 101 if(hexCode.length > 4) 102 { 103 red = hexCode.substring(0,2); 104 green = hexCode.substring(2,4); 105 blue = hexCode.substring(4,6); 106 } 107 else if(hexCode.length > 0 & hexCode.length < 4) 108 { 109 var r = hexCode.substring(0,1); 110 var g = hexCode.substring(1,2); 111 var b = hexCode.substring(2); 112 red = r+r; 113 green = g+g; 114 blue = b+b; 115 } 116 return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) ); 117 } 118 119 /** 120 * Factory method for creating a color from the background of 121 * an HTML element. 122 */ 123 Rico.Color.createColorFromBackground = function(elem) { 124 125 var actualColor = Element.getStyle($(elem), "background-color"); 126 if ( actualColor == "transparent" && elem.parent ) 127 return Rico.Color.createColorFromBackground(elem.parent); 128 129 if ( actualColor == null ) 130 return new Rico.Color(255,255,255); 131 132 if ( actualColor.indexOf("rgb(") == 0 ) { 133 var colors = actualColor.substring(4, actualColor.length - 1 ); 134 var colorArray = colors.split(","); 135 return new Rico.Color( parseInt( colorArray[0] ), 136 parseInt( colorArray[1] ), 137 parseInt( colorArray[2] ) ); 138 139 } 140 else if ( actualColor.indexOf("#") == 0 ) { 141 return Rico.Color.createFromHex(actualColor); 142 } 143 else 144 return new Rico.Color(255,255,255); 145 } 146 147 Rico.Color.HSBtoRGB = function(hue, saturation, brightness) { 148 149 var red = 0; 150 var green = 0; 151 var blue = 0; 152 153 if (saturation == 0) { 154 red = parseInt(brightness * 255.0 + 0.5); 155 green = red; 156 blue = red; 157 } 158 else { 159 var h = (hue - Math.floor(hue)) * 6.0; 160 var f = h - Math.floor(h); 161 var p = brightness * (1.0 - saturation); 162 var q = brightness * (1.0 - saturation * f); 163 var t = brightness * (1.0 - (saturation * (1.0 - f))); 164 165 switch (parseInt(h)) { 166 case 0: 167 red = (brightness * 255.0 + 0.5); 168 green = (t * 255.0 + 0.5); 169 blue = (p * 255.0 + 0.5); 170 break; 171 case 1: 172 red = (q * 255.0 + 0.5); 173 green = (brightness * 255.0 + 0.5); 174 blue = (p * 255.0 + 0.5); 175 break; 176 case 2: 177 red = (p * 255.0 + 0.5); 178 green = (brightness * 255.0 + 0.5); 179 blue = (t * 255.0 + 0.5); 180 break; 181 case 3: 182 red = (p * 255.0 + 0.5); 183 green = (q * 255.0 + 0.5); 184 blue = (brightness * 255.0 + 0.5); 185 break; 186 case 4: 187 red = (t * 255.0 + 0.5); 188 green = (p * 255.0 + 0.5); 189 blue = (brightness * 255.0 + 0.5); 190 break; 191 case 5: 192 red = (brightness * 255.0 + 0.5); 193 green = (p * 255.0 + 0.5); 194 blue = (q * 255.0 + 0.5); 195 break; 196 } 197 } 198 199 return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) }; 200 } 201 202 Rico.Color.RGBtoHSB = function(r, g, b) { 203 204 var hue; 205 var saturaton; 206 var brightness; 207 208 var cmax = (r > g) ? r : g; 209 if (b > cmax) 210 cmax = b; 211 212 var cmin = (r < g) ? r : g; 213 if (b < cmin) 214 cmin = b; 215 216 brightness = cmax / 255.0; 217 if (cmax != 0) 218 saturation = (cmax - cmin)/cmax; 219 else 220 saturation = 0; 221 222 if (saturation == 0) 223 hue = 0; 224 else { 225 var redc = (cmax - r)/(cmax - cmin); 226 var greenc = (cmax - g)/(cmax - cmin); 227 var bluec = (cmax - b)/(cmax - cmin); 228 229 if (r == cmax) 230 hue = bluec - greenc; 231 else if (g == cmax) 232 hue = 2.0 + redc - bluec; 233 else 234 hue = 4.0 + greenc - redc; 235 236 hue = hue / 6.0; 237 if (hue < 0) 238 hue = hue + 1.0; 239 } 240 241 return { h : hue, s : saturation, b : brightness }; 242 } 243 244 1 245 Prado.WebUI.TColorPicker = Class.create(); 2 246 3 247 Object.extend(Prado.WebUI.TColorPicker, 4 248 { 5 palettes: 249 palettes: 6 250 { 7 251 Small : [["fff", "fcc", "fc9", "ff9", "ffc", "9f9", "9ff", "cff", "ccf", "fcf"], … … 18 262 }, 19 263 20 UIImages : 264 UIImages : 21 265 { 22 266 'button.gif' : 'button.gif', … … 29 273 }); 30 274 31 Object.extend(Prado.WebUI.TColorPicker.prototype, 275 Object.extend(Prado.WebUI.TColorPicker.prototype, 32 276 { 33 277 initialize : function(options) 34 278 { 35 var basics = 279 var basics = 36 280 { 37 281 Palette : 'Small', … … 42 286 ShowColorPicker : true 43 287 } 44 288 45 289 this.element = null; 46 290 this.showing = false; … … 58 302 updatePicker : function(e) 59 303 { 60 var color = Rico.Color.createFromHex(this.input.value); 304 var color = Rico.Color.createFromHex(this.input.value); 61 305 this.button.style.backgroundColor = color.toString(); 62 306 }, 63 307 64 308 buttonOnClick : function(event) 65 309 { … … 85 329 } 86 330 this.show(mode); 87 }, 331 }, 88 332 89 333 show : function(type) … … 97 341 this.element.style.left = pos[0] + "px"; 98 342 this.element.style.display = "block"; 99 343 100 344 this.ieHack(type); 101 345 … … 104 348 this._documentKeyDownEvent = this.keyPressed.bindEvent(this, type); 105 349 Event.observe(document.body, "click", this._documentClickEvent); 106 Event.observe(document,"keydown", this._documentKeyDownEvent); 350 Event.observe(document,"keydown", this._documentKeyDownEvent); 107 351 this.showing = true; 108 352 … … 116 360 } 117 361 }, 118 362 119 363 hide : function(event) 120 364 { … … 127 371 this.showing = false; 128 372 Event.stopObserving(document.body, "click", this._documentClickEvent); 129 Event.stopObserving(document,"keydown", this._documentKeyDownEvent); 130 373 Event.stopObserving(document,"keydown", this._documentKeyDownEvent); 374 131 375 if(this._observingMouseMove) 132 { 133 Event.stopObserving(document.body, "mousemove", this._onMouseMove); 376 { 377 Event.stopObserving(document.body, "mousemove", this._onMouseMove); 134 378 this._observingMouseMove = false; 135 379 } … … 153 397 within = within || el == this.input; 154 398 if(within) break; 155 el = el.parentNode; 399 el = el.parentNode; 156 400 } 157 401 while(el); … … 159 403 }, 160 404 161 ieHack : function() 405 ieHack : function() 162 406 { 163 407 // IE hack 164 if(this.iePopUp) 408 if(this.iePopUp) 165 409 { 166 410 this.iePopUp.style.display = "block"; … … 205 449 cellOnClick : function(e) 206 450 { 207 var el = Event.element(e); 451 var el = Event.element(e); 208 452 if(el.tagName.toLowerCase() != "img") 209 453 return; … … 221 465 222 466 getFullPickerContainer : function(pickerID) 223 { 467 { 224 468 //create the 3 buttons 225 this.buttons = 469 this.buttons = 226 470 { 227 471 //Less : INPUT({value:'Less Colors', className:'button', type:'button'}), … … 229 473 Cancel : INPUT({value:this.options.CancelButtonText, className:'button', type:'button'}) 230 474 }; 231 475 232 476 //create the 6 inputs 233 477 var inputs = {}; … … 240 484 inputs['HEX'] = INPUT({className:'hex',type:'text',size:'6',maxlength:'6'}); 241 485 this.inputs = inputs; 242 486 243 487 var images = Prado.WebUI.TColorPicker.UIImages; 244 488 … … 246 490 this.inputs['oldColor'] = SPAN({className:'oldColor'}); 247 491 248 var inputsTable = 492 var inputsTable = 249 493 TABLE({className:'inputs'}, TBODY(null, 250 494 TR(null, … … 259 503 TD(null,'S:'), 260 504 TD(null,this.inputs['S'], '%')), 261 262 TR(null, 505 506 TR(null, 263 507 TD(null,'V:'), 264 508 TD(null,this.inputs['V'], '%')), 265 266 TR(null, 509 510 TR(null, 267 511 TD({className:'gap'},'R:'), 268 512 TD({className:'gap'},this.inputs['R'])), 269 270 TR(null, 513 514 TR(null, 271 515 TD(null,'G:'), 272 516 TD(null, this.inputs['G'])), 273 517 274 TR(null, 518 TR(null, 275 519 TD(null,'B:'), 276 520 TD(null, this.inputs['B'])), 277 521 278 TR(null, 522 TR(null, 279 523 TD({className:'gap'},'#'), 280 524 TD({className:'gap'},this.inputs['HEX'])) 281 525 )); 282 526 283 var UIimages = 284 { 527 var UIimages = 528 { 285 529 selector : SPAN({className:'selector'}), 286 530 background : SPAN({className:'colorpanel'}), … … 295 539 UIimages['background'] = SPAN({className:'colorpanel',style:filter+"(src='"+images['background.png']+"' sizingMethod=scale);"}) 296 540 } 297 541 298 542 this.inputs = Object.extend(this.inputs, UIimages); 299 543 300 var pickerTable = 544 var pickerTable = 301 545 TABLE(null,TBODY(null, 302 546 TR({className:'selection'}, … … 305 549 TD({className:'inputs'}, inputsTable) 306 550 ), 307 TR({className:'options'}, 308 TD({colSpan:3}, 309 this.buttons['OK'], 551 TR({className:'options'}, 552 TD({colSpan:3}, 553 this.buttons['OK'], 310 554 this.buttons['Cancel']) 311 555 ) 312 556 )); 313 557 314 558 return DIV({className:this.options['ClassName']+" FullColorPicker", 315 559 id:pickerID+"_picker"},pickerTable); … … 322 566 this.setColor(color,true); 323 567 324 var i = 0; 568 var i = 0; 325 569 for(var type in this.inputs) 326 570 { 327 Event.observe(this.inputs[type], "change", 571 Event.observe(this.inputs[type], "change", 328 572 this.onInputChanged.bindEvent(this,type)); 329 573 i++; … … 331 575 if(i > 6) break; 332 576 } 333 577 334 578 this.isMouseDownOnColor = false; 335 579 this.isMouseDownOnHue = false; … … 344 588 Event.observe(this.inputs.hue, "mousedown", this._onHueMouseDown); 345 589 Event.observe(this.inputs.slider, "mousedown", this._onHueMouseDown); 346 590 347 591 Event.observe(document.body, "mouseup", this._onMouseUp); 348 592 349 593 this.observeMouseMovement(); 350 594 … … 359 603 Event.observe(document.body, "mousemove", this._onMouseMove); 360 604 this._observingMouseMove = true; 361 } 605 } 362 606 }, 363 607 … … 390 634 this.changeH(ev); 391 635 Event.stop(ev); 392 }, 636 }, 393 637 394 638 changeSV : function(ev) … … 397 641 var py = Event.pointerY(ev); 398 642 var pos = Position.cumulativeOffset(this.inputs.background); 399 400 var x = this.truncate(px - pos[0],0,255); 643 644 var x = this.truncate(px - pos[0],0,255); 401 645 var y = this.truncate(py - pos[1],0,255); 402 646 … … 407 651 var current_s = parseInt(this.inputs.S.value); 408 652 var current_b = parseInt(this.inputs.V.value); 409 653 410 654 if(current_s == parseInt(s*100) && current_b == parseInt(b*100)) return; 411 655 … … 418 662 this.inputs.selector.style.left = x+"px"; 419 663 this.inputs.selector.style.top = y+"px"; 420 664 421 665 this.inputs.currentColor.style.backgroundColor = color.asHex(); 422 666 … … 429 673 var pos = Position.cumulativeOffset(this.inputs.background); 430 674 var y = this.truncate(py - pos[1],0,255); 431 675 432 676 var h = (255-y)/255; 433 677 var current_h = this.truncate(this.inputs.H.value,0,360); … … 500 744 this.inputs.B.value = color.rgb.b; 501 745 this.inputs.HEX.value = color.asHex().substring(1).toUpperCase(); 502 746 503 747 var images = Prado.WebUI.TColorPicker.UIImages; 504 748 505 749 var changeCss = color.isBright() ? 'removeClassName' : 'addClassName'; 506 750 Element[changeCss](this.inputs.selector, 'target_white'); 507 751 508 752 if(update) 509 753 this.updateSelectors(color); … … 514 758 var hsb = color.asHSB(); 515 759 var pos = [hsb.s*255, hsb.b*255, hsb.h*255]; 516 760 517 761 this.inputs.selector.style.left = this.truncate(pos[0],0,255)+"px"; 518 762 this.inputs.selector.style.top = this.truncate(255-pos[1],0,255)+"px"; 519 763 this.inputs.slider.style.top = this.truncate(255-pos[2],0,255)+"px";
