21.2 ColorConverter Interface

ORIGIN '~beta/basiclib/betaenv';
BODY 'private/colorConverterBody'
(* 
 * ColorConverter:
 *    This file implements conversion routines between the different
 *    color models.  Currently, three color models are supported:
 *    RGB, CMY and HSV.
 *
 *    The implementation of HSVtoRGB and RGBtoHSV routines
 *    are based on the Bifrost implementation (PaintImpl.bet)
 * 
 * Usage:
 *    converter: @colorConverter;
 *    ...
 *    RGBcolor->converter.RGBtoHSV->HSVcolor;
 *    ...
 *    HSVcolor->converter.HSVtoRGB->RGBcolor;
 *  
 * You can costumize this converter to convert into RGB and HSV
 * color models with different ranges for the dimensions in the
 * color models by further binding the maxRGB, MaxHue, MaxSat, and
 * maxVal virtuale in colorConverter.  E.g. to use a 256 RGB cube,
 * declare:
 * 
 *    converter: @colorConverter(# maxRGB:: (# do 256->value #);
 * 
 * Please note, that due to the duality of the RGB and CMY color models,
 * they use the same MaxRGB constant.
 *)
--- LIB: attributes ---

colorConverter:
  (# (* Contants specifying the range for RGB, hue, saturation and value *)
     MaxRGB:< IntegerValue(# do 65535->value; INNER #);
     MaxHue:< IntegerValue(# do   360->value; INNER #);
     MaxSat:< IntegerValue(# do 32768->value; INNER #); (* (2^15) *)
     MaxVal:< IntegerValue(# do 32768->value; INNER #); (* (2^15) *)
    
     HSVtoRGB: 
       (* Convert h in [0, MaxHue], s in [0, MaxSat], V in [0, MaxVal]
        * to RGB-values in [0, MaxRGB].
        * Adapted from algorithm in
        * "Foley and van Dam: Fundamentals of Interactive Computer Graphics"
        *)
       (# H, S, V: @integer;
          R, G, B: @integer;
          convert: @...
       enter (H, S, V)
       do convert
       exit (r, g, b)
       #);
     RGBtoHSV: 
       (* Obtain HSV from RGB *)
       (# R, G, B: @integer;
          H, S, V: @integer;
          convert: @...
       enter (r, g, b)
       do convert
       exit (H, S, V)
       #);
     CMYtoRGB:
       (* convert CMY color to RGB color *)
       (# C, M, Y: @integer;
          R, G, B: @integer;
          max: (* private *) @integer;
       enter (C, M, Y)
       do maxRGB->max; (max-C, max-M, max-Y)->(R, G, B)
       exit (R, G, B)
       #);
     RGBtoCMY:
       (* convert RGB color to CMY color *)
       (# R, G, B: @integer;
          C, M, Y: @integer;
          max: (* private *) @integer;
       enter (R, G, B)
       do maxRGB->max; (max-R, max-G, max-Y)->(C, M, Y)
       exit (C, M, Y)
       #);
  #)


21.2 ColorConverter Interface
© 1994-2002 Mjølner Informatics
[Modified: Thursday April 23rd 1998 at 14:56]