Articles Archive
Articles Search
Director Wiki

Paint Bucket Behaviors

May 2, 1999
by Pat McClellan

Dear Multimedia Handyman,

I make a multimedia piece for children that displays a black & white image. Then the child chooses a color from the corner of the screen and click on the closed area which will fill with this color.

So if it is no Lingo command (as example ... fill ), please tell me the trick. I am using Director 6. Thank you very much.

Dr. Mostafa Niazy

Dear Mostafa,

There are 2 tasks involved in this operation. First, we need to be able to "grab" a color from a color palette (like an eye-dropper function in a paint program). Second, we need to "dump" that color onto the area of the picture (similar to the "bucket" function in a paint program.)

In either case, the only way that Lingo can identify the color is by using "the forecolor of sprite x" or "the backcolor of sprite x". (With Director 7 Vector members, you have more options, but I'll keep this explanation to D6.) Notice that the forecolor and backcolor refers to the sprite -- not to the castmember. During authoring, you can set the forecolor and backcolor using Director's Tool Palette. Just select a sprite in the score, then change the color chip in the Tool Palette.

Take a look at this demo.

Download the sample movie in Mac or PC format.

The "paint chips" at the top of the screen are all the same QuickDraw shape member. I drew a single square, then dragged the same shape cast member into sprite channels 1-5. After they were in the score, I selected each one and used the Tool Palette to change its forecolor to a different hue. Keep in mind that these 5 color chips are all the same cast member, but they look different because each sprite instance has a different forecolor.

Just for fun, I dropped the "Change Pointer on Rollover" behavior from the D6 behavior library on the color chip sprites, selecting the eye-dropper cursor.

Next we need a behavior on these color chips which will "grab" the color and store it in a global variable. Here's what I used.

-- grabColor Behavior
global gColor
on mouseUp me
  set gColor = the forecolor of sprite the spriteNum of me

Yes, it's that easy. A single line of code.

Now, after clicking on one of the paint chips, the forecolor of the sprite is saved in a global variable called gColor. The next task is to assign that color to another sprite.

I'm assuming that you'll want to use bitmap cast members for the picture which is to be colored in. The trick here is that the bitmap must be converted to a 1-bit cast member. That means that only black and white data is save with the cast member. Even if you draw a cast member in the paint window using only black, that cast member will usually be saved at the bit depth you have your monitor set to. So, after drawing the cast member, you have to convert it to 1-bit. Select the cast member in the cast window, then under the Modify menu, select Transform Bitmap... and choose 1-bit.

When you drag the 1-bit member onto the stage, you'll be able to set the forecolor and back color using the tool palette (just as if it were a shape member). You'll need to have a separate sprite for each area of the drawing which will be colored. You'll have to use the "Matte" ink effect so that the irregular shapes will fit together (without making the white interior invisible).

In order to be consistent with the eye-dropper cursor, I used the "Change Pointer on Rollover" behavior on each sprite making up the picture. I assigned the bucket cursor. Here's the behavior for dumping the color on the sprite.

-- dropColor Behavior
global gColor
on mouseUp me
  set the backcolor of sprite the spriteNum of me = gColor

Again, so easy that you can do it with one line of code. One thing to note here is that you're assigning the color to the "backcolor" of the sprite -- not the forecolor. The forecolor of a 1-bit member is the line part, which we want to stay black.

Good luck with your project.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2019, Director Online. Article content copyright by respective authors.