-- TJaMColorImpl.mesa
-- last edited by Maureen Stone January 31, 1984 2:31:33 pm PST
DIRECTORY
Graphics,
TJaMGraphicsInfo,
TJaMGraphics USING [Painter],
JaM USING [PopInt, PopReal, Register, PopBool, PushReal, State],
Terminal USING [SetColorCursorPresentation,Current, GetColorMode],
GraphicsColor,
ColorMap,
TJaMGraphicsContexts USING [GProc],
WindowManager USING[StartColorViewers, StopColorViewers, ScreenPos];

TJaMColorImpl: CEDAR MONITOR
IMPORTS Graphics, JaM, Terminal, GraphicsColor, ColorMap, WindowManager, TJaMGraphicsInfo, TJaMGraphics
EXPORTS TJaMGraphicsInfo = {
OPEN P: TJaMGraphicsInfo, TDC: TJaMGraphicsContexts, J: JaM;

State: TYPE = JaM.State;

Painter: PROC[proc: TDC.GProc, frame: State ← NIL] = INLINE {
 TJaMGraphics.Painter[proc,frame]};

maxIndex: INTEGER ← MaxIndex[];
TurnOnColor: PROC[frame: State] = {
nbits: INTEGERJ.PopInt[frame];
SELECT nbits FROM
0 => WindowManager.StopColorViewers[];
1 => WindowManager.StartColorViewers[screenpos,1];
2 => WindowManager.StartColorViewers[screenpos,2];
4 => WindowManager.StartColorViewers[screenpos,4];
8 => WindowManager.StartColorViewers[screenpos,8];
24 => WindowManager.StartColorViewers[screenpos,24];
ENDCASE;
maxIndex ← MaxIndex[];
};

MaxIndex: PROC RETURNS[max: CARDINAL] = {
max ← (SELECT Terminal.GetColorMode[Terminal.Current[]].bitsPerPixelChannelA FROM
1 => 1, 2 => 3, 4 => 15, 8 => 255, ENDCASE => 0);
};


TurnOffColor: PROC [frame: State] = {WindowManager.StopColorViewers[]};
screenpos: WindowManager.ScreenPos ← left;
OnLeft: PROC [frame: State] = {
 onLeft: BOOLEANJ.PopBool[frame];
IF onLeft THEN screenpos ← left ELSE screenpos ← right;
 };

Rainbow: PROCEDURE [frame: State] = {
--fully saturated rainbow in the even values. grays in the odd values
i: NAT ← 2;
scaler: REAL ← 1.0/maxIndex;
ColorMap.StandardMap[]; --odd values are gray
UNTIL i>maxIndex DO
ColorMap.SetHSVColor[i,i*scaler,1,1];
i ← i+2;
ENDLOOP;
};

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

SetRGBColorMap: PROC [frame: State] = {
b: REALMAX[MIN[1,J.PopReal[frame]],0];
g: REALMAX[MIN[1,J.PopReal[frame]],0];
r: REALMAX[MIN[1,J.PopReal[frame]],0];
index: INTEGERJ.PopInt[frame];
index ← MAX[MIN[maxIndex,index],0];
[] ← ColorMap.SetRGBColor[index,r,g,b];
};

SetHSVColorMap: PROC [frame: State] = {
v: REALMAX[MIN[1,J.PopReal[frame]],0];
s: REALMAX[MIN[1,J.PopReal[frame]],0];
h: REALMAX[MIN[1,J.PopReal[frame]],0];
index: INTEGERJ.PopInt[frame];
index ← MAX[MIN[maxIndex,index],0];
[] ← ColorMap.SetHSVColor[index,h,s,v];
};

GetMapValue: PROC [frame: State] = {
index: INTEGERJ.PopInt[frame];
r,g,b: REAL;
[r,g,b] ← ColorMap.GetColor[MAX[MIN[maxIndex,index],0]];
J.PushReal[frame,r/255.0];
J.PushReal[frame,g/255.0];
J.PushReal[frame,b/255.0];
};

GetColor: PROC [frame: State] = {
info: P.Info ← P.GetInfo[frame];
h,s,v: REAL;
color: Graphics.Color;
paint: TDC.GProc = { color ← Graphics.GetColor[dc]};
Painter[paint,frame];
[h,s,v] ← GraphicsColor.ColorToHSV[color];
J.PushReal[frame,h];
J.PushReal[frame,s];
J.PushReal[frame,v];
};

Red: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.red]}; Painter[paint,frame]};
Green: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.green]}; Painter[paint,frame]};
Blue: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.blue]}; Painter[paint,frame]};
Magenta: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.magenta]}; Painter[paint,frame]};
Cyan: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc, GraphicsColor.cyan]}; Painter[paint,frame]};
Yellow: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.yellow]}; Painter[paint,frame]};
Black: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.black]}; Painter[paint,frame]};
White: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.white]}; Painter[paint,frame]};
Gray: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.IntensityToColor[.5]]};
Painter[paint,frame];
};

IColor: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.IntensityToColor[gray]]};
gray: REALJ.PopReal[frame];
gray ← MIN[MAX[0,gray],1];
Painter[paint,frame];
};

RGBColor: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.RGBToColor[r,g,b]]};
b: REALMAX[MIN[1,J.PopReal[frame]],0];
g: REALMAX[MIN[1,J.PopReal[frame]],0];
r: REALMAX[MIN[1,J.PopReal[frame]],0];
Painter[paint,frame];
};

HSVColor: PROC [frame: State] = {
paint: TDC.GProc ={Graphics.SetColor[dc,GraphicsColor.HSVToColor[h,s,v]]};
v: REALMAX[MIN[1,J.PopReal[frame]],0];
s: REALMAX[MIN[1,J.PopReal[frame]],0];
h: REALMAX[MIN[1,J.PopReal[frame]],0];
Painter[paint,frame];
};

JSetStipple: PROCEDURE [frame: State] = {
paint: TDC.GProc ={Graphics.SetStipple[dc, i]};
i: LONG INTEGERJ.PopInt[frame];
Painter[paint,frame];
};


BlackCursor: PROC [frame: State] = {
 [] ← Terminal.SetColorCursorPresentation[Terminal.Current[],onesAreBlack]};
WhiteCursor: PROC [frame: State] = {
 [] ← Terminal.SetColorCursorPresentation[Terminal.Current[],onesAreWhite]};


-- Initialization starts here

RegisterColor: PUBLIC PROC[frame: State] = {

J.Register[frame,".turnoncolor",TurnOnColor];
J.Register[frame,".turnoffcolor",TurnOffColor];
J.Register[frame,".onleft",OnLeft];
J.Register[frame,".rainbow",Rainbow];
J.Register[frame,".graymap",GrayMap];
J.Register[frame,".standardmap",StandardMap];
J.Register[frame,".setgamma",SetGamma];
J.Register[frame,".getgamma",GetGamma];
J.Register[frame,".setrgbmap",SetRGBColorMap];
J.Register[frame,".sethsvmap", SetHSVColorMap];
J.Register[frame,".getmapvalue", GetMapValue];
J.Register[frame,".red", Red];
J.Register[frame,".green", Green];
J.Register[frame,".blue", Blue];
J.Register[frame,".magenta", Magenta];
J.Register[frame,".cyan", Cyan];
J.Register[frame,".yellow", Yellow];
J.Register[frame,".black", Black];
J.Register[frame,".white", White];
J.Register[frame,".gray", Gray];
J.Register[frame,".icolor", IColor];
J.Register[frame,".rgbcolor", RGBColor];
J.Register[frame,".hsvcolor", HSVColor];
J.Register[frame,".blackcursor", BlackCursor];
J.Register[frame,".whitecursor", WhiteCursor];
J.Register[frame,".getcolor", GetColor];
J.Register[frame,".stipple",JSetStipple];
};


}.