ChipmonkColorMap.mesa
Copyright © 1983, 1984 by Xerox Corporation. All rights reserved.
by Ch. Jacobi October 28, 1983 4:21 pm
last edited by Christian Jacobi, May 21, 1985 3:59:53 pm PDT
DIRECTORY
CDIO,
CDMenus,
CDSequencer,
Commander USING [CommandProc, Register],
Convert,
FS,
IO,
Rope,
Terminal,
TerminalIO,
UserProfile;
ChipmonkColorMap:
CEDAR
PROGRAM
IMPORTS CDMenus, CDIO, CDSequencer, Commander, Convert, FS, IO, Rope, Terminal, TerminalIO, UserProfile =
BEGIN
colorTabsNum: CARDINAL = 40;
ColorTabs: TYPE = ARRAY [0..colorTabsNum) OF ARRAY [0..49) OF CARDINAL;
cTabCnt: CARDINAL ← 0;
colorTabs: REF ColorTabs = NEW[ColorTabs];
ColorMode4:
PROC []
RETURNS [
BOOL] =
INLINE
BEGIN
virtual: Terminal.Virtual = Terminal.Current[];
RETURN [virtual.hasColorDisplay AND ~virtual.GetColorMode[].full AND virtual.GetColorMode[].bitsPerPixelChannelA=4];
END;
SetCMColorMap:
PROC [val:
INT←-1, file: Rope.
ROPE←
NIL] =
-- val<0 : read it
-- val>=0 : use it
BEGIN
SetCMColorMapEntry:
PROC [i:
INT] =
BEGIN
Range:
PROC [c:
CARDINAL]
RETURNS [
CARDINAL] =
BEGIN
RETURN [ 255-MIN[c, 255] ]
END;
virtual: Terminal.Virtual = Terminal.Current[];
IF ~ColorMode4[] THEN RETURN;
IF i=0 THEN {TerminalIO.WriteRope["Don't use this command to reset default\n"]; RETURN};
IF i>0
AND i<=cTabCnt
THEN {
ColorMap.SetGamma[1.0];
i ← i-1;
FOR j:
CARDINAL
IN [0..16)
DO
r: REAL = MAX[0, MIN[1, 1.0/255*(255-colorTabs[i][1+j*3])]];
g: REAL = MAX[0, MIN[1, 1.0/255*(255-colorTabs[i][2+j*3])]];
b: REAL = MAX[0, MIN[1, 1.0/255*(255-colorTabs[i][3+j*3])]];
ColorMap.SetRGBColor[index: j, r:r, g:g, b:b];
Terminal.SetColor[vt: virtual,
aChannelValue: j,
bChannelValue: 0,
red: Range[colorTabs[i][1+j*3]],
green: Range[colorTabs[i][2+j*3]],
blue: Range[colorTabs[i][3+j*3]]
];
ENDLOOP;
ColorMap.SetGamma[2.2];
}
ELSE TerminalIO.WriteRope["**color map number out of range\n"];
END;
--SetCMColorMap
IF cTabCnt=0 THEN ReadColorMapFile[file];
IF cTabCnt=0 THEN {TerminalIO.WriteRope["no color map table read\n"]; RETURN};
IF val<0
THEN {
TerminalIO.WriteRope["color map ( <="];
TerminalIO.WriteInt[cTabCnt];
TerminalIO.WriteRope["; often used: cmos: 2; nmos: 1)"];
TerminalIO.WriteRope[") > "];
val ← TerminalIO.RequestInt["" ! TerminalIO.UserAbort => GOTO UserAbort];
};
SetCMColorMapEntry[val]
EXITS
UserAbort => {NULL};
END;
ReadColorMapFile:
PROC [fileName: Rope.
ROPE←
NIL] =
BEGIN
binfile: IO.STREAM;
k, i: CARDINAL;
GetByte:
PROC []
RETURNS[c: [0..255]] =
INLINE {
c ← LOOPHOLE[IO.GetChar[binfile], [0..255]];
};
Get2Bytes:
PROC []
RETURNS [c:
CARDINAL] =
INLINE {
h: [0..255] = GetByte[];
c ← h*256+GetByte[];
};
IF Rope.IsEmpty[fileName]
THEN fileName←UserProfile.Token[
key: "ChipNDale.ChipmonkColorMaps",
default: "default.CDColorMaps"
];
fileName ← CDIO.MakeName[base: fileName, ext: "CDColorMaps", wDir: CDIO.GetWorkingDirectory[NIL]];
binfile ← FS.StreamOpen[fileName ! FS.Error =>
IF error.group # bug
THEN {
TerminalIO.WriteRope[fileName];
TerminalIO.WriteRope[" not opened: "];
TerminalIO.WriteRope[error.explanation];
TerminalIO.WriteLn[];
GOTO FileName
}];
cTabCnt ← 0;
k ← MIN[Get2Bytes[], colorTabsNum];
FOR k
IN [0..k)
DO
FOR i IN [0..49) DO colorTabs[k][i] ← Get2Bytes[]; ENDLOOP;
ENDLOOP;
IO.Close[binfile];
cTabCnt ← k
EXITS
FileName => {NULL};
END;
ColorMapCommand: Commander.CommandProc =
BEGIN
mapNum: INT ← -1;
mapFile: Rope.ROPE ← NIL;
s: IO.STREAM ~ IO.RIS[cmd.commandLine];
DO
token: Rope.ROPE ← IO.GetTokenRope[s ! IO.EndOfStream => EXIT].token;
IF Rope.IsEmpty[token] THEN EXIT;
IF Rope.Fetch[token, 0]
IN ['0..'9]
THEN {
mapNum ← Convert.IntFromRope[token ! Convert.Error => EXIT];
}
ELSE mapFile ← CDIO.MakeName[token, "CDColorMaps"];
ENDLOOP;
SetCMColorMap[mapNum, mapFile]
InitFromProfile:
PROC [] =
BEGIN
colorMapNum: INT ← -1;
IF Terminal.LegalColorMode[virtual, [
FALSE, 4, 0]]
THEN
colorMapNum ← UserProfile.Number[key: "ChipNDale.ChipmonkColorMapNum", default: -1];
IF colorMapNum>0 THEN SetCMColorMap[colorMapNum];
END;
ReadColorMapComm: CDSequencer.CommandProc = {SetCMColorMap[]};
ResetColorMapFromProfileComm: CDSequencer.CommandProc = {InitFromProfile[]};
-- Module initialization
virtual: Terminal.Virtual = Terminal.Current[];
IF Terminal.LegalColorMode[virtual, [
FALSE, 4, 0]]
THEN {
InitFromProfile[];
Commander.Register[
key: "CDColorMap",
proc: ColorMapCommand,
doc: "read colormap from chipmonk file"
];
CDSequencer.ImplementCommand[$SetCMColorMap, ReadColorMapComm,, doQueue];
CDSequencer.ImplementCommand[$InitColorMapFromProfile, ResetColorMapFromProfileComm,, doQueue];
CDMenus.CreateEntry[$DisplayMenu, "type cm map", $SetCMColorMap];
CDMenus.CreateEntry[$DisplayMenu, "profile cm map", $InitColorMapFromProfile];
};
END.