The Liberty Basic Newsletter - Issue #16 - AUG 1998

© 2003, http://groups.yahoo.com/group/lbnews/

All Rights Reserved

Individual authors retain copyrights to their works.


Brosco's Liberty Basic Newsletter - Issue #16 - August 98


Picking Colours Using the Common Dialog in Liberty BASIC Guest Author: Alyce Watson



You've probably seen the common color dialog, if you have played around with MS Paint or similar paint programs. If you haven't seen it, here is a brief description:

The upper right side is filled with a gradiant color box. Beside it, there is a tall, thin box showing the gradations from light (at the top) to dark (at the bottom) of the currently chosen color. To use the large color box, just click the mouse in the box on a color you want. Then, choose its intensity by clicking in the gradation box to the right.

The lower right side has a preview of the color that has been chosen in the gradient box, a tattletale of the RGB value for the color and a button to press to add it to the custom colors palette.

The upper left side has a number of small boxes filled with predefined colors. You may click on one of these to use as a starting point. Try it and see what happens.

The lower left side has 16 empty boxes that you may fill with custom colors. They will be filled one after the other when you click the button to add to custom colors, or you may click on the empty box that you would like to fill or change.

It is not necessary to add colors to the empty boxes. You may simply click on a color, then close the dialog. You will only have chosen that one color, however.


The colorpk.dll provides access to this Windows color dialog. To use it in a program, you must first build a struct.

A struct is simply an array of values that you will pass into the api call to be filled. There are 16 items in the struct for this dll that correspond to those 16 empty custom color boxes in the common color dialog.

Here is the struct to build before making the call:

    open "colorpk.dll" for dll as #cp

    struct custcol, _   ' You can define upto 16 Custom colours
        c1 as long,_
        c2 as long,_
        c3 as long,_
        c4 as long,_
        c5 as long,_
        c6 as long,_
        c7 as long,_
        c8 as long,_
        c9 as long,_
        c10 as long,_
        c11 as long,_
        c12 as long,_
        c13 as long,_
        c14 as long,_
        c15 as long,_
        c16 as long

When you write "c1 as long" you are telling the dll that you want to reserve the first 32 bits of information to store the custom color in the first box. c2 will contain the 32 bit value of the color in the second box, and so on. Any unfilled box, will simply return a value of 0, which is BLACK.

Now that you have the struct to hold the color values, you may make the call:

    CallDll #cp, "ColorPk", custcol as struct, result as long

    close #cp

Its really a simple call to make. You need only send it your struct and it returns a value for the last color chosen.

This means that "result" will contain the value of the last color clicked in the gradiant box, whether it was added to the custom colors or not. So, the dll can be used to define a palette of 16 custom colors, or to retrieve only one color - the last color chosen.

First, lets talk about that last color - the one returned as "result." It is a 32 bit value. That's just fine to use in API calls to the GDI.DLL, but its useless in Liberty BASIC graphics functions. Remember that we need the color in RGB value form to use it in LB.

The syntax to use RGB color in LB is:

	print #1, "color 100 150 33"

The first number represents red, the second is green and the third is blue. They may each have a value from 0 to 255. The color 0 0 0 is pure black and the color 255 255 255 is white.

Brosco has thoughtfully provided us with a formula to change that 32 bit "long" color value into its red, green and blue components:

    blue = int(result / (256 * 256))
    green = int((result - blue *256*256) / 256)
    red = result - blue *256 * 256 - green * 256

Now, your LB command can read:

	print #1, "color ";red;" ";green;" ";blue

Some of you are muttering to yourselves now (yes, I can hear you!) saying that is fine for the result, but what about that struct thing? Here's the way to retrieve your custom color palette from the struct:

    colorOne = custcol.c1.struct
    colorTwo = custcol.c2.struct

Well, you get the idea. Just do it for all 16 items in the struct. Now, you have the 32 bit color values of all 16 custom colors, ready to use in calls to GDI. To retrieve the RGB value from these "long" values, use the same formula you used earlier:

    blueOne = int(colorOne / (256 * 256))
    greenOne = int((colorOne - blueOne *256*256) / 256)
    redOne = colorOne - blueOne *256 * 256 - greenOne * 256

There is a sample program attached called custom.bas that uses colorpk.dll and calls to gdi.dll to make a color palette. Here is a sample program that uses LB's own color command to retrieve the last color chosen and use it in a graphic window:



nomainwin

notice "Click in the gradiant box to choose a color, "_
    +"then choose an intensity from the box on the right, "_
    +"then click the OK button."
    open "colorpk.dll" for dll as #cp

    struct custcol, _
        c1 as long,_
        c2 as long,_
        c3 as long,_
        c4 as long,_
        c5 as long,_
        c6 as long,_
        c7 as long,_
        c8 as long,_
        c9 as long,_
        c10 as long,_
        c11 as long,_
        c12 as long,_
        c13 as long,_
        c14 as long,_
        c15 as long,_
        c16 as long

    CallDll #cp, "ColorPk", custcol as struct, result as long

    close #cp


    blue = int(result / (256 * 256))
    green = int((result - blue *256*256) / 256)
    red = result - blue *256 * 256 - green * 256

UpperLeftX=10:UpperLeftY=10
WindowWidth=400:WindowHeight=400
open "Pick a Color" for graphics_nsb as #1
    print #1, "trapclose [quit]"
    print #1, "down; size 100"
    print #1, "color ";red;" ";green;" ";blue
    print #1, "line 0 100 400 300"
    print #1, "flush"

input a$

[quit]
    close #1:end


Newsletter written by: Alyce.

Comments, requests or corrections mailto:brosco@orac.net.au

mailto:awatson@wctc.net

Translated from American to English by an Australian:

Cliff Bros - Chief Editor. Thanks Cliff.