LoginSignup
8
7

More than 5 years have passed since last update.

2つの色の中間色を求める

Posted at

アニメーションなどで背景を赤から青へ変化させるときなどに使う関数。開始色、終了色、係数を指定して色を得る。

係数は0なら開始色と同じになり、1なら終了色と同じになり、0.5ならちょうど中間の色となる。Tweenなどで係数を0から1へ変化させるよい。

package
{
    import flash.display.Sprite;

    public class Test extends Sprite
    {

        public static function interpolateRGB(fromRGB:uint, toRGB:uint, interpolation:Number):uint
        {
            var fr:uint = (fromRGB & 0xff0000) >> 16;
            var fg:uint = (fromRGB & 0xff00) >> 8;
            var fb:uint = fromRGB & 0xff;

            var tr:uint = (toRGB & 0xff0000) >> 16;
            var tg:uint = (toRGB & 0xff00) >> 8;
            var tb:uint = toRGB & 0xff;

            if (interpolation < 0)
            {
                interpolation = 0;
            }
            else if (interpolation > 1)
            {
                interpolation = 1;
            }

            var r:uint = Math.round(fr + (tr - fr) * interpolation);
            var g:uint = Math.round(fg + (tg - fg) * interpolation);
            var b:uint = Math.round(fb + (tb - fb) * interpolation);

            return (r << 16) | (g << 8) | b;
        }
    }
}

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7