/* * Application: ColorToolkit.as * ------------------------------------------------- * Version: 1.2 * ------------------------------------------------- * Latest update: October 29, 2002 * ------------------------------------------------- * Developer: Richard Wright [wisolutions2002@shaw.ca] * ------------------------------------------------- * Copyright: (c) 2002 Robert Penner [www.robertpenner.com] * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ------------------------------------------------------ * * MovieClip getter/setter properties: * * _brightness * _brightOffset * _negativeColor * * _rgb * _red * _green * _blue * * _redPercent * _greenPercent * _bluePercent * * _redOffset * _greenOffset * _blueOffset * * Color methods: * * 1. setRGBStr(hexStr) * 2. getRGBStr() * 3. setRGB2(r,g,b) * 4. getRGB2() * 5. reset() * * 6. setBrightness(bright) * 7. getBrightness() * 8. setBrightOffset(offset) * 9. getBrightOffset() * * 10. setTint(r,g,b,percent) * 11. getTint() * 12. setTint2(rgb,percent) * 13. getTint2() * 14. setTintOffset(r,g,b) * 15. getTintOffset() * * 16. invert() * 17. setNegative(percent) * 18. getNegative() * * 19. setRed(amount) * 20. getRed() * 21. setGreen(amount) * 22. getGreen() * 23. setBlue(amount) * 24. getBlue() * * 25. setRedPercent(percent) * 26. getRedPercent() * 27. setGreenPercent(percent) * 28. getGreenPercent() * 29. setBluePercent(percent) * 30. getBluePercent() * * 31. setRedOffset(offset) * 32. getRedOffset() * 33. setGreenOffset(offset) * 34. getGreenOffset() * 35. setBlueOffset(offset) * 36. getBlueOffset() * * * MovieClip methods: * * 37. setRGB(col) * 38. getRGB() * 39. setRGBStr(hexStr) * 40. getRGBStr() * 41. setRGB2(r,g,b) * 42. getRGB2() * 43. resetColor() * 44. setColorTransform(trans) * 45. getColorTransform() * * 46. setBrightness(bright) * 47. getBrightness() * 48. setBrightOffset(offset) * 49. getBrightOffset() * * 50. setTint(r,g,b,percent) * 51. getTint() * 52. setTint2(rgb,percent) * 53. getTint2() * 54. setTintOffset(r,g,b) * 55. getTintOffset() * * 56. invertColor() * 57. setNegativeColor(percent) * 58. getNegativeColor() * * 59. setRed(amount) * 60. getRed() * 61. setGreen(amount) * 62. getGreen() * 63. setBlue(amount) * 64. getBlue() * * 65. setRedPercent(percent) * 66. getRedPercent() * 67. setGreenPercent(percent) * 68. getGreenPercent() * 69. setBluePercent(percent) * 70. getBluePercent() * * 71. setRedOffset(offset) * 72. getRedOffset() * 73. setGreenOffset(offset) * 74. getGreenOffset() * 75. setBlueOffset(offset) * 76. getBlueOffset() * * Discussed in Chapter 9 of * Robert Penner's Programming Macromedia Flash MX * * http://www.robertpenner.com/profmx * http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20 * ----------------------------------------------------- * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ----------------------------------------------------- */ // ====== solid color - Color methods ====== // 1. setRGBStr Color.prototype.setRGBStr = function(hexStr) { // grab the last six characters of the string hexStr = hexStr.substr(-6,6); this.setRGB(parseInt(hexStr,16)); }; // 2. getRGBStr Color.prototype.getRGBStr = function() { var hexStr = this.getRGB().toString(16); // fill in zeroes as needed var toFill = 6-hexStr.length; while (toFill--) hexStr = "0"+hexStr; return hexStr.toUpperCase(); }; // 3. setRGB2 - set red, green, and blue with normal numbers // r, g, b between 0 and 255 Color.prototype.setRGB2 = function(r,g,b) { this.setRGB (r<<16 | g<<8 | b); }; // Branden Hall - www.figleaf.com // 4. getRGB2 - returns an object with r, g, and b properties Color.prototype.getRGB2 = function() { var t = this.getTransform(); return {r:t.rb, g:t.gb, b:t.bb}; }; // 5. reset - reset the color object to normal Color.prototype.reset = function() { this.setTransform ({ra:100,ga:100,ba:100,rb:0,gb:0,bb:0}); }; // ====== brightness - Color methods ====== // 6. setBrightness - brighten just like Property Inspector // bright between -100 and 100 Color.prototype.setBrightness = function(bright) { var trans = this.getTransform(); with (trans) { ra = ga = ba = 100-Math.abs(bright); // color percent rb = gb = bb = (bright>0) ? bright*(256/100) : 0; // color offset } this.setTransform(trans); }; // 7. getBrightness Color.prototype.getBrightness = function() { var trans = this.getTransform(); with (trans) return rb ? 100-ra : ra-100; }; // ====== brightOffset - Color methods ====== // 8. setBrightOffset - offset between -255 and 255 Color.prototype.setBrightOffset = function(offset) { var trans = this.getTransform() with (trans) rb = gb = bb = offset; this.setTransform(trans); }; // 9. getBrightOffset Color.prototype.getBrightOffset = function() { return this.getTransform().rb; }; // ====== tint - Color methods ====== // 10. setTint - tint with a color just like Property Inspector // r, g, b between 0 and 255; percent between 0 and 100 Color.prototype.setTint = function(r,g,b,percent) { var ratio = percent/100; var trans = {rb:r*ratio,gb:g*ratio,bb:b*ratio}; trans.ra = trans.ga = trans.ba = 100-percent; this.setTransform(trans); }; // 11. getTint - returns a tint object containing r, g, b, and percent properties Color.prototype.getTint = function() { var trans = this.getTransform(); var tint = {percent: 100-trans.ra}; var ratio = 100/tint.percent; tint.r = trans.rb*ratio; tint.g = trans.gb*ratio; tint.b = trans.bb*ratio; return tint; }; // 12. setTint2 - tint with a color - alternate approach // rgb a color number between 0 and 0xFFFFFF; percent between 0 and 100 Color.prototype.setTint2 = function(rgb,percent) { var r = (rgb>>16) ; var g = (rgb>>8) & 0xFF; var b = rgb & 0xFF; var ratio = percent/100; var trans = {rb:r*ratio,gb:g*ratio,bb:b*ratio}; trans.ra = trans.ga = trans.ba = 100-percent; this.setTransform(trans); }; // 13. getTint2 - returns a tint object containing rgb (a 0xFFFFFF number) and percent properties Color.prototype.getTint2 = function() { var trans = this.getTransform(); var tint = {percent:100-trans.ra}; var ratio = 100/tint.percent; tint.rgb = (trans.rb*ratio)<<16 | (trans.gb*ratio)<<8 | trans.bb*ratio; return tint; }; // ====== tint offset - Color methods ====== // 14. setTintOffset - r, g, b between -255 and 255 Color.prototype.setTintOffset = function(r,g,b) { var trans = this.getTransform(); with (trans) {rb = r;gb = g;bb = b;} this.setTransform(trans); }; // 15. getTintOffset Color.prototype.getTintOffset = function() { var t = this.getTransform(); return {r:t.rb,g:t.gb,b:t.bb}; }; // ====== color inversion - Color methods ====== // 16. invert - invert the current color values Color.prototype.invert = function() { var trans = this.getTransform(); with (trans) { ra = -ra; ga = -ga; ba = -ba; rb = 255-rb; gb = 255-gb; bb = 255-bb; } this.setTransform(trans); }; // 17. setNegative - produce a negative image of the normal appearance Color.prototype.setNegative = function(percent) { var t = {}; t.ra = t.ga = t.ba = 100-2*percent; t.rb = t.gb = t.bb = percent*(255/100); this.setTransform(t); }; // 18. getNegative Color.prototype.getNegative = function() { return this.getTransform().rb*(100/255); }; // ====== solid color - Color methods ====== // 19. setRed Color.prototype.setRed = function(amount) { var t = this.getTransform(); this.setRGB (amount<<16 | t.gb<<8 | t.bb); }; // 20. getRed Color.prototype.getRed = function() { return this.getTransform().rb; }; // 21. setGreen Color.prototype.setGreen = function(amount) { var t = this.getTransform(); this.setRGB (t.rb<<16 | amount<<8 | t.bb); }; // 22. getGreen Color.prototype.getGreen = function() { return this.getTransform().gb; }; // 23. setBlue Color.prototype.setBlue = function(amount) { var t = this.getTransform(); this.setRGB (t.rb<<16 | t.gb<<8 | amount); }; // 24. getBlue Color.prototype.getBlue = function() { return this.getTransform().bb; }; // ====== color percent - Color methods ====== // 25. setRedPercent Color.prototype.setRedPercent = function(percent) { var trans = this.getTransform(); trans.ra = percent; this.setTransform(trans); }; // 26. getRedPercent Color.prototype.getRedPercent = function() { return this.getTransform().ra; }; // 27. setGreenPercent Color.prototype.setGreenPercent = function(percent) { var trans = this.getTransform(); trans.ga = percent; this.setTransform(trans); }; // 28. getGreenPercent Color.prototype.getGreenPercent = function() { return this.getTransform().ga; }; // 29. setBluePercent Color.prototype.setBluePercent = function(percent) { var trans = this.getTransform(); trans.ba = percent; this.setTransform(trans); }; // 30. getBluePercent Color.prototype.getBluePercent = function() { return this.getTransform().ba; }; // ====== color offset - Color methods ====== // 31. setRedOffset Color.prototype.setRedOffset = function(offset) { var trans = this.getTransform(); trans.rb = offset; this.setTransform(trans); }; // 32. getRedOffset Color.prototype.getRedOffset = function() { return this.getTransform().rb; }; // 33. setGreenOffset Color.prototype.setGreenOffset = function(offset) { var trans = this.getTransform(); trans.gb = offset; this.setTransform(trans); }; // 34. getGreenOffset Color.prototype.getGreenOffset = function() { return this.getTransform().gb; }; // 35. setBlueOffset Color.prototype.setBlueOffset = function(offset) { var trans = this.getTransform(); trans.bb = offset; this.setTransform(trans); }; // 36. getBlueOffset Color.prototype.getBlueOffset = function() { return this.getTransform().bb; }; ////////////////////////////////////////////////////////////////// // ====== solid color - MovieClip methods ====== // 37. setRGB MovieClip.prototype.setRGB = function(col) { (new Color(this)).setRGB(col); }; // 38. getRGB MovieClip.prototype.getRGB = function() { return (new Color(this)).getRGB(); }; // 39. setRGBStr MovieClip.prototype.setRGBStr = function(hexStr) { (new Color(this)).setRGBStr(hexStr); }; // 40. getRGBStr MovieClip.prototype.getRGBStr = function() { return (new Color(this)).getRGBStr(); }; // 41. v - set red, green, and blue with normal numbers // r, g, b between 0 and 255 MovieClip.prototype.setRGB2 = function(r,g,b) { (new Color(this)).setRGB2(r,g,b); }; // 42. getRGB2 - returns an object with r, g, and b properties MovieClip.prototype.getRGB2 = function() { return (new Color(this)).getRGB2(); }; // 43. v - invert current color values MovieClip.prototype.resetColor = function() { (new Color(this)).reset(); }; // ====== color transform - MovieClip methods ====== // 44. setColorTransform MovieClip.prototype.setColorTransform = function(trans) { (new Color(this)).setTransform(trans); }; // 45. getColorTransform MovieClip.prototype.getColorTransform = function() { return (new Color(this)).getTransform(); }; // ====== brightness - MovieClip methods ====== // 46. v - brighten just like Property Inspector // bright between -100 and 100 MovieClip.prototype.setBrightness = function(bright) { (new Color(this)).setBrightness(bright); }; // 47. getBrightness MovieClip.prototype.getBrightness = function() { return (new Color(this)).getBrightness(); }; // ====== brightOffset - MovieClip methods ====== // 48. setBrightOffset - offset between -255 and 255 MovieClip.prototype.setBrightOffset = function(offset) { (new Color(this)).setBrightOffset(offset); }; // 49. getBrightOffset MovieClip.prototype.getBrightOffset = function() { return (new Color(this)).getBrightOffset(); }; // ====== tint - MovieClip methods ====== // 50. setTint - tint with a color just like Property Inspector // r, g, b between 0 and 255; percent between 0 and 100 MovieClip.prototype.setTint = function(r,g,b,percent) { (new Color(this)).setTint(r,g,b,percent); }; // 51. getTint - returns a tint object containing r, g, b, and percent properties MovieClip.prototype.getTint = function() { return (new Color(this)).getTint(); }; // 52. setTint2 - tint with a color - alternate approach // rgb a color number between 0 and 0xFFFFFF; percent between 0 and 100 MovieClip.prototype.setTint2 = function(rgb,percent) { (new Color(this)).setTint2 (rgb,percent); }; // 53. getTint2 - returns a tint object containing rgb (a 0xFFFFFF number) and percent properties MovieClip.prototype.getTint2 = function() { return (new Color(this)).getTint2(); }; // ====== tint offset - MovieClip methods ====== // 54. setTintOffset - r, g, b between -255 and 255 MovieClip.prototype.setTintOffset = function(r,g,b) { (new Color(this)).setTintOffset(r,g,b); }; // 55. getTintOffset - returns a tint offset object containing r, g, and b properties MovieClip.prototype.getTintOffset = function() { return (new Color(this)).getTintOffset(); }; // ====== color inversion - MovieClip methods ====== // 56. invertColor - invert current color values MovieClip.prototype.invertColor = function() { (new Color(this)).invert(); }; // 57. setNegativeColor - produce a negative image of the normal appearance MovieClip.prototype.setNegativeColor = function(percent) { (new Color(this)).setNegative (percent); }; // 58. getNegativeColor MovieClip.prototype.getNegativeColor = function() { return (new Color(this)).getNegative(); }; // ====== solid color - MovieClip methods ====== // 59. setRed MovieClip.prototype.setRed = function(amount) { (new Color(this)).setRed(percent); }; // 60. getRed MovieClip.prototype.getRed = function() { return (new Color(this)).getRed(); }; // 61. setGreen MovieClip.prototype.setGreen = function(amount) { (new Color(this)).setGreen(percent); }; // 62. getGreen MovieClip.prototype.getGreen = function() { return (new Color(this)).getGreen(); }; // 63. setBlue MovieClip.prototype.setBlue = function(amount) { (new Color(this)).setBlue(percent); }; // 64. getBlue MovieClip.prototype.getBlue = function() { return (new Color(this)).getBlue(); }; // ====== color percent - MovieClip methods ====== // 65. setRedPercent MovieClip.prototype.setRedPercent = function(percent) { (new Color(this)).setRedPercent(percent); }; // 66. getRedPercent MovieClip.prototype.getRedPercent = function() { return (new Color(this)).getRedPercent(); }; // 67. setGreenPercent MovieClip.prototype.setGreenPercent = function(percent) { (new Color(this)).setGreenPercent(percent); }; // 68. getGreenPercent MovieClip.prototype.getGreenPercent = function() { return (new Color(this)).getGreenPercent(); }; // 69. setBluePercent MovieClip.prototype.setBluePercent = function(percent) { (new Color(this)).setBluePercent(percent); }; // 70. getBluePercent MovieClip.prototype.getBluePercent = function() { return (new Color(this)).getBluePercent(); }; // ====== color offset - MovieClip methods ====== // 71. setRedOffset MovieClip.prototype.setRedOffset = function(offset) { (new Color(this)).setRedOffset(offset); }; // 72. getRedOffset MovieClip.prototype.getRedOffset = function() { return (new Color(this)).getRedOffset(); }; // 73. setGreenOffset MovieClip.prototype.setGreenOffset = function(offset) { (new Color(this)).setGreenOffset(offset); }; // 74. getGreenOffset MovieClip.prototype.getGreenOffset = function() { return (new Color(this)).getGreenOffset(); }; // 75. setBlueOffset MovieClip.prototype.setBlueOffset = function(offset) { (new Color(this)).setBlueOffset (offset); }; // 76. getBlueOffset MovieClip.prototype.getBlueOffset = function() { return (new Color(this)).getBlueOffset(); }; // =========================================================== // create MovieClip color properties // associate getter/setter methods with property names with (MovieClip.prototype) { addProperty ("_brightness",getBrightness,setBrightness); addProperty ("_brightOffset",getBrightOffset,setBrightOffset); addProperty ("_negativeColor",getNegativeColor,setNegativeColor); addProperty ("_rgb",getRGB,setRGB); addProperty ("_red",getRed,setRed); addProperty ("_green",getGreen,setGreen); addProperty ("_blue",getBlue,setBlue); addProperty ("_redPercent",getRedPercent,setRedPercent); addProperty ("_greenPercent",getGreenPercent,setGreenPercent); addProperty ("_bluePercent",getBluePercent,setBluePercent); addProperty ("_redOffset",getRed,setRedOffset); addProperty ("_greenOffset",getGreen,setGreenOffset); addProperty ("_blueOffset",getBlue,setBlueOffset); } // hide stuff from for..in loops ASSetPropFlags (MovieClip.prototype,null,1); //trace ("-- Color Toolkit loaded --");