Articles Archive
Articles Search
Director Wiki
 

Color Picker

September 20, 2002
by Ivan Bachev

[Editor's Note: Author Ivan Bachev submitted this movie and some notes about its creation along with his programming notes. The words below aren't exactly his, but we've tried to keep the flavor of his own wording in the editing process. We'd like to encourage anyone who has a nifty Director gadget but is worried about their English writing skills to be as brave as Ivan and submit to Director Online. Remember: your English is likely to be better than our grasp of your language!]

I'm not a programmer, but I've been working with Director and Lingo for three years now. The tool I describe in this article was made as part of a project for adventurestudios.net, the company I work for.

I was working on a text formatting tool in Director and I needed to create gadgets for font selection, font color selection, etc. I didn't want to restrict the palette to just 8 or 12 colors -- I wanted the user to be able to pick virtually any color they wanted. The Australian site lingoworkshop.com has a similar tool, but I decided to make my own that was my own design.

First, I took a screen capture of a color gamma image to use as the selection canvas. Using the Imaging Lingo getPixel function in conjunction with the mouse location, I could determine the color the cursor was over if it was over the selection canvas.

This sample movie has two selection canvases (one at each side of the movie). The vector shape between them is filled with a blend. By changing the fillColor and endColor of the vector shape, I can create an almost instantaneous blend between the two selections.

Here's what's happening in the Lingo:

When the mouse goes down over a selection canvas, I begin a repeat loop that retrieves the color value and updates the stage:

on mousedown me

  repeat while the stilldown
    cursor 281

First, I change the cursor to the eyedropper (another behavior changes the cursor back to the pointer when it leaves the sprite).

    mhato = the mouseh
    mveto = the mousev
    spriteloch = sprite (the currentspritenum).loch
    spritelocv = sprite (the currentspritenum).locv
    hto = (mhato - spriteloch) + (member ("colorgama").width / 2)
    mto = (mveto - spritelocv) + (member ("colorgama").height / 2)

Next, using the mouse location and sprite position, I calculate what point of the sprite the cursor hot spot is over. This gives me horizontal and vertical offsets for the bitmap sprite's image.

    if hto < 0 then exit repeat
    if hto > member ("colorgama").width - 1 then exit repeat
    if mto < 0 then exit repeat
    if mto > member ("colorgama").height - 1 then exit repeat

This litttle section releases the repeat loop if the cursor moves beyond the bounds of the sprite. If it has, then the rest of the behavior is useless and we exit the repeat loop.

    cveta = member ("colorgama").image.getPixel (hto, mto)
    member ("vec").endcolor = cveta
    updatestage
  end repeat

end

This is the core of the behavior. Once we've determined where we are (and that we're still over the sprite) getPixel finds the color beneath the cursor from the image of the color gamma cast member, then feeds that into the vector shape cast member (this is for the selection canvas on the right, which controls the end of the vector shape's blend).

The updatestage command allows the color to be updated almost immediately (the original application wasn't written for Shockwave, or I might have gone with an exitFrame solution).The user can slide around the color palette looking for the right color.

Another successful gadget, all the tension is gone.

I hope every one will find this an easy explanation of how to select and use a color.

Director 8.5 source files are available in SIT and ZIP archive format.

All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/

Ivan has been working with Director since 1999 at adventurestudios.net.in Sofia, Bulgaria. He describes his position as "a good job, at a good place, working together with good people". Ivan hopes to build a Bulgarian-language website for the local Director development community.

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