A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Color Cycle

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5

    Color Cycle

    Hey All,

    I'm looking for an effect that I believe is relatively simple in AS3. I have solid color JPG that I'd like to automatically cycle through different hues every 5 seconds via actionscript. I found an informative post here on Flash Kit. However, it wasn't exactly what I was looking for and I wanted to find out if there was a way of compacting the code.

    Any help as always is appreciated.

    Trey

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I love it when I think "Oh, I can probably help with that", then look and find out that it was me in the first place.

    Why do you use a JPG for a solid color? PNG would likely be smaller and look better if you need an image at all. Otherwise, just generate your bitmap and fill it with the desired value.

    You can use BitmapData.fillRect to fill a bitmapdata with a single color. So you just need to define how to get the next color from your current color.

    Define "cycle through different hues". What hues? Do you actually mean hue as in HSV?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Hey 5tons,

    Thanks for the info and the suggestion about using PNG's in this case.

    When I refer to different hues I'm really just talking about colors in general. I'm looking for a way to have the colors change every 5 seconds. I know this is easily done with a tween, but I wanted to know the specific AS3 code to do it.

    Thanks again,

    T

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Here's what I'd do if I wanted a patch of color.
    Code:
    var patchWidth:int = 50;
    var patchHeight = 50;
    var patch:Sprite = new Sprite();
    addChild(patch);
    
    function drawPatch(color:uint):void{
      var g:Graphics = patch.graphics;
      g.clear();
      g.beginFill(color);
      g.drawRect(0,0,patchWidth, patchHeight);
      g.endFill();
    }
    Then, just call drawPatch with whatever new color you want. Let's say you have N randomly generated colors, and want to switch between them every T seconds.
    Code:
    var n:int = 12;
    var colors:Array = [];
    for (var i:int = 0; i < n; i++){
      colors.push(Math.floor(Math.random()*0xFFFFFF));
    }
    var t:int = 5000;
    var colorIndex:int = 0;
    
    var timer:Timer = new Timer(t);
    timer.addEventListener(TimerEvent.TIMER, nextColor);
    timer.start();
    
    function nextColor(e:TimerEvent):void{
      var c:uint = colors[colorIndex];
      colorIndex = (colorIndex + 1) % colors.length; //wraps around.
      drawPatch(c);
    }

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks much 5tons. I appreciate the assistance as always.

    T

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center