Color Chooser
| Category | : Flash | Views | : 23517 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : color-chooser.zip | ||
| Result | : See the result | ||||
You can use this in customizable applications; the user clicks on the color of his choice and the sample changes to that color and the text show the hexadecimal code.

First get an image like this one:

The code works for any image, but this one is fine.
Import the image to your library and set a linkage using the identifier "colors".
Now create a new Movie Clip and draw a small square of any color you want, then drag it to the stage and give the instance name "userColor".
On the stage draw a dynamic textbox with the instance name "colorTxt", place it next to the "userColor" Movie Clip.

Paste the actions for the first frame:
//import bitmap objects and functions
import flash.display.BitmapData;
//create a bitmap object with the image in our library
colorBitmap = BitmapData.loadBitmap("colors");
//Movie Clip to hold the image
_root.createEmptyMovieClip("image", 5);
//attach the bitmap to the new Movie Clip
image.attachBitmap(colorBitmap, 5);
//don稚 use hand cursor over the image to
//be easier to choose a color
image.useHandCursor = false;
//color object linked with the userColor Movie Clip, if we
//change mcColor we also change the color of the Movie Clip
mcColor = new Color(userColor);
_root.onEnterFrame = function()
{
//if this function return 0 the mouse is not inside the image
if (colorBitmap.getPixel(_xmouse, _ymouse) != 0)
{
//explained below
mcColor.setRGB("0x" + colorBitmap.getPixel(_xmouse, _ymouse).toString(16));
}
}
mcColor.setRGB("0x" + colorBitmap.getPixel(_xmouse, _ymouse).toString(16));
This code sets the color of the Movie Clip to the hexadecimal value of the pixel in the coordinates of the mouse.
