-- TJaMColorImpl.mesa
-- last edited by Maureen Stone June 30, 1982 9:35 pm
DIRECTORY
  Graphics,
  TJaMGraphicsPrivate,
  JaMInternal USING [Frame],
  JaMOps USING [PopInteger, PopReal, RegisterExplicit, PopBoolean, defaultFrame, PushReal],
  ColorDisplay USING [SetCursorMode],
  GraphicsColor,
  ColorMap,
  ColorWorld;

TJaMColorImpl: MONITOR
IMPORTS Graphics, JaMOps, ColorDisplay, GraphicsColor, ColorMap, ColorWorld, TJaMGraphicsPrivate = {
OPEN J: JaMOps, JaMInternal, P: TJaMGraphicsPrivate;

TurnOnColor: PROC[frame: Frame] = { 
  nbits: INTEGER ← J.PopInteger[frame.opstk];
  SELECT nbits FROM
    0 => ColorWorld.TurnOffColor[];
    1 => [] ← ColorWorld.TurnOnColor[1, onLeft];
    2 => [] ← ColorWorld.TurnOnColor[2, onLeft];
    4 => [] ← ColorWorld.TurnOnColor[4, onLeft];
    8 => [] ← ColorWorld.TurnOnColor[8, onLeft];
    24 => [] ← ColorWorld.TurnOnColor[24, onLeft];
   ENDCASE;
   };

TurnOffColor: PROC [frame: Frame] = {ColorWorld.TurnOffColor[]}; 
onLeft: BOOLEAN ← TRUE;
OnLeft: PROC [frame: Frame]  = { onLeft ← J.PopBoolean[frame.opstk]};
 
Rainbow:  PROCEDURE [frame: Frame] = {
--fully saturated rainbow
   v: REAL ← 1/16;
   h: REAL ← 0;
   FOR i: INTEGER IN [1..241) DO 
       ColorMap.SetHSIColor[i,h/240,1,1];
       h ← h+1;
       ENDLOOP;
   ColorMap.SetRGBColor[0,0,0,0];	--background=black
   FOR i: INTEGER IN [241..255] DO
       ColorMap.SetRGBColor[i,v,v,v];
       v ← v+1/16;
       ENDLOOP;
   }; 

GrayMap:  PROCEDURE [frame: Frame] = {ColorMap.GrayMap[]};
StandardMap:  PROCEDURE [frame: Frame] = {ColorMap.StandardMap[]};
SetGamma: PROC [frame: Frame] = {ColorMap.SetGamma[J.PopReal[frame.opstk]]};
GetGamma: PROC [frame: Frame] = {J.PushReal[frame.opstk, ColorMap.GetGamma[]]};

SetRGBColorMap: PROC [frame: Frame] = {
  b: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  g: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  r: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  index: INTEGER ← J.PopInteger[frame.opstk];
  index ← MAX[MIN[255,index],0];
  [] ← ColorMap.SetRGBColor[index,r,g,b];
  };
  
SetHSIColorMap: PROC [frame: Frame] = {
  i: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  s: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  h: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  index: INTEGER ← J.PopInteger[frame.opstk];
  index ← MAX[MIN[255,index],0];
  [] ← ColorMap.SetHSIColor[index,h,s,i];
  };

GetColor: PROC [frame: Frame] = {
  index: INTEGER ← J.PopInteger[frame.opstk];
  r,g,b: REAL;
  [r,g,b] ← ColorMap.GetColor[index];
  J.PushReal[frame.opstk,r/255.0];
  J.PushReal[frame.opstk,g/255.0];
  J.PushReal[frame.opstk,b/255.0];
  };
  
Red: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.red]};
Green: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.green]};
Blue: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.blue]};
Magenta: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.magenta]};
Cyan: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.cyan]};
Yellow: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.yellow]};
Black: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.black]};
White: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.white]};
Gray: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame]; Graphics.SetColor[info.dc,GraphicsColor.IntensityToColor[128]]};

GrayOf: PROC [frame: Frame] = {
    info: P.Info ← P.GetInfo[frame];
    gray: REAL ← J.PopReal[frame.opstk];
    gray ← MIN[MAX[0,gray],1];
    Graphics.SetColor[info.dc,GraphicsColor.IntensityToColor[gray]]
    };

RGBColor: PROC [frame: Frame] = {
  info: P.Info ← P.GetInfo[frame];
  b: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  g: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  r: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  Graphics.SetColor[info.dc,GraphicsColor.RGBToColor[r,g,b]];
  };
  
HSLColor: PROC [frame: Frame] = {
  info: P.Info ← P.GetInfo[frame];
  i: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  s: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  h: REAL ← MAX[MIN[1,J.PopReal[frame.opstk]],0];
  Graphics.SetColor[info.dc,GraphicsColor.HSIToColor[h,s,i]];
  };
  

BlackCursor: PROC [frame: Frame] = {ColorDisplay.SetCursorMode[zeros]};
WhiteCursor: PROC [frame: Frame] = {ColorDisplay.SetCursorMode[ones]};    


-- Initialization starts here

  frame: Frame ← JaMOps.defaultFrame;
  
  J.RegisterExplicit[frame,".turnoncolor"L,TurnOnColor];
  J.RegisterExplicit[frame,".turnoffcolor"L,TurnOffColor];
  J.RegisterExplicit[frame,".onleft"L,OnLeft];
  J.RegisterExplicit[frame,".rainbow"L,Rainbow];
  J.RegisterExplicit[frame,".graymap"L,GrayMap];
  J.RegisterExplicit[frame,".standardmap"L,StandardMap];
  J.RegisterExplicit[frame,".setgamma"L,SetGamma];
  J.RegisterExplicit[frame,".getgamma"L,GetGamma];
  J.RegisterExplicit[frame,".setrgbmap"L,SetRGBColorMap];
  J.RegisterExplicit[frame,".sethsimap", SetHSIColorMap];
  J.RegisterExplicit[frame,".getcolor", GetColor];
  J.RegisterExplicit[frame,".red", Red];
  J.RegisterExplicit[frame,".green", Green];
  J.RegisterExplicit[frame,".blue", Blue];
  J.RegisterExplicit[frame,".magenta", Magenta];
  J.RegisterExplicit[frame,".cyan", Cyan];
  J.RegisterExplicit[frame,".yellow", Yellow];
  J.RegisterExplicit[frame,".black", Black];
  J.RegisterExplicit[frame,".white", White];
  J.RegisterExplicit[frame,".gray", Gray];
  J.RegisterExplicit[frame,".grayof", GrayOf];
  J.RegisterExplicit[frame,".rgbcolor", RGBColor];
  J.RegisterExplicit[frame,".hsicolor", HSLColor];
  J.RegisterExplicit[frame,".blackcursor", BlackCursor];
  J.RegisterExplicit[frame,".whitecursor", WhiteCursor];


}.