FB II Compiler







Disk I/O














Made with FB


Represent colours by numbers

Représentation de couleurs par des nombres
Is there an existing (and simple) method of using a single number to represent color? Similar to the FB COLOR=_zBlack but with 256 values or more? I don't want to re-invent the wheel.

Pierre A Zippi


Long Color blueVal%, greenVal%, redVal%

feed it values from 0 to 65535. It is ignored on non-color Macs. See page 204 of FBII manual.

Roger M. Clary

Sorry for not being more explicit.

I am looking a method of converting a single number (code) into R&,G&,B& Where the single number (0 to 256 or 0 to 32000) is converted to RGB. I could create a lookup table where 0 to 85 cycles reds, 85 to 170 cycles blues, and 170 to 255 cycles greens. However, if don't want to create my own coloring scheme if one already exists. Much of the data for this application will already have a single number for color. I am sometimes getting data with a 'MicroStation' color code. MicroStation is a UNIX drafting package (I think). Plus, it is far easier for the end-user to choose a single number than 3. Sort of like the crayon color picker versus the RGB color picker.

Pierre A Zippi

Probably the best solution here is to create your own palette with the colors you are going to need and use CALL PMFORECOLOR (colorID) where colorID is a value from 0 to 255 and matches one of the colors in the palette. Its really quite painless to do. IM:5 has the full info.


Your best bet is to do something like this:

determine how many colors you're going to have; 1024 for instance

First this _might_ be needed; WORD means 0 to 65535, which color values are. LONG means -32767 to 32767...


Here's some globals:

dim myRed&(1024)
dim myGreen&(1024)
dim myBlue&(1024)

Initialize your look up table; if you want the first 256 to be a nice range of the shades of red, try this:

for tmp1& = 0 to 255
  myRed&(tmp1&) = tmp1& * 255 + tmp1&
  myGreen&(tmp1&) = 0
  myBlue&(tmp1&) = 0
next tmp1&

And here's your routine for setting a pixel on screen with that particular color.

fn setmypixel(ox&,oy&,oc&)
  dim myrgbrec.6
  myrgbrec.red = myRed&(oc&)
  myrgbrec.green = myGreen&(oc&)
  myrgbrec.blue = myBlue&(oc&)
  call rgbforecolor(#@myrgbrec)
  call moveto(ox&,oy&)
  call lineto(ox&,oy&)
end fn

You might want to just set it as the current color, and not a pixel(to set yourself up for a line, circle, etc.):

fn setmycolor(oc&)
  dim myrgbrec.6
  myrgbrec.red = myRed&(oc&)
  myrgbrec.green = myGreen&(oc&)
  myrgbrec.blue = myBlue&(oc&)
  call rgbforecolor(#@myrgbrec)
end fn

untested, but I've worked with rgbforecolor enough that I'm pretty darn sure it'll work.

TJ Grant

The toolbox calls COLOR2INDEX and INDEX2COLOR supply what you want. Here is a short demo. Note: the result of FN COLOR2INDEX depends on the number of colours displayed on your monitor. In 256 colour mode the result is a number 0-255.

COMPILE _dimmedVarsOnly
DIM colorNumber&,plotRGB.6


colorNumber&=FN COLOR2INDEX(plotRGB)
PRINT colorNumber&

CALL INDEX2COLOR(colorNumber&, plotRGB)
PRINT plotRGB.green%,plotRGB.blue%,plotRGB.red%



Pierre Zippi has raised the question of how my comments on this topic, sent privately to him, relate to the comments of Robert Purves. I don't know the answer, but I am posting a copy of my letter in case others can supply the link, if a link exists

"Dear Pierre,
Several people have given answers to your query. But I would like to make you aware of something which might be nearer to what you want. I became aware of it a few years ago when I needed a form of computer animation. Inside Macintosh Volume 6 of the old series has a chapter on the Palette Manager and one subheading is entitled Linking a Color Table to a Palette. I have just looked up Advanced Color Imaging in the new edition of Inside Macintosh. In the section labelled " Using Palettes with Offscreen Graphics Worlds" it states that "after setting bit 14 in the ctFlags field, each array in the ctTable fiel contains an INDEX VALUE and a color."
It seems to me that ,essentially, you assign colours to palette member entries and then call on the palette entry number and not the color. This is brought about by "Setting bit 14 in the ctFlags field of the color table record".
If you think that this information might be what you want, we could discuss it on the FB mailing list. Best wishes

Victor Maslen

[see futurebasic.6374]

Word of caution. I just finished working on a hack that uses color tables. There appears to be a mistake in the constants that refer to the color table. There is a constant called _ctTable. This constant is the offset from the color table handle to the actual data or colors in the table. I don't know what the value is defined as right now but when my program didn't work I looked it up and it didn't match Inside Mac. It was kind of confusing to figure out what it should have been but I concluded the value is 10.


Many of the FB constants are different from the ones in IM (for example, the constants for the window record). So you have to double check every constant that you use when you refer to Toolbox records. This is, needless to say, a pain in the tuckus.

If you are referring to the ctTable offset in the ColorTable structure then the offset appears to be 8, according to Think Reference.