palettes.icn: Procedures for programmer-defined palettes

procedure BuiltinPalette:  check for built-in palette
procedure CreatePalette:   create palette
procedure DrawImage:       draw image
procedure InitializePalett initialize palettes
procedure Measure:         measure of RGB distance
procedure NearColor:       close color in palette
procedure PaletteChars:    characters in palette
procedure PaletteColor:    color for key in palette
procedure PaletteKey:      key for color in palette
procedure RGB:             convert RGB color to record
procedure makepalette:     make palette automatically
procedure palette_colors:  list of palette colors
procedure keyseq:          sequence of palette keys
procedure color_range:     adjust RGB range
procedure colorseq:        sequence of palette colors
procedure value:           RGB magnitude

link palettes
January 23, 2001; Ralph E. Griswold
Requires: Version 9 graphics
This file is in the public domain.

These procedures implement programmer-defined palettes.  They overload
and build on top of the built-in palette mechanism.
____________________________________________________________

Data structures:

     Palette_() is a record that holds the information for a
     programmer-defined palette.  Its fields are:

             name:    the name the palette is known by
             keys:  the string of the palette characters
             table:   a table keyed by the palette characters
                      whose corresponding values are the colors

     Color_() is a record that holds the components of an RGB
     color in separate r, g, and b fields.

     PDB_ is a table whose keys are the names of programmer-
     defined palettes and whose corresponding values are the
     palettes.  PDB_ is a global variable and provides the
     way for programmer-defined palette procedures to access
     a particular database.  If it is null, a new database is
     created.

Procedures:

     BuiltinPalette(name)
             succeeds if name is the name of a built-in palette but
             fails otherwise.

     CreatePalette(name, keys, colors)
             creates a new palette with the given colors and
             corresponding keys.  The colors used are the given ones.

     InitializePalettes()
             initializes the built-in palette mechanism; it is called
             by the first palette procedure that is called.

     Measure(color1, color2) returns the a measure of the distance
              between color1 and color2 in RGB space.

     NearColor(name, color)
             returns a color close to color in the palette name.

     PaletteChars(win, palette)
             returns the palette characters of palette.  It extends
             the standard version.

     PaletteColor(win, palette, key)
             returns color in palette for the given key. It extends
             the standard version.

     PaletteKey(win, palette, color)
             returns the key in palette closest to the given color.

     RGB(color)
             parses RGB color and returns a corresponding record.

     makepalette(name, clist)
             makes a palette from the list of colors, choosing
             keys automatically.

     palette_colors(palette)

             returns the list of colors in palette.

Procedures fail in case of errors. This leaves control and error
reporting to programs that use this module.  This module is intended
to be used by programs that manage the necessary data and supply
the table through PDB_.  The problem with this is that there is
no way to differentiate errors.  A solution would be to post error
messages in a global variable.

Limitations and problems:

     The names of built-in palettes may not be used for programmer-
     defined ones.

     PaletteGrays() is not implemented for programmer-defined
     palettes.  The library version should work for built-in
     palettes with this module linked.

     Transparency is not yet implemented for DrawImage().

     ReadImage() does not yet support programmer defined palettes.

     Not tested:  Capture(), which may work.

     There is some library code that checks for the names of
     built-in palettes in an ad-hoc fashion.  It therefore is
     not advisable to use names for programmer-defined palettes
     that begin with "c" or "g" followed by a digit.

Source code | Program Library Page | Icon Home Page