| 1 | var currentItemInEdit; |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | Event.observe(window, 'load', CreateEditnPlaceObjects); |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | var EIPObject = Class.create({ |
|---|
| 8 | |
|---|
| 9 | // -- Constructor |
|---|
| 10 | initialize: function(element, valueType, showPanel) { |
|---|
| 11 | |
|---|
| 12 | var originalContent = ""; |
|---|
| 13 | |
|---|
| 14 | // Initialize object variables |
|---|
| 15 | this.postID = ''; |
|---|
| 16 | this.metaID = ''; |
|---|
| 17 | this.elementID = ''; |
|---|
| 18 | this.elementInnerID = ''; |
|---|
| 19 | this.myNicEditor1 = null; |
|---|
| 20 | this.editActive = false; |
|---|
| 21 | this.oldVal = ''; |
|---|
| 22 | this.valueType = valueType; |
|---|
| 23 | this.showPanel = showPanel; |
|---|
| 24 | this.panelID = ''; |
|---|
| 25 | this.highlightColor = '#FFFFCC'; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | // Get post ID |
|---|
| 29 | var tmpPostID = ''; |
|---|
| 30 | var tmpMetaID = ''; |
|---|
| 31 | $w(element.className).each(function(currClassName){ |
|---|
| 32 | if (currClassName.substr(0, 10) == 'EIP_postid') |
|---|
| 33 | tmpPostID = currClassName.substr(10); |
|---|
| 34 | |
|---|
| 35 | if (currClassName.substr(0, 8) == 'EIP_mid_') |
|---|
| 36 | tmpMetaID = currClassName.substr(8); |
|---|
| 37 | }); |
|---|
| 38 | this.postID = tmpPostID; |
|---|
| 39 | this.metaID = tmpMetaID; |
|---|
| 40 | |
|---|
| 41 | // Get/create element ID |
|---|
| 42 | if (element.id == ''){ |
|---|
| 43 | // Create random ID |
|---|
| 44 | var d = new Date(); |
|---|
| 45 | randomnumber= Math.floor(Math.random()*1000); |
|---|
| 46 | element.id = "id" + d.getMilliseconds() + "_" + randomnumber; |
|---|
| 47 | } |
|---|
| 48 | this.elementID = element.id; |
|---|
| 49 | this.elementInnerID = element.id + '_inner'; |
|---|
| 50 | originalContent = $(this.elementID).innerHTML; |
|---|
| 51 | elementBG = GetFontColor(this.elementID); |
|---|
| 52 | |
|---|
| 53 | // Create nicEditor object |
|---|
| 54 | if (showPanel){ |
|---|
| 55 | this.myNicEditor1 = new nicEditor({iconsPath : JS_FLUTTER_URI + 'js/nicEditorIcons.gif',buttonList : ['bold','italic','underline','ol','ul','link','unlink']}); |
|---|
| 56 | } |
|---|
| 57 | else{ |
|---|
| 58 | this.myNicEditor1 = new nicEditor({buttonList : []}); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // Creat nicEditor panel |
|---|
| 62 | this.panelID = "panel_" + this.elementID; |
|---|
| 63 | myNicPanel = new Element('div', {'id': this.panelID, 'class': 'EIPnicPanelDiv','style': "display:none"}); |
|---|
| 64 | $(document.body).insert({top: myNicPanel}); |
|---|
| 65 | this.myNicEditor1.setPanel(this.panelID); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | // Wrap the field in a div |
|---|
| 69 | element_inner = new Element('div', {'id': this.elementInnerID, 'style':'overflow:hidden'}).update(element.innerHTML); |
|---|
| 70 | element.innerHTML = ""; |
|---|
| 71 | element.insert({top: element_inner}); |
|---|
| 72 | |
|---|
| 73 | // Inherit styles |
|---|
| 74 | stylesList = ["fontSize", "fontFamily", "fontWeight", "letterSpacing", "color", "textTransform", "lineHeight"]; |
|---|
| 75 | applyStyles(this.elementID, this.elementInnerID, stylesList); |
|---|
| 76 | //applyStyles(this.elementID, this.elementID, ['height']); |
|---|
| 77 | |
|---|
| 78 | if (showPanel){ |
|---|
| 79 | recursiveApplyStyles($(this.elementInnerID)); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | // Attach niceditor |
|---|
| 84 | this.myNicEditor1.addInstance(this.elementID); |
|---|
| 85 | if (isset(element.firstChild.contentDocument)){ |
|---|
| 86 | this.elementDivDocument = element.firstChild.contentDocument; |
|---|
| 87 | Event.observe(this.elementDivDocument.firstChild, "mousedown", this.startEdit.bindAsEventListener(this)); |
|---|
| 88 | } |
|---|
| 89 | else{ |
|---|
| 90 | this.elementInnerID = this.elementID; |
|---|
| 91 | $(this.elementID).innerHTML = originalContent; |
|---|
| 92 | this.elementDivDocument = document; // $(this.elementInnerID) |
|---|
| 93 | Event.observe($(this.elementInnerID), "mousedown", this.startEdit.bindAsEventListener(this)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // Adjust highlight color to be suitable for the theme |
|---|
| 97 | |
|---|
| 98 | if (elementBG != ''){ |
|---|
| 99 | newcolor = new Color(elementBG); |
|---|
| 100 | this.highlightColor = newcolor.invert().getHex(); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | $$('EIP_title:hover, .EIP_content:hover').each(function(element) { |
|---|
| 104 | element.setStyle({backgroundColor: this.highlightColor}); |
|---|
| 105 | }); |
|---|
| 106 | |
|---|
| 107 | }, |
|---|
| 108 | |
|---|
| 109 | startEdit: function (){ |
|---|
| 110 | if (!this.editActive){ |
|---|
| 111 | |
|---|
| 112 | // set current field |
|---|
| 113 | cancelSaveField(); |
|---|
| 114 | currentItemInEdit = this; |
|---|
| 115 | |
|---|
| 116 | // Adjust pane/save positions |
|---|
| 117 | objOffset = $(this.elementID).cumulativeOffset(); |
|---|
| 118 | elementTop = objOffset['top'] ; |
|---|
| 119 | if(Prototype.Browser.IE) elementTop = elementTop-20; |
|---|
| 120 | elementLeft = objOffset['left']; |
|---|
| 121 | if (this.showPanel){ |
|---|
| 122 | panel_top = elementTop - $(this.panelID).getHeight(); |
|---|
| 123 | $(this.panelID).setStyle({display: "", top:panel_top+"px",left:elementLeft+"px"}); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | currWidth = $(this.elementID).getWidth(); |
|---|
| 127 | if ( currWidth < $('save_cancel_field').getWidth()) |
|---|
| 128 | currWidth = $('save_cancel_field').getWidth(); |
|---|
| 129 | |
|---|
| 130 | save_cancel_field_left = elementLeft + currWidth - $('save_cancel_field').getWidth(); |
|---|
| 131 | save_cancel_field_top = elementTop - $('save_cancel_field').getHeight(); |
|---|
| 132 | $('save_cancel_field').setStyle({display: "", top:save_cancel_field_top+"px",left:save_cancel_field_left+"px"}); |
|---|
| 133 | |
|---|
| 134 | // Save old value to restore it on cancel |
|---|
| 135 | this.oldVal = this.elementDivDocument.getElementById(this.elementInnerID).innerHTML; |
|---|
| 136 | this.elementDivDocument.getElementById(this.elementInnerID).style.backgroundColor = this.highlightColor; |
|---|
| 137 | |
|---|
| 138 | this.editActive = true; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | }); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | //------------------------------------------------------------ |
|---|
| 148 | // Create common objects (save button and status messages) |
|---|
| 149 | // and initialize editnplace objects |
|---|
| 150 | //------------------------------------------------------------ |
|---|
| 151 | |
|---|
| 152 | function CreateEditnPlaceObjects() { |
|---|
| 153 | |
|---|
| 154 | // Create save/cancel buttons |
|---|
| 155 | saveCancel = new Element('div', {'id': 'save_cancel_field', 'class':'EIPSaveCancel', 'style': "display:none;"}); |
|---|
| 156 | saveCancel.innerHTML = "<div id='savingDiv' style='display:none'>saving ...</div><div id='saveButton'><input type='button' value='Save' onclick='saveField()' /> Or <input type='button' value='Cancel' onclick=' cancelSaveField()' /></div>"; |
|---|
| 157 | $(document.body).insert({top: saveCancel}); |
|---|
| 158 | |
|---|
| 159 | /*// Create status message div |
|---|
| 160 | savingDiv = new Element('div', {'id': 'savingDiv', 'class':'EIPSaveStatus', 'style': "display:none;"}); |
|---|
| 161 | savingDiv.innerHTML = "Saving ..."; |
|---|
| 162 | $(document.body).insert({top: savingDiv});*/ |
|---|
| 163 | |
|---|
| 164 | // Loop through all post titles |
|---|
| 165 | $$('.EIP_title').each(function(element){ |
|---|
| 166 | new EIPObject(element, 'EIP_title', false); |
|---|
| 167 | }); |
|---|
| 168 | |
|---|
| 169 | // Loop through all post contents |
|---|
| 170 | $$('.EIP_content').each(function(element){ |
|---|
| 171 | new EIPObject(element, 'EIP_content', true); |
|---|
| 172 | }); |
|---|
| 173 | |
|---|
| 174 | // Loop through text fields |
|---|
| 175 | $$('.EIP_textbox').each(function(element){ |
|---|
| 176 | new EIPObject(element, 'EIP_textbox', false); |
|---|
| 177 | }); |
|---|
| 178 | |
|---|
| 179 | // Loop through all post contents |
|---|
| 180 | $$('.EIP_mulittextbox').each(function(element){ |
|---|
| 181 | new EIPObject(element, 'EIP_mulittextbox', true); |
|---|
| 182 | }); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | //------------------------------------------------------------ |
|---|
| 189 | // Saving a field |
|---|
| 190 | //------------------------------------------------------------ |
|---|
| 191 | |
|---|
| 192 | function saveField(){ |
|---|
| 193 | var postParameters; |
|---|
| 194 | |
|---|
| 195 | $('savingDiv').style.display = ""; |
|---|
| 196 | $('saveButton').style.display = "none"; |
|---|
| 197 | //$('save_cancel_field').style.display = ""; |
|---|
| 198 | //$(currentItemInEdit.elementID).style.display = "none"; |
|---|
| 199 | if (currentItemInEdit.showPanel) $(currentItemInEdit.panelID).style.display = "none"; |
|---|
| 200 | |
|---|
| 201 | fieldVal = currentItemInEdit.elementDivDocument.getElementById(currentItemInEdit.elementInnerID).innerHTML; |
|---|
| 202 | |
|---|
| 203 | postParameters = |
|---|
| 204 | "post_id=" + escape(encodeURI(currentItemInEdit.postID)) + |
|---|
| 205 | "&meta_id=" + escape(encodeURI(currentItemInEdit.metaID)) + |
|---|
| 206 | "&field_value=" + escape(encodeURI(fieldVal )) + |
|---|
| 207 | "&field_type=" + escape(encodeURI(currentItemInEdit.valueType)); |
|---|
| 208 | |
|---|
| 209 | new Ajax.Request(JS_FLUTTER_URI + 'RCCWP_EditnPlaceResponse.php', |
|---|
| 210 | { |
|---|
| 211 | method:'post', |
|---|
| 212 | onSuccess: function(transport){ |
|---|
| 213 | currentItemInEdit.oldVal = fieldVal; |
|---|
| 214 | cancelSaveField(); |
|---|
| 215 | }, |
|---|
| 216 | parameters: postParameters |
|---|
| 217 | }); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | //------------------------------------------------------------ |
|---|
| 221 | // Cancel Saving |
|---|
| 222 | //------------------------------------------------------------ |
|---|
| 223 | |
|---|
| 224 | function cancelSaveField(){ |
|---|
| 225 | if (!isset(currentItemInEdit)) return; |
|---|
| 226 | currentItemInEdit.elementDivDocument.getElementById(currentItemInEdit.elementInnerID).innerHTML = currentItemInEdit.oldVal; |
|---|
| 227 | |
|---|
| 228 | $('savingDiv').style.display = "none"; |
|---|
| 229 | $('saveButton').style.display = ""; |
|---|
| 230 | $('save_cancel_field').style.display = "none"; |
|---|
| 231 | if (currentItemInEdit.showPanel) $(currentItemInEdit.panelID).style.display = "none"; |
|---|
| 232 | //$(currentItemInEdit.elementID).style.display = ""; |
|---|
| 233 | |
|---|
| 234 | currentItemInEdit.elementDivDocument.getElementById(currentItemInEdit.elementInnerID).style.backgroundColor = ""; |
|---|
| 235 | currentItemInEdit.editActive = false; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | //------------------------------------------------------------ |
|---|
| 239 | // Copy styles from an object to another |
|---|
| 240 | //------------------------------------------------------------ |
|---|
| 241 | |
|---|
| 242 | function applyStyles(from, to, stylesList){ |
|---|
| 243 | to_styles = {}; |
|---|
| 244 | stylesList.each( function (elmnt){ |
|---|
| 245 | to_styles[elmnt] = $(from).getStyle(elmnt); |
|---|
| 246 | }); |
|---|
| 247 | $(to).setStyle(to_styles); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | //------------------------------------------------------------ |
|---|
| 251 | // Recursivly apply all styles of an object as an inline |
|---|
| 252 | // styles to that object. |
|---|
| 253 | //------------------------------------------------------------ |
|---|
| 254 | |
|---|
| 255 | function recursiveApplyStyles(elmnt){ |
|---|
| 256 | stylesList = [ "backgroundAttachment", "backgroundColor","backgroundImage", "backgroundPosition","backgroundRepeat", |
|---|
| 257 | "fontSize", "fontFamily", "fontWeight", "fontStyle", |
|---|
| 258 | "color", "direction", "lineHeight", "letterSpacing", "textAlign", "textDecoration", "textIndent", "textTransform", "whiteSpace", "wordSpacing", |
|---|
| 259 | "borderBottomColor", "borderBottomStyle", "borderBottomWidth", |
|---|
| 260 | "borderLeftColor", "borderLeftStyle", "borderLeftWidth", |
|---|
| 261 | "borderTopColor", "borderTopStyle", "borderTopWidth", |
|---|
| 262 | "borderRightColor", "borderRightStyle", "borderRightWidth", |
|---|
| 263 | "listStyleImage", "listStylePosition", "listStyleType", |
|---|
| 264 | "paddingLeft", "paddingRight", "paddingTop", "paddingBottom", |
|---|
| 265 | "marginLeft", "marginRight", "marginTop", "marginBottom", |
|---|
| 266 | "left", "right", "top", "bottom", "width", |
|---|
| 267 | "clear", "cursor", "display", "float", "position", "visibility" |
|---|
| 268 | |
|---|
| 269 | ]; |
|---|
| 270 | |
|---|
| 271 | applyStyles(elmnt, elmnt, stylesList); |
|---|
| 272 | |
|---|
| 273 | childElements = elmnt.childElements(); |
|---|
| 274 | if (childElements) |
|---|
| 275 | childElements.each(recursiveApplyStyles); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | function isset( ) { |
|---|
| 280 | // http://kevin.vanzonneveld.net |
|---|
| 281 | // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
|---|
| 282 | // + improved by: FremyCompany |
|---|
| 283 | // * example 1: isset( undefined, true); |
|---|
| 284 | // * returns 1: false |
|---|
| 285 | // * example 2: isset( 'Kevin van Zonneveld' ); |
|---|
| 286 | // * returns 2: true |
|---|
| 287 | |
|---|
| 288 | var a=arguments; var l=a.length; var i=0; |
|---|
| 289 | |
|---|
| 290 | while ( i!=l ) { |
|---|
| 291 | if (typeof(a[i])=='undefined') { |
|---|
| 292 | return false; |
|---|
| 293 | } else { |
|---|
| 294 | i++; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | return true; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | //------------------------------------------------------------------------------------ |
|---|
| 304 | // Colors library |
|---|
| 305 | // Based on code from : http://www.ozzu.com/programming-forum/javascript-color-object-t66915.html |
|---|
| 306 | //------------------------------------------------------------------------------------ |
|---|
| 307 | |
|---|
| 308 | // Try to get the hex of the color |
|---|
| 309 | function GetColorHex(colorName){ |
|---|
| 310 | |
|---|
| 311 | if (!colorName || colorName == "" || colorName == "transparent") return ""; |
|---|
| 312 | if (colorName.charAt(0) == "#") return colorName; |
|---|
| 313 | |
|---|
| 314 | tmpColor = Color.getFilteredObject(colorName); |
|---|
| 315 | if (tmpColor == false){ |
|---|
| 316 | for (i=0;i<147;i=i+2){ |
|---|
| 317 | if (colorName.toLowerCase == COLOR_NAMES[i].toLowerCase) |
|---|
| 318 | return COLOR_NAMES[i+1]; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | return colorName; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | // Recursive function to get the font color |
|---|
| 325 | function GetFontColor(element){ |
|---|
| 326 | var elementBGColor = GetColorHex($(element).getStyle('color')); |
|---|
| 327 | |
|---|
| 328 | tmpColor = Color.getFilteredObject(elementBGColor); |
|---|
| 329 | if (tmpColor == false){ |
|---|
| 330 | if (isset($(element).parentNode)) |
|---|
| 331 | return GetFontColor($(element).parentNode); |
|---|
| 332 | else |
|---|
| 333 | return ''; |
|---|
| 334 | } |
|---|
| 335 | else |
|---|
| 336 | return elementBGColor; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | /* |
|---|
| 341 | Converts INT to HEX |
|---|
| 342 | If Prototype library is loaded, use theirs, else use ours. |
|---|
| 343 | */ |
|---|
| 344 | if(!Number.toColorPart){Number.prototype.toColorPart = function(){return ((this < 16 ? '0' : '') + this.toString(16));}} |
|---|
| 345 | |
|---|
| 346 | /* |
|---|
| 347 | Constructor |
|---|
| 348 | @String c : hexadecimal, shorthand hex, or rgb() |
|---|
| 349 | #returns : Object reference to instance or false |
|---|
| 350 | */ |
|---|
| 351 | Color = function(c){ |
|---|
| 352 | if(!c || !(c = Color.getFilteredObject(c))){return false;} |
|---|
| 353 | this.original = c; |
|---|
| 354 | this.r=c.r;this.g=c.g;this.b=c.b; |
|---|
| 355 | this.check(); |
|---|
| 356 | this.gray = Math.round(.3*this.r + .59*this.g + .11*this.b); |
|---|
| 357 | this.hex = this.getHex(); |
|---|
| 358 | this.rgb = this.getRGB(); |
|---|
| 359 | return this; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | /* |
|---|
| 363 | Screens color strings. |
|---|
| 364 | @String str : hexadecimal, shorthand hex, or rgb() |
|---|
| 365 | #returns : Object {r: XXX, g: XXX, b: XXX} or false |
|---|
| 366 | */ |
|---|
| 367 | Color.getFilteredObject = function(str){ |
|---|
| 368 | if(/^#?([\da-f]{3}|[\da-f]{6})$/i.test(str)){ |
|---|
| 369 | function _(s,i){return parseInt(s.substr(i,2), 16);} |
|---|
| 370 | str = str.replace(/^#/, '').replace(/^([\da-f])([\da-f])([\da-f])$/i, "$1$1$2$2$3$3"); |
|---|
| 371 | return {r:_(str,0), g:_(str,2), b:_(str,4)} |
|---|
| 372 | }else if(/^rgb *\( *\d{0,3} *, *\d{0,3} *, *\d{0,3} *\)$/i.test(str)){ |
|---|
| 373 | str = str.match(/^rgb *\( *(\d{0,3}) *, *(\d{0,3}) *, *(\d{0,3}) *\)$/i); |
|---|
| 374 | return {r:parseInt(str[1]), g:parseInt(str[2]), b:parseInt(str[3])}; |
|---|
| 375 | } |
|---|
| 376 | return false; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | /* |
|---|
| 380 | Checks the internal RGB registers for out of range values. |
|---|
| 381 | Resets out of range values. |
|---|
| 382 | #returns : Object reference to instance |
|---|
| 383 | */ |
|---|
| 384 | Color.prototype.check = function(){ |
|---|
| 385 | if(this.r>255){this.r=255;}else if(this.r<0){this.r=0;} |
|---|
| 386 | if(this.g>255){this.g=255;}else if(this.g<0){this.g=0;} |
|---|
| 387 | if(this.b>255){this.b=255;}else if(this.b<0){this.b=0;} |
|---|
| 388 | return this; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | /* |
|---|
| 392 | Resets color to the original color passed to the constructor. |
|---|
| 393 | #returns : Object reference to instance |
|---|
| 394 | */ |
|---|
| 395 | Color.prototype.revert = function(){ |
|---|
| 396 | this.r=this.original.r;this.g=this.original.g;this.b=this.original.b; |
|---|
| 397 | return this; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | /* |
|---|
| 401 | Inverts the color. |
|---|
| 402 | Black to White, vice versa |
|---|
| 403 | #returns : Object reference to instance |
|---|
| 404 | */ |
|---|
| 405 | Color.prototype.invert = function(){ |
|---|
| 406 | this.check(); |
|---|
| 407 | this.r = 255-this.r; |
|---|
| 408 | this.g = 255-this.g; |
|---|
| 409 | this.b = 255-this.b; |
|---|
| 410 | return this; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | /* |
|---|
| 414 | Lightens the color. |
|---|
| 415 | @Int amount : 1-254 -- RGB amount to lighten the color |
|---|
| 416 | #returns : Object reference to instance |
|---|
| 417 | */ |
|---|
| 418 | Color.prototype.lighten = function(amount){ |
|---|
| 419 | this.r += parseInt(amount); |
|---|
| 420 | this.g += parseInt(amount); |
|---|
| 421 | this.b += parseInt(amount); |
|---|
| 422 | return this; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | /* |
|---|
| 426 | Darkens the color. |
|---|
| 427 | @Int amount : 1-254 -- RGB amount to darken the color |
|---|
| 428 | #returns : Object reference to instance |
|---|
| 429 | */ |
|---|
| 430 | Color.prototype.darken = function(amount){ |
|---|
| 431 | return this.lighten(parseInt('-'+amount)); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | /* |
|---|
| 435 | Converts the color to Grayscale |
|---|
| 436 | #returns : Object reference to instance |
|---|
| 437 | */ |
|---|
| 438 | Color.prototype.grayscale = function(){ |
|---|
| 439 | this.check(); |
|---|
| 440 | this.gray = Math.round(.3*this.r + .59*this.g + .11*this.b); |
|---|
| 441 | this.r=this.gray;this.g=this.gray;this.b=this.gray; |
|---|
| 442 | return this; |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | /* |
|---|
| 446 | Convenience function for lightening color. |
|---|
| 447 | @Int amount : amount to lighten color |
|---|
| 448 | @Bool returnRGB : true uses RGB return string, false uses HEX return string. |
|---|
| 449 | #returns : String color |
|---|
| 450 | */ |
|---|
| 451 | Color.prototype.getLighter = function(amount, returnRGB){ |
|---|
| 452 | return this.lighten(amount).check()[returnRGB ? 'getRGB' : 'getHex'](); |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | /* |
|---|
| 456 | Convenience function for darkening color. |
|---|
| 457 | @Int amount : amount to darken color |
|---|
| 458 | @Bool returnRGB : true uses RGB return string, false uses HEX return string. |
|---|
| 459 | #returns : String color |
|---|
| 460 | */ |
|---|
| 461 | Color.prototype.getDarker = function(amount, returnRGB){ |
|---|
| 462 | return this.darken(amount).check()[returnRGB ? 'getRGB' : 'getHex'](); |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | /* |
|---|
| 466 | Convenience function for grayscaling color. |
|---|
| 467 | @Bool returnRGB : true uses RGB return string, false uses HEX return string. |
|---|
| 468 | #returns : String color |
|---|
| 469 | */ |
|---|
| 470 | Color.prototype.getGrayscale = function(returnRGB){ |
|---|
| 471 | this.grayscale(); |
|---|
| 472 | return (returnRGB ? ('rgb('+this.gray+','+this.gray+','+this.gray+')') : this.gray.toColorPart().replace(/^([\da-f]{2})$/i, "#$1$1$1")); |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | /* |
|---|
| 476 | Convenience function for inverting color. |
|---|
| 477 | @Bool returnRGB : true uses RGB return string, false uses HEX return string. |
|---|
| 478 | #returns : String color |
|---|
| 479 | */ |
|---|
| 480 | Color.prototype.getInverted = function(returnRGB){ |
|---|
| 481 | return this.invert()[returnRGB ? 'getRGB' : 'getHex'](); |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | /* |
|---|
| 485 | Gets the rgb(x,x,x) value of the color |
|---|
| 486 | #returns : String rgb color |
|---|
| 487 | */ |
|---|
| 488 | Color.prototype.getRGB = function(){ |
|---|
| 489 | this.check(); |
|---|
| 490 | this.rgb = 'rgb('+this.r+','+this.g+','+this.b+')'; |
|---|
| 491 | return this.rgb; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | /* |
|---|
| 495 | Gets the hex value of the color |
|---|
| 496 | @Bool shorthandReturnAcceptable : true will return #333 instead of #333333 |
|---|
| 497 | #returns : String hex color |
|---|
| 498 | */ |
|---|
| 499 | Color.prototype.getHex = function(shorthandReturnAcceptable){ |
|---|
| 500 | this.check(); |
|---|
| 501 | this.hex = '#' + this.r.toColorPart() + this.g.toColorPart() + this.b.toColorPart(); |
|---|
| 502 | if(shorthandReturnAcceptable){return this.hex.replace(/^#([\da-f])\1([\da-f])\2([\da-f])\3$/i, "#$1$2$3");} |
|---|
| 503 | return this.hex; |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | var COLOR_NAMES = new Array( |
|---|
| 508 | "AliceBlue" , "#F0F8FF", |
|---|
| 509 | "AntiqueWhite" , "#FAEBD7", |
|---|
| 510 | "Aqua" , "#00FFFF", |
|---|
| 511 | "Aquamarine" , "#7FFFD4", |
|---|
| 512 | "Azure" , "#F0FFFF", |
|---|
| 513 | "Beige" , "#F5F5DC", |
|---|
| 514 | "Bisque" , "#FFE4C4", |
|---|
| 515 | "Black" , "#000000", |
|---|
| 516 | "BlanchedAlmond" , "#FFEBCD", |
|---|
| 517 | "Blue" , "#0000FF", |
|---|
| 518 | "BlueViolet" , "#8A2BE2", |
|---|
| 519 | "Brown" , "#A52A2A", |
|---|
| 520 | "BurlyWood" , "#DEB887", |
|---|
| 521 | "CadetBlue" , "#5F9EA0", |
|---|
| 522 | "Chartreuse" , "#7FFF00", |
|---|
| 523 | "Chocolate" , "#D2691E", |
|---|
| 524 | "Coral" , "#FF7F50", |
|---|
| 525 | "CornflowerBlue" , "#6495ED", |
|---|
| 526 | "Cornsilk" , "#FFF8DC", |
|---|
| 527 | "Crimson" , "#DC143C", |
|---|
| 528 | "Cyan" , "#00FFFF", |
|---|
| 529 | "DarkBlue" , "#00008B", |
|---|
| 530 | "DarkCyan" , "#008B8B", |
|---|
| 531 | "DarkGoldenRod" , "#B8860B", |
|---|
| 532 | "DarkGray" , "#A9A9A9", |
|---|
| 533 | "DarkGrey" , "#A9A9A9", |
|---|
| 534 | "DarkGreen" , "#006400", |
|---|
| 535 | "DarkKhaki" , "#BDB76B", |
|---|
| 536 | "DarkMagenta" , "#8B008B", |
|---|
| 537 | "DarkOliveGreen" , "#556B2F", |
|---|
| 538 | "Darkorange" , "#FF8C00", |
|---|
| 539 | "DarkOrchid" , "#9932CC", |
|---|
| 540 | "DarkRed" , "#8B0000", |
|---|
| 541 | "DarkSalmon" , "#E9967A", |
|---|
| 542 | "DarkSeaGreen" , "#8FBC8F", |
|---|
| 543 | "DarkSlateBlue" , "#483D8B", |
|---|
| 544 | "DarkSlateGray" , "#2F4F4F", |
|---|
| 545 | "DarkSlateGrey" , "#2F4F4F", |
|---|
| 546 | "DarkTurquoise" , "#00CED1", |
|---|
| 547 | "DarkViolet" , "#9400D3", |
|---|
| 548 | "DeepPink" , "#FF1493", |
|---|
| 549 | "DeepSkyBlue" , "#00BFFF", |
|---|
| 550 | "DimGray" , "#696969", |
|---|
| 551 | "DimGrey" , "#696969", |
|---|
| 552 | "DodgerBlue" , "#1E90FF", |
|---|
| 553 | "FireBrick" , "#B22222", |
|---|
| 554 | "FloralWhite" , "#FFFAF0", |
|---|
| 555 | "ForestGreen" , "#228B22", |
|---|
| 556 | "Fuchsia" , "#FF00FF", |
|---|
| 557 | "Gainsboro" , "#DCDCDC", |
|---|
| 558 | "GhostWhite" , "#F8F8FF", |
|---|
| 559 | "Gold" , "#FFD700", |
|---|
| 560 | "GoldenRod" , "#DAA520", |
|---|
| 561 | "Gray" , "#808080", |
|---|
| 562 | "Grey" , "#808080", |
|---|
| 563 | "Green" , "#008000", |
|---|
| 564 | "GreenYellow" , "#ADFF2F", |
|---|
| 565 | "HoneyDew" , "#F0FFF0", |
|---|
| 566 | "HotPink" , "#FF69B4", |
|---|
| 567 | "IndianRed " , "#CD5C5C", |
|---|
| 568 | "Indigo " , "#4B0082", |
|---|
| 569 | "Ivory" , "#FFFFF0", |
|---|
| 570 | "Khaki" , "#F0E68C", |
|---|
| 571 | "Lavender" , "#E6E6FA", |
|---|
| 572 | "LavenderBlush" , "#FFF0F5", |
|---|
| 573 | "LawnGreen" , "#7CFC00", |
|---|
| 574 | "LemonChiffon" , "#FFFACD", |
|---|
| 575 | "LightBlue" , "#ADD8E6", |
|---|
| 576 | "LightCoral" , "#F08080", |
|---|
| 577 | "LightCyan" , "#E0FFFF", |
|---|
| 578 | "LightGoldenRodYellow" , "#FAFAD2", |
|---|
| 579 | "LightGray" , "#D3D3D3", |
|---|
| 580 | "LightGrey" , "#D3D3D3", |
|---|
| 581 | "LightGreen" , "#90EE90", |
|---|
| 582 | "LightPink" , "#FFB6C1", |
|---|
| 583 | "LightSalmon" , "#FFA07A", |
|---|
| 584 | "LightSeaGreen" , "#20B2AA", |
|---|
| 585 | "LightSkyBlue" , "#87CEFA", |
|---|
| 586 | "LightSlateGray" , "#778899", |
|---|
| 587 | "LightSlateGrey" , "#778899", |
|---|
| 588 | "LightSteelBlue" , "#B0C4DE", |
|---|
| 589 | "LightYellow" , "#FFFFE0", |
|---|
| 590 | "Lime" , "#00FF00", |
|---|
| 591 | "LimeGreen" , "#32CD32", |
|---|
| 592 | "Linen" , "#FAF0E6", |
|---|
| 593 | "Magenta" , "#FF00FF", |
|---|
| 594 | "Maroon" , "#800000", |
|---|
| 595 | "MediumAquaMarine" , "#66CDAA", |
|---|
| 596 | "MediumBlue" , "#0000CD", |
|---|
| 597 | "MediumOrchid" , "#BA55D3", |
|---|
| 598 | "MediumPurple" , "#9370D8", |
|---|
| 599 | "MediumSeaGreen" , "#3CB371", |
|---|
| 600 | "MediumSlateBlue" , "#7B68EE", |
|---|
| 601 | "MediumSpringGreen" , "#00FA9A", |
|---|
| 602 | "MediumTurquoise" , "#48D1CC", |
|---|
| 603 | "MediumVioletRed" , "#C71585", |
|---|
| 604 | "MidnightBlue" , "#191970", |
|---|
| 605 | "MintCream" , "#F5FFFA", |
|---|
| 606 | "MistyRose" , "#FFE4E1", |
|---|
| 607 | "Moccasin" , "#FFE4B5", |
|---|
| 608 | "NavajoWhite" , "#FFDEAD", |
|---|
| 609 | "Navy" , "#000080", |
|---|
| 610 | "OldLace" , "#FDF5E6", |
|---|
| 611 | "Olive" , "#808000", |
|---|
| 612 | "OliveDrab" , "#6B8E23", |
|---|
| 613 | "Orange" , "#FFA500", |
|---|
| 614 | "OrangeRed" , "#FF4500", |
|---|
| 615 | "Orchid" , "#DA70D6", |
|---|
| 616 | "PaleGoldenRod" , "#EEE8AA", |
|---|
| 617 | "PaleGreen" , "#98FB98", |
|---|
| 618 | "PaleTurquoise" , "#AFEEEE", |
|---|
| 619 | "PaleVioletRed" , "#D87093", |
|---|
| 620 | "PapayaWhip" , "#FFEFD5", |
|---|
| 621 | "PeachPuff" , "#FFDAB9", |
|---|
| 622 | "Peru" , "#CD853F", |
|---|
| 623 | "Pink" , "#FFC0CB", |
|---|
| 624 | "Plum" , "#DDA0DD", |
|---|
| 625 | "PowderBlue" , "#B0E0E6", |
|---|
| 626 | "Purple" , "#800080", |
|---|
| 627 | "Red" , "#FF0000", |
|---|
| 628 | "RosyBrown" , "#BC8F8F", |
|---|
| 629 | "RoyalBlue" , "#4169E1", |
|---|
| 630 | "SaddleBrown" , "#8B4513", |
|---|
| 631 | "Salmon" , "#FA8072", |
|---|
| 632 | "SandyBrown" , "#F4A460", |
|---|
| 633 | "SeaGreen" , "#2E8B57", |
|---|
| 634 | "SeaShell" , "#FFF5EE", |
|---|
| 635 | "Sienna" , "#A0522D", |
|---|
| 636 | "Silver" , "#C0C0C0", |
|---|
| 637 | "SkyBlue" , "#87CEEB", |
|---|
| 638 | "SlateBlue" , "#6A5ACD", |
|---|
| 639 | "SlateGray" , "#708090", |
|---|
| 640 | "SlateGrey" , "#708090", |
|---|
| 641 | "Snow" , "#FFFAFA", |
|---|
| 642 | "SpringGreen" , "#00FF7F", |
|---|
| 643 | "SteelBlue" , "#4682B4", |
|---|
| 644 | "Tan" , "#D2B48C", |
|---|
| 645 | "Teal" , "#008080", |
|---|
| 646 | "Thistle" , "#D8BFD8", |
|---|
| 647 | "Tomato" , "#FF6347", |
|---|
| 648 | "Turquoise" , "#40E0D0", |
|---|
| 649 | "Violet" , "#EE82EE", |
|---|
| 650 | "Wheat" , "#F5DEB3", |
|---|
| 651 | "White" , "#FFFFFF", |
|---|
| 652 | "WhiteSmoke" , "#F5F5F5", |
|---|
| 653 | "Yellow" , "#FFFF00", |
|---|
| 654 | "YellowGreen" , "#9ACD32"); |
|---|