Commonly Used Actionscript: Color Code
|
Set Clip Color, From senocular at http://proto.layer51.com/. When a clip is clicked it turns blue, then gradually changes to red. Click Here for a popup.
// blendRGB uses this Number prototype
// converts a (hex) number to r,g, and b.
// ftn: functions, place in the first frame of the clip
Number.prototype.HEXtoRGB = function()
{
var r = this >> 16;
var rr = this ^ r << 16;
var g = rr >> 8;
var b = rr ^ g << 8;
return {rb:r, gb:g, bb:b};
}
Color.prototype.blendRGB = function(c1,c2,t)
{
if (arguments.length == 2)
{
t = c2;
c2 = this.getRGB();
}
if (t<-1){t=-1;}
else if (t>1){t=1;}
if (t<0){t=1+t;}
c1 = c1.HEXtoRGB();
c2 = c2.HEXtoRGB();
var ct = (c1.rb+(c2.rb-c1.rb)*t) << 16 | (c1.gb+(c2.gb-c1.gb)*t) << 8 | (c1.bb+(c2.bb-c1.bb)*t);
this.setRGB(ct);
return ct;
}
Example:
// from blue to red over 100 frames with a mouse down
// ftn: onClipEvents, attach to a movie clip you want to change colors on
onClipEvent(load)
{
// make a new color object (col) for this clip
col = new Color(this);
// set doBlend variable to 0
doBlend = 0;
}
onClipEvent(enterFrame)
{
// if doBlend is true or non 0
if (doBlend)
{
// call the colors new blendRGB method. Make the color a
// range between red and blue based on doBlend
col.blendRGB(0xFF0000, 0x0000FF, doBlend);
// decrement doBlend everyframe, thereby going from 1
// to 0 and shifting the colors accordingly. Once doBlend
// is 0, the shifting will stop (from the if statement)
doBlend -= .01; // for 100 frames: 1/100 = .01
// check to make sure doBlend stops after reaching 0
//(just in case it goes past zero from the decrement)
if (doBlend < 0) {doBlend = 0;}
}
}
onClipEvent(mouseDown)
{
// when the mouse is pressed, set doBlend to 1
doBlend = 1;
}
|
Color BLENDRGB: takes two hex colors and sets the color
of the color object to a blend of those two colors
based on the 3rd argument
Arguments:
- c1, c2: (Color 1 and Color 2 (optional)) Hex values of the colors to blend. If only one
color and t is passed, the color is blended with the current color of the color object.
- t: a value between -1 and 1 which blends the passed colors together. The range is
actually 0 to 1 or 0 to -1. Using negative numbers just reverses the blend from
color 1 to color 2 to color 2 to color 1. However, in any case, a 't' of 0 is color 1.
Returns:
- the new HEX value of the color blend
Example:
red = 0xFF0000;
blue = 0x0000FF;
colorObj.blendRGB(red, blue, t);
Going from a t of 0 to 1 will go from red to blue.
Going from 0 to -1 will go from blue to red (though 0 itself is still red)
|
Random Color and Array Selection From senocular at http://www.flashkit.com. Click Here for a popup.
UltraDav:
I'll keep this straight forward. On a duplicated MC that say duplicates an arrow in all different sizes and shades of alpha, Is it possible to randomise the colour too?
code:
count = 1;
while (count<10)
{
_root.arrow.duplicateMovieClip("arrownew"+count, count);
_root["arrownew"+count]._x = random(750);
_root["arrownew"+count]._y = random(390)+10;
_root["arrownew"+count]._xscale = random(200);
_root["arrownew"+count]._yscale = 450;
_root["arrownew"+count]._alpha = random(100) ;
count += 1;
}
senocular:
Include in there:
new Color(_root["arrownew"+count]).setRGB(Math.random()*0xffffff);
To get a random color from an array, use a random number from 0 to array.length - 1;
To get that use:
random(array.length)
Then for the color you'd use:
colors = [0xFF0000, 0x00FF00, ... ];
new Color(_root["arrownew"+count]).setRGB(colors[random(colors.length)]);
//get a random array element:
ranElem = arrayName[ random(arrayName.length) ];
FTN: Here's the working code used in the example, attach to a dummy clip and be sure to export the arrow as "arrownew".
onClipEvent(load)
{
x=1;
increment=1;
count=1;
}
onClipEvent(enterFrame)
{
if(x>=5)
{
increment=-1;
initArrow = new Object();
for(i=0; i<50; i++)
{
initArrow._x = Math.random()*300;
initArrow._y = Math.random()*300;
initArrow._xscale = Math.random()*100+20;
initArrow._yscale = Math.random()*100+20;
initArrow._alpha = Math.random()*100;
initArrow._rotation = Math.random()*360;
attachMovie("arrownew", "arrownew"+i, i, initArrow);
col=new Color("arrownew"+i);
col.setRGB(Math.random()*0xffffff);
}
}
if(x<=1)
{
increment=1;
i=1;
}
x+=increment;
}
|
Change a Clip's Color, author unknown. Click Here for a popup.
/*
mc - Movie Clip to change
r - red component
g - green component
b - blue component
*/
function setColorRGB (mc, r, g, b)
{
// ftn: isNaN means "is not a number" so with the "!",
// meaning "not", the code reads "not NotaNumber",
// lol. It checks to see if r, g, and b are numbers.
if (!isNaN(r) && !isNaN(g) && !isNaN(b) && r<256 && r>=0 && g<256 && g>=0 && b<256 && b>=0)
{
r = (parseInt (r)).toString(16); r.length == 1 ? r = "0" + r : r;
g = (parseInt (g)).toString(16); g.length == 1 ? g = "0" + g : g;
b = (parseInt (b)).toString(16); b.length == 1 ? b = "0" + b : b;
col = new Color(mc);
col.setRGB( parseInt ("0x" + r + g + b));
}
}
Usage:
setColorRGB (mc, r, g, b);
Test:
onClipEvent(mouseDown)
{
// Attach to a movie clip named box
// When the mouse is pressed the clip's color is set to yellow
_root.setColorRGB (_root.box, 255, 255, 0);
}
|
Draws the Color Spectrum Line by Line by Jean-Louis Gaujal at http://proto.layer51.com/. Click Here for a popup.
//-------------------------------------------------------
// return the spectrum of colors with an angle in input
//-------------------------------------------------------
function spectre(angle)
{
r = (180-angle) / 180 * Math.PI;
var c_roug = Math.sin(r++) * 127 + 128 << 16;
var c_vert = Math.sin(r) * 127 + 128 << 8;
var c_bleu = Math.cos(r--) * 127 + 128;
return (c_roug | c_vert | c_bleu);
}
Usage:
for (var i = 0; i < 360; i++)
{
_root.lineStyle(0, spectre(i), 100);
_root.moveTo(i, 1);
_root.lineTo(i, 200);
}
|
From Podenphant at http://www.flashkit.com
newColor = "0x5B7CA7";
rgb = newColor;
red = (rgb >> 16) & 0xFF;
green = (rgb >> 8) & 0xFF;
blue = rgb & 0xFF;
for(i=0;i<5;i++)
{
factor = i * 20 - 40;
myColor = new Color(["menu" + (i+1)]);
myColor.setRGB(SetWheelColor.newColor);
brightness = new Color(menu["menu" + (i+1)]);
brightnessTransform = new Object( );
brightnessTransform.rb = red + factor;
brightnessTransform.gb = green + factor;
brightnessTransform.bb = blue + factor;
brightness.setTransform(brightnessTransform);
}
|