※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


FORKED
  1. // forked from ferv's Flower
  2. package  
  3. {
  4.     import com.flashdynamix.utils.SWFProfiler;
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.BlendMode;
  8.     import flash.display.Sprite;
  9.     import flash.events.Event;
  10.     import flash.filters.ColorMatrixFilter;
  11.     
  12.     /**
  13.      * 元ネタ http://abduzeedo.com/super-cool-abstract-vectors-illustrator-and-photoshop
  14.      * 解説 http://ferv.jp/blog/2009/11/03/colormatrixfilter/
  15.      * 
  16.      * @langversion ActionScript 3.0
  17.      * @playerversion Flash 10.0
  18.      * 
  19.      * @author dsk
  20.      * @since 2009/11/01
  21.      */
  22.     [SWF(backgroundColor = '0x000000', width = '465', height = '465', frameRate = '30')]
  23.     public class Flowers extends Sprite 
  24.     {
  25.         
  26.         //--------------------------------------
  27.         // CLASS CONSTANTS
  28.         //--------------------------------------
  29.         
  30.         
  31.         //--------------------------------------
  32.         // PRIVATE VARIABLES
  33.         //--------------------------------------
  34.         
  35.         private var _bitmap:Bitmap;
  36.         private var _background:Bitmap;
  37.         private var _rotation:Number;
  38.         
  39.         
  40.         //--------------------------------------
  41.         // GETTER/SETTERS
  42.         //--------------------------------------
  43.         
  44.         
  45.         //--------------------------------------
  46.         // CONSTRUCTOR
  47.         //--------------------------------------
  48.         
  49.         public function Flowers() 
  50.         {
  51.             SWFProfiler.init(this);
  52.             
  53.             _rotation = 0;
  54.             
  55.             var i:int, flower:Flower;
  56.             for (i = 4; i >= 0; i --) {
  57.                 flower = new Flower(20 * i + 80);
  58.                 flower.x = stage.stageWidth / 2;
  59.                 flower.y = stage.stageHeight / 2;
  60.                 flower.blendMode = BlendMode.SCREEN;
  61.                 addChild(flower);
  62.             }
  63.             
  64.             _bitmap = new Bitmap(BitmapDataUtil.capture(this));
  65.             _background = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, false0));
  66.             while (numChildren > 0) removeChildAt(0);
  67.             
  68.             addChild(_background);
  69.             addChild(_bitmap);
  70.             
  71.             addEventListener(Event.ENTER_FRAME, _onEnterFrame);
  72.         }
  73.         
  74.         
  75.         //--------------------------------------
  76.         // PUBLIC METHODS
  77.         //--------------------------------------
  78.         
  79.         
  80.         //--------------------------------------
  81.         // PRIVATE METHODS
  82.         //--------------------------------------
  83.         
  84.         private function _onEnterFrame(e:Event):void 
  85.         {
  86.             _rotation += Math.PI / 180;
  87.             
  88.             var cos0:Number = (Math.cos(_rotation) + 1) / 2;
  89.             var cos1:Number = (Math.cos(_rotation + Math.PI * 2 / 3) + 1) / 2;
  90.             var cos2:Number = (Math.cos(_rotation + Math.PI * 4 / 3) + 1) / 2;
  91.             
  92.             _bitmap.filters = [new ColorMatrixFilter([
  93.                 cos0, cos1, cos2, 00,
  94.                 cos2, cos0, cos1, 00,
  95.                 cos1, cos2, cos0, 00,
  96.                 00010
  97.             ])];
  98.         }
  99.         
  100.         
  101.     }
  102.     
  103.     
  104. }
  105.     import flash.display.BitmapData;
  106.     import flash.display.BlendMode;
  107.     import flash.display.DisplayObject;
  108.     import flash.display.GraphicsPath;
  109.     import flash.display.GraphicsPathCommand;
  110.     import flash.display.GraphicsSolidFill;
  111.     import flash.display.IGraphicsData;
  112.     import flash.display.Shape;
  113.     import flash.display.Sprite;
  114.     import flash.geom.Matrix;
  115.     import flash.geom.Rectangle;
  116.     
  117.     internal class Flower extends Sprite 
  118.     {
  119.         private const PETALS_LENGTH:int = 24;
  120.         
  121.         private var _petals:Vector.<Petal>;
  122.         
  123.         public function Flower(radius:Number
  124.         {
  125.             _petals = new Vector.<Petal>();
  126.             var i:int, rotation:Number, hsv:HSV, petal:Petal;
  127.             for (i = 0; i < PETALS_LENGTH; i ++) {
  128.                 rotation = 360 * i / PETALS_LENGTH;
  129.                 
  130.                 hsv = new HSV();
  131.                 hsv.hue = rotation;
  132.                 hsv.saturation = 1;
  133.                 hsv.value = 1;
  134.                 
  135.                 petal = new Petal(radius, hsv.color);
  136.                 petal.alpha = 0.1;
  137.                 petal.rotation = rotation;
  138.                 _petals[i] = petal;
  139.                 
  140.                 addChild(petal);
  141.             }
  142.         }
  143.     }
  144.     
  145.     internal class Petal extends Shape 
  146.     {
  147.         public function Petal(radius:Number, color:uint
  148.         {
  149.             var anchorX:Number = (1 - Math.SQRT1_2) * radius;
  150.             var anchorY:Number = Math.SQRT1_2 * radius;
  151.             var handleY:Number = Math.sin(Math.PI / 8) * radius;
  152.             
  153.             graphics.drawGraphicsData(Vector.<IGraphicsData>([
  154.                 new GraphicsSolidFill(color, 1),
  155.                 new GraphicsPath(
  156.                     Vector.<int>([
  157.                         GraphicsPathCommand.MOVE_TO,
  158.                         GraphicsPathCommand.CURVE_TO,
  159.                         GraphicsPathCommand.CURVE_TO,
  160.                         GraphicsPathCommand.CURVE_TO,
  161.                         GraphicsPathCommand.CURVE_TO
  162.                     ]),
  163.                     Vector.<Number>([
  164.                         00,
  165.                         -anchorX, -anchorY + handleY, -anchorX, -anchorY,
  166.                         -anchorX, -anchorY - handleY, 0, -anchorY * 2,
  167.                         anchorX, -anchorY - handleY, anchorX, -anchorY,
  168.                         anchorX, -anchorY + handleY, 00
  169.                     ])
  170.                 )
  171.             ]));
  172.         }
  173.     }
  174.     
  175.     internal class BitmapDataUtil 
  176.     {
  177.         public static function capture(source:DisplayObject, fillColor:uint = 0x00000000, extra:int = 0):BitmapData 
  178.         {
  179.             var temp:BitmapData = new BitmapData(source.width + extra * 2, source.height + extra * 2true, 0x00000000);
  180.             var extraMatrix:Matrix = new Matrix();
  181.             extraMatrix.translate(extra, extra);
  182.             temp.draw(source, extraMatrix);
  183.             
  184.             var colored:Rectangle = temp.getColorBoundsRect(0xFF000000, 0x00000000, false);
  185.             var bitmapData:BitmapData = new BitmapData(colored.width, colored.height, true, fillColor);
  186.             var matrix:Matrix = new Matrix();
  187.             matrix.translate(-colored.x, -colored.y);
  188.             bitmapData.draw(temp, matrix);
  189.             
  190.             temp.dispose();
  191.             
  192.             return bitmapData;
  193.         }
  194.     }
  195.     
  196.     internal class HSV 
  197.     {
  198.         
  199.         //--------------------------------------
  200.         // CLASS CONSTANTS
  201.         //--------------------------------------
  202.         
  203.         
  204.         //--------------------------------------
  205.         // PRIVATE VARIABLES
  206.         //--------------------------------------
  207.         
  208.         private var _color:uint;
  209.         private var _hue:Number;
  210.         private var _saturation:Number;
  211.         private var _value:Number;
  212.         
  213.         //--------------------------------------
  214.         // GETTER/SETTERS
  215.         //--------------------------------------
  216.         
  217.         public function get color():uint { return _color; }
  218.         public function set color(value:uint):void 
  219.         {
  220.             _color = value;
  221.             _updateHSV();
  222.         }
  223.         
  224.         public function get hue():Number { return _hue; }
  225.         public function set hue(value:Number):void 
  226.         {
  227.             _hue = value;
  228.             _updateColor();
  229.         }
  230.         
  231.         public function get saturation():Number { return _saturation; }
  232.         public function set saturation(value:Number):void 
  233.         {
  234.             _saturation = value;
  235.             _updateColor();
  236.         }
  237.         
  238.         public function get value():Number { return _value; }
  239.         public function set value(value:Number):void 
  240.         {
  241.             _value = value;
  242.             _updateColor();
  243.         }
  244.         
  245.         
  246.         //--------------------------------------
  247.         // CONSTRUCTOR
  248.         //--------------------------------------
  249.         
  250.         public function HSV(color:uint = 0x000000) 
  251.         {
  252.             _color = color;
  253.             _updateHSV();
  254.         }
  255.         
  256.         public function clone():HSV 
  257.         {
  258.             return new HSV(_color);
  259.         }
  260.         
  261.         
  262.         //--------------------------------------
  263.         // PUBLIC METHODS
  264.         //--------------------------------------
  265.         
  266.         public function toString():String 
  267.         {
  268.             return '[HSV' + 
  269.                    ' color=' + toHexString() + 
  270.                    ' hue=' + _hue.toString() + 
  271.                    ' saturation=' + _saturation.toString() + 
  272.                    ' value=' + _value.toString() + ']';
  273.         }
  274.         
  275.         public function toHexString(length:int = 6):String 
  276.         {
  277.             var hex:String = _color.toString(16);
  278.             while (hex.length < length) hex = '0' + hex;
  279.             return '0x' + hex;
  280.         }
  281.         
  282.         
  283.         //--------------------------------------
  284.         // PRIVATE METHODS
  285.         //--------------------------------------
  286.         
  287.         private function _updateHSV():void
  288.         {
  289.             var rgb:RGB = new RGB(_color);
  290.             var ratioR:Number = rgb.red / 0xFF;
  291.             var ratioG:Number = rgb.green / 0xFF;
  292.             var ratioB:Number = rgb.blue / 0xFF;
  293.             
  294.             var h:Number, s:Number, v:Number;
  295.             var max:Number = Math.max(ratioR, ratioG, ratioB);
  296.             var min:Number = Math.min(ratioR, ratioG, ratioB);
  297.             var difference:Number = max - min;
  298.             if (max == ratioR) {
  299.                 h = 60 * (ratioG - ratioB) / difference;
  300.             } else if (max == ratioG) {
  301.                 h = 60 * ((ratioB - ratioR) / difference + 2);
  302.             } else {
  303.                 h = 60 * ((ratioR - ratioG) / difference + 4);
  304.             }
  305.             if (h < 0) {
  306.                 h += 360;
  307.             }
  308.             s = difference / max;
  309.             v = max;
  310.             
  311.             _hue = h;
  312.             _saturation = s;
  313.             _value = v;
  314.         }
  315.         
  316.         private function _updateColor():void
  317.         {
  318.             var h:Number = _hue;
  319.             var s:Number = _saturation;
  320.             var v:Number = _value;
  321.             
  322.             var ratioR:Number, ratioG:Number, ratioB:Number;
  323.             h %= 360;
  324.             h += (h < 0)? 3600;
  325.             if (h < 0 || h > 360trace(h);
  326.             if (s == 0) {
  327.                 ratioR = ratioG = ratioB = v;
  328.             } else {
  329.                 var hi:Number = Math.floor(h / 60) % 6;
  330.                 var f:Number = h / 60 - hi;
  331.                 var p:Number = v * (1 - s);
  332.                 var q:Number = v * (1 - f * s);
  333.                 var t:Number = v * (1 - (1 - f) * s);
  334.                 switch (hi) {
  335.                     case 0: ratioR = v; ratioG = t; ratioB = p; break;
  336.                     case 1: ratioR = q; ratioG = v; ratioB = p; break;
  337.                     case 2: ratioR = p; ratioG = v; ratioB = t; break;
  338.                     case 3: ratioR = p; ratioG = q; ratioB = v; break;
  339.                     case 4: ratioR = t; ratioG = p; ratioB = v; break;
  340.                     case 5: ratioR = v; ratioG = p; ratioB = q; break;
  341.                 }
  342.             }
  343.             
  344.             _color = Math.round(0xFF * ratioR) << 16 | Math.round(0xFF * ratioG) << 8 | Math.round(0xFF * ratioB);
  345.         }
  346.         
  347.         
  348.     }
  349.     
  350.     internal class RGB 
  351.     {
  352.         
  353.         //--------------------------------------
  354.         // CLASS CONSTANTS
  355.         //--------------------------------------
  356.         
  357.         
  358.         //--------------------------------------
  359.         // PRIVATE VARIABLES
  360.         //--------------------------------------
  361.         
  362.         private var _color:uint;
  363.         private var _red:uint;
  364.         private var _green:uint;
  365.         private var _blue:uint;
  366.         
  367.         
  368.         //--------------------------------------
  369.         // GETTER/SETTERS
  370.         //--------------------------------------
  371.         
  372.         public function get color():uint { return _color; }
  373.         public function set color(value:uint):void 
  374.         {
  375.             _color = value;
  376.             _updateRGB();
  377.         }
  378.         
  379.         public function get red():uint { return _red; }
  380.         public function set red(value:uint):void 
  381.         {
  382.             _red = value;
  383.             _updateColor();
  384.         }
  385.         
  386.         public function get green():uint { return _green; }
  387.         public function set green(value:uint):void 
  388.         {
  389.             _green = value;
  390.             _updateColor();
  391.         }
  392.         
  393.         public function get blue():uint { return _blue; }
  394.         public function set blue(value:uint):void 
  395.         {
  396.             _blue = value;
  397.             _updateColor();
  398.         }
  399.         
  400.         
  401.         //--------------------------------------
  402.         // CONSTRUCTOR
  403.         //--------------------------------------
  404.         
  405.         public function RGB(color:uint = 0x000000) 
  406.         {
  407.             _color = color;
  408.             _updateRGB();
  409.         }
  410.         
  411.         public function clone():RGB 
  412.         {
  413.             return new RGB(_color);
  414.         }
  415.         
  416.         
  417.         //--------------------------------------
  418.         // PUBLIC METHODS
  419.         //--------------------------------------
  420.         
  421.         public function toString():String 
  422.         {
  423.             return '[RGB' + 
  424.                    ' color=' + toHexString() + 
  425.                    ' red=' + _red.toString() + 
  426.                    ' green=' + _green.toString() + 
  427.                    ' blue=' + _blue.toString() + ']';
  428.         }
  429.         
  430.         public function toHexString(length:int = 6):String 
  431.         {
  432.             var hex:String = _color.toString(16);
  433.             while (hex.length < length) hex = '0' + hex;
  434.             return '0x' + hex;
  435.         }
  436.         
  437.         
  438.         //--------------------------------------
  439.         // PRIVATE METHODS
  440.         //--------------------------------------
  441.         
  442.         private function _updateRGB():void
  443.         {
  444.             _red = (_color & 0xFF0000) >> 16;
  445.             _green = (_color & 0xFF00) >> 8;
  446.             _blue = _color & 0xFF;
  447.         }
  448.         
  449.         private function _updateColor():void
  450.         {
  451.             _color = _red << 16 | _green << 8 | _blue;
  452.         }
  453.         
  454.         
  455.     }
noswf
  1. // forked from ferv's Flower
  2. package  
  3. {
  4.     import com.flashdynamix.utils.SWFProfiler;
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.BlendMode;
  8.     import flash.display.Sprite;
  9.     import flash.events.Event;
  10.     import flash.filters.ColorMatrixFilter;
  11.     
  12.     /**
  13.      * 元ネタ http://abduzeedo.com/super-cool-abstract-vectors-illustrator-and-photoshop
  14.      * 解説 http://ferv.jp/blog/2009/11/03/colormatrixfilter/
  15.      * 
  16.      * @langversion ActionScript 3.0
  17.      * @playerversion Flash 10.0
  18.      * 
  19.      * @author dsk
  20.      * @since 2009/11/01
  21.      */
  22.     [SWF(backgroundColor = '0x000000', width = '465', height = '465', frameRate = '30')]
  23.     public class Flowers extends Sprite 
  24.     {
  25.         
  26.         //--------------------------------------
  27.         // CLASS CONSTANTS
  28.         //--------------------------------------
  29.         
  30.         
  31.         //--------------------------------------
  32.         // PRIVATE VARIABLES
  33.         //--------------------------------------
  34.         
  35.         private var _bitmap:Bitmap;
  36.         private var _background:Bitmap;
  37.         private var _rotation:Number;
  38.         
  39.         
  40.         //--------------------------------------
  41.         // GETTER/SETTERS
  42.         //--------------------------------------
  43.         
  44.         
  45.         //--------------------------------------
  46.         // CONSTRUCTOR
  47.         //--------------------------------------
  48.         
  49.         public function Flowers() 
  50.         {
  51.             SWFProfiler.init(this);
  52.             
  53.             _rotation = 0;
  54.             
  55.             var i:int, flower:Flower;
  56.             for (i = 4; i >= 0; i --) {
  57.                 flower = new Flower(20 * i + 80);
  58.                 flower.x = stage.stageWidth / 2;
  59.                 flower.y = stage.stageHeight / 2;
  60.                 flower.blendMode = BlendMode.SCREEN;
  61.                 addChild(flower);
  62.             }
  63.             
  64.             _bitmap = new Bitmap(BitmapDataUtil.capture(this));
  65.             _background = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, false0));
  66.             while (numChildren > 0) removeChildAt(0);
  67.             
  68.             addChild(_background);
  69.             addChild(_bitmap);
  70.             
  71.             addEventListener(Event.ENTER_FRAME, _onEnterFrame);
  72.         }
  73.         
  74.         
  75.         //--------------------------------------
  76.         // PUBLIC METHODS
  77.         //--------------------------------------
  78.         
  79.         
  80.         //--------------------------------------
  81.         // PRIVATE METHODS
  82.         //--------------------------------------
  83.         
  84.         private function _onEnterFrame(e:Event):void 
  85.         {
  86.             _rotation += Math.PI / 180;
  87.             
  88.             var cos0:Number = (Math.cos(_rotation) + 1) / 2;
  89.             var cos1:Number = (Math.cos(_rotation + Math.PI * 2 / 3) + 1) / 2;
  90.             var cos2:Number = (Math.cos(_rotation + Math.PI * 4 / 3) + 1) / 2;
  91.             
  92.             _bitmap.filters = [new ColorMatrixFilter([
  93.                 cos0, cos1, cos2, 00,
  94.                 cos2, cos0, cos1, 00,
  95.                 cos1, cos2, cos0, 00,
  96.                 00010
  97.             ])];
  98.         }
  99.         
  100.         
  101.     }
  102.     
  103.     
  104. }
  105.     import flash.display.BitmapData;
  106.     import flash.display.BlendMode;
  107.     import flash.display.DisplayObject;
  108.     import flash.display.GraphicsPath;
  109.     import flash.display.GraphicsPathCommand;
  110.     import flash.display.GraphicsSolidFill;
  111.     import flash.display.IGraphicsData;
  112.     import flash.display.Shape;
  113.     import flash.display.Sprite;
  114.     import flash.geom.Matrix;
  115.     import flash.geom.Rectangle;
  116.     
  117.     internal class Flower extends Sprite 
  118.     {
  119.         private const PETALS_LENGTH:int = 24;
  120.         
  121.         private var _petals:Vector.<Petal>;
  122.         
  123.         public function Flower(radius:Number
  124.         {
  125.             _petals = new Vector.<Petal>();
  126.             var i:int, rotation:Number, hsv:HSV, petal:Petal;
  127.             for (i = 0; i < PETALS_LENGTH; i ++) {
  128.                 rotation = 360 * i / PETALS_LENGTH;
  129.                 
  130.                 hsv = new HSV();
  131.                 hsv.hue = rotation;
  132.                 hsv.saturation = 1;
  133.                 hsv.value = 1;
  134.                 
  135.                 petal = new Petal(radius, hsv.color);
  136.                 petal.alpha = 0.1;
  137.                 petal.rotation = rotation;
  138.                 _petals[i] = petal;
  139.                 
  140.                 addChild(petal);
  141.             }
  142.         }
  143.     }
  144.     
  145.     internal class Petal extends Shape 
  146.     {
  147.         public function Petal(radius:Number, color:uint
  148.         {
  149.             var anchorX:Number = (1 - Math.SQRT1_2) * radius;
  150.             var anchorY:Number = Math.SQRT1_2 * radius;
  151.             var handleY:Number = Math.sin(Math.PI / 8) * radius;
  152.             
  153.             graphics.drawGraphicsData(Vector.<IGraphicsData>([
  154.                 new GraphicsSolidFill(color, 1),
  155.                 new GraphicsPath(
  156.                     Vector.<int>([
  157.                         GraphicsPathCommand.MOVE_TO,
  158.                         GraphicsPathCommand.CURVE_TO,
  159.                         GraphicsPathCommand.CURVE_TO,
  160.                         GraphicsPathCommand.CURVE_TO,
  161.                         GraphicsPathCommand.CURVE_TO
  162.                     ]),
  163.                     Vector.<Number>([
  164.                         00,
  165.                         -anchorX, -anchorY + handleY, -anchorX, -anchorY,
  166.                         -anchorX, -anchorY - handleY, 0, -anchorY * 2,
  167.                         anchorX, -anchorY - handleY, anchorX, -anchorY,
  168.                         anchorX, -anchorY + handleY, 00
  169.                     ])
  170.                 )
  171.             ]));
  172.         }
  173.     }
  174.     
  175.     internal class BitmapDataUtil 
  176.     {
  177.         public static function capture(source:DisplayObject, fillColor:uint = 0x00000000, extra:int = 0):BitmapData 
  178.         {
  179.             var temp:BitmapData = new BitmapData(source.width + extra * 2, source.height + extra * 2true, 0x00000000);
  180.             var extraMatrix:Matrix = new Matrix();
  181.             extraMatrix.translate(extra, extra);
  182.             temp.draw(source, extraMatrix);
  183.             
  184.             var colored:Rectangle = temp.getColorBoundsRect(0xFF000000, 0x00000000, false);
  185.             var bitmapData:BitmapData = new BitmapData(colored.width, colored.height, true, fillColor);
  186.             var matrix:Matrix = new Matrix();
  187.             matrix.translate(-colored.x, -colored.y);
  188.             bitmapData.draw(temp, matrix);
  189.             
  190.             temp.dispose();
  191.             
  192.             return bitmapData;
  193.         }
  194.     }
  195.     
  196.     internal class HSV 
  197.     {
  198.         
  199.         //--------------------------------------
  200.         // CLASS CONSTANTS
  201.         //--------------------------------------
  202.         
  203.         
  204.         //--------------------------------------
  205.         // PRIVATE VARIABLES
  206.         //--------------------------------------
  207.         
  208.         private var _color:uint;
  209.         private var _hue:Number;
  210.         private var _saturation:Number;
  211.         private var _value:Number;
  212.         
  213.         //--------------------------------------
  214.         // GETTER/SETTERS
  215.         //--------------------------------------
  216.         
  217.         public function get color():uint { return _color; }
  218.         public function set color(value:uint):void 
  219.         {
  220.             _color = value;
  221.             _updateHSV();
  222.         }
  223.         
  224.         public function get hue():Number { return _hue; }
  225.         public function set hue(value:Number):void 
  226.         {
  227.             _hue = value;
  228.             _updateColor();
  229.         }
  230.         
  231.         public function get saturation():Number { return _saturation; }
  232.         public function set saturation(value:Number):void 
  233.         {
  234.             _saturation = value;
  235.             _updateColor();
  236.         }
  237.         
  238.         public function get value():Number { return _value; }
  239.         public function set value(value:Number):void 
  240.         {
  241.             _value = value;
  242.             _updateColor();
  243.         }
  244.         
  245.         
  246.         //--------------------------------------
  247.         // CONSTRUCTOR
  248.         //--------------------------------------
  249.         
  250.         public function HSV(color:uint = 0x000000) 
  251.         {
  252.             _color = color;
  253.             _updateHSV();
  254.         }
  255.         
  256.         public function clone():HSV 
  257.         {
  258.             return new HSV(_color);
  259.         }
  260.         
  261.         
  262.         //--------------------------------------
  263.         // PUBLIC METHODS
  264.         //--------------------------------------
  265.         
  266.         public function toString():String 
  267.         {
  268.             return '[HSV' + 
  269.                    ' color=' + toHexString() + 
  270.                    ' hue=' + _hue.toString() + 
  271.                    ' saturation=' + _saturation.toString() + 
  272.                    ' value=' + _value.toString() + ']';
  273.         }
  274.         
  275.         public function toHexString(length:int = 6):String 
  276.         {
  277.             var hex:String = _color.toString(16);
  278.             while (hex.length < length) hex = '0' + hex;
  279.             return '0x' + hex;
  280.         }
  281.         
  282.         
  283.         //--------------------------------------
  284.         // PRIVATE METHODS
  285.         //--------------------------------------
  286.         
  287.         private function _updateHSV():void
  288.         {
  289.             var rgb:RGB = new RGB(_color);
  290.             var ratioR:Number = rgb.red / 0xFF;
  291.             var ratioG:Number = rgb.green / 0xFF;
  292.             var ratioB:Number = rgb.blue / 0xFF;
  293.             
  294.             var h:Number, s:Number, v:Number;
  295.             var max:Number = Math.max(ratioR, ratioG, ratioB);
  296.             var min:Number = Math.min(ratioR, ratioG, ratioB);
  297.             var difference:Number = max - min;
  298.             if (max == ratioR) {
  299.                 h = 60 * (ratioG - ratioB) / difference;
  300.             } else if (max == ratioG) {
  301.                 h = 60 * ((ratioB - ratioR) / difference + 2);
  302.             } else {
  303.                 h = 60 * ((ratioR - ratioG) / difference + 4);
  304.             }
  305.             if (h < 0) {
  306.                 h += 360;
  307.             }
  308.             s = difference / max;
  309.             v = max;
  310.             
  311.             _hue = h;
  312.             _saturation = s;
  313.             _value = v;
  314.         }
  315.         
  316.         private function _updateColor():void
  317.         {
  318.             var h:Number = _hue;
  319.             var s:Number = _saturation;
  320.             var v:Number = _value;
  321.             
  322.             var ratioR:Number, ratioG:Number, ratioB:Number;
  323.             h %= 360;
  324.             h += (h < 0)? 3600;
  325.             if (h < 0 || h > 360trace(h);
  326.             if (s == 0) {
  327.                 ratioR = ratioG = ratioB = v;
  328.             } else {
  329.                 var hi:Number = Math.floor(h / 60) % 6;
  330.                 var f:Number = h / 60 - hi;
  331.                 var p:Number = v * (1 - s);
  332.                 var q:Number = v * (1 - f * s);
  333.                 var t:Number = v * (1 - (1 - f) * s);
  334.                 switch (hi) {
  335.                     case 0: ratioR = v; ratioG = t; ratioB = p; break;
  336.                     case 1: ratioR = q; ratioG = v; ratioB = p; break;
  337.                     case 2: ratioR = p; ratioG = v; ratioB = t; break;
  338.                     case 3: ratioR = p; ratioG = q; ratioB = v; break;
  339.                     case 4: ratioR = t; ratioG = p; ratioB = v; break;
  340.                     case 5: ratioR = v; ratioG = p; ratioB = q; break;
  341.                 }
  342.             }
  343.             
  344.             _color = Math.round(0xFF * ratioR) << 16 | Math.round(0xFF * ratioG) << 8 | Math.round(0xFF * ratioB);
  345.         }
  346.         
  347.         
  348.     }
  349.     
  350.     internal class RGB 
  351.     {
  352.         
  353.         //--------------------------------------
  354.         // CLASS CONSTANTS
  355.         //--------------------------------------
  356.         
  357.         
  358.         //--------------------------------------
  359.         // PRIVATE VARIABLES
  360.         //--------------------------------------
  361.         
  362.         private var _color:uint;
  363.         private var _red:uint;
  364.         private var _green:uint;
  365.         private var _blue:uint;
  366.         
  367.         
  368.         //--------------------------------------
  369.         // GETTER/SETTERS
  370.         //--------------------------------------
  371.         
  372.         public function get color():uint { return _color; }
  373.         public function set color(value:uint):void 
  374.         {
  375.             _color = value;
  376.             _updateRGB();
  377.         }
  378.         
  379.         public function get red():uint { return _red; }
  380.         public function set red(value:uint):void 
  381.         {
  382.             _red = value;
  383.             _updateColor();
  384.         }
  385.         
  386.         public function get green():uint { return _green; }
  387.         public function set green(value:uint):void 
  388.         {
  389.             _green = value;
  390.             _updateColor();
  391.         }
  392.         
  393.         public function get blue():uint { return _blue; }
  394.         public function set blue(value:uint):void 
  395.         {
  396.             _blue = value;
  397.             _updateColor();
  398.         }
  399.         
  400.         
  401.         //--------------------------------------
  402.         // CONSTRUCTOR
  403.         //--------------------------------------
  404.         
  405.         public function RGB(color:uint = 0x000000) 
  406.         {
  407.             _color = color;
  408.             _updateRGB();
  409.         }
  410.         
  411.         public function clone():RGB 
  412.         {
  413.             return new RGB(_color);
  414.         }
  415.         
  416.         
  417.         //--------------------------------------
  418.         // PUBLIC METHODS
  419.         //--------------------------------------
  420.         
  421.         public function toString():String 
  422.         {
  423.             return '[RGB' + 
  424.                    ' color=' + toHexString() + 
  425.                    ' red=' + _red.toString() + 
  426.                    ' green=' + _green.toString() + 
  427.                    ' blue=' + _blue.toString() + ']';
  428.         }
  429.         
  430.         public function toHexString(length:int = 6):String 
  431.         {
  432.             var hex:String = _color.toString(16);
  433.             while (hex.length < length) hex = '0' + hex;
  434.             return '0x' + hex;
  435.         }
  436.         
  437.         
  438.         //--------------------------------------
  439.         // PRIVATE METHODS
  440.         //--------------------------------------
  441.         
  442.         private function _updateRGB():void
  443.         {
  444.             _red = (_color & 0xFF0000) >> 16;
  445.             _green = (_color & 0xFF00) >> 8;
  446.             _blue = _color & 0xFF;
  447.         }
  448.         
  449.         private function _updateColor():void
  450.         {
  451.             _color = _red << 16 | _green << 8 | _blue;
  452.         }
  453.         
  454.         
  455.     }
noswf
Get Adobe Flash Player