ConstantColorsImpl.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Michael Plass, August 1, 1983 11:43 am
Last Edited by: Stone, August 11, 1984 4:30:05 pm PDT
Last Edited by: Crow, April 20, 1984 12:29:49 pm PST
DIRECTORY
ConstantColors,
ColorModels,
ColorNames,
RealFns USING [AlmostZero],
ImagerBasic USING [ConstantColor, ColorRep],
Rope USING [ROPE],
Real;
ConstantColorsImpl: CEDAR PROGRAM
IMPORTS Real, ColorNames, ColorModels, RealFns
EXPORTS ConstantColors
~ BEGIN OPEN ConstantColors;
ConstantColor: TYPE ~ ImagerBasic.ConstantColor;  -- Color Definitions
black: PUBLIC ConstantColor ← NameToColor["Black"];
white: PUBLIC ConstantColor ← NameToColor["White"];
grey: PUBLIC ConstantColor ← NameToColor["Grey"];
darkGrey: PUBLIC ConstantColor ← NameToColor["Dark Grey"];
lightGrey: PUBLIC ConstantColor ← NameToColor["Light Grey"];
veryDarkGrey: PUBLIC ConstantColor ← NameToColor["Very Dark Grey"];
veryLightGrey: PUBLIC ConstantColor ← NameToColor["Very Light Grey"];
red: PUBLIC ConstantColor ← NameToColor["Red"];
green: PUBLIC ConstantColor ← NameToColor["Green"];
blue: PUBLIC ConstantColor ← NameToColor["Blue"];
magenta: PUBLIC ConstantColor ← NameToColor["Magenta"];
cyan: PUBLIC ConstantColor ← NameToColor["Cyan"];
yellow: PUBLIC ConstantColor ← NameToColor["Yellow"];
orange: PUBLIC ConstantColor ← NameToColor["Orange"];
purple: PUBLIC ConstantColor ← NameToColor["Purple"];
brown: PUBLIC ConstantColor ← NameToColor["Brown"];
IntensityToColor: PUBLIC PROC [intensity: REAL, cal: Calibration ← NIL] RETURNS [ConstantColor] ~ {
IF intensity <= 0 THEN RETURN [black];
IF intensity >= 1 THEN RETURN [white];
RETURN [MakeRep[x: 0.3101, y: 0.3163, Y: intensity]];
};
HSVToColor: PUBLIC PROC[h,s,v: REAL, cal: Calibration ← NIL] RETURNS[ConstantColor] = {
x,y,Y: REAL;
r,g,b: REAL;
[r: r,g: g,b: b] ← ColorModels.HSVToRGB[h: h, s: s, v: v];
[x: x,y: y,Y: Y] ← ColorModels.RGBToCIE[r: r, g: g, b: b, calibration: Cal[cal]];
RETURN[MakeRep[x: x, y: y,Y: Y]];
};
HSLToColor: PUBLIC PROC[h,s,l: REAL, cal: Calibration ← NIL] RETURNS[ConstantColor] = {
x,y,Y: REAL;
r,g,b: REAL;
[r: r,g: g,b: b] ← ColorModels.HSLToRGB[h: h, s: s, l: l];
[x: x,y: y,Y: Y] ← ColorModels.RGBToCIE[r: r, g: g, b: b, calibration: Cal[cal]];
RETURN[MakeRep[x: x, y: y,Y: Y]];
};
RGBToColor: PUBLIC PROC[r,g,b: REAL, cal: Calibration ← NIL] RETURNS[ConstantColor] = {
x,y,Y: REAL;
[x: x,y: y,Y: Y] ← ColorModels.RGBToCIE[r: r, g: g, b: b, calibration: Cal[cal]];
RETURN[MakeRep[x: x, y: y,Y: Y]];
};
CIEToColor: PUBLIC PROC [x,y,Y: REAL] RETURNS [ConstantColor] = {
RETURN[MakeRep[x: x, y: y,Y: Y]]};
NameToColor: PUBLIC PROC[name: Rope.ROPE, cal: Calibration ← NIL] RETURNS[ConstantColor] = {
h,s,l: REAL;
[h: h,s: s,l: l] ← ColorNames.ParseColorName[name];
RETURN[HSLToColor[h: h,s: s,l: l, cal: cal]];
};
ColorToIntensity: PUBLIC PROC[color: ConstantColor, cal: Calibration ← NIL] RETURNS[intensity: REAL] = {
RETURN[CardToReal[color.Y]];
};
ColorToHSV: PUBLIC PROC[color: ConstantColor, cal: Calibration ← NIL] RETURNS[h, s, v: REAL] = {
x,y,Y: REAL;
r,g,b: REAL;
x ← CardToReal[color.x]; y ← CardToReal[color.y]; Y ← CardToReal[color.Y];
[r: r,g: g,b: b] ← ColorModels.CIEToRGB[x: x, y: y,Y: Y, calibration: Cal[cal]];
[r,g,b] ← Defuzz[r,g,b];
[h: h,s: s,v: v] ← ColorModels.RGBToHSV[r: r, g: g, b: b];
RETURN[h: h,s: s,v: v];
};
ColorToHSL: PUBLIC PROC[color: ConstantColor, cal: Calibration ← NIL] RETURNS[h, s, l: REAL] = {
x,y,Y: REAL;
r,g,b: REAL;
x ← CardToReal[color.x]; y ← CardToReal[color.y]; Y ← CardToReal[color.Y];
[r: r,g: g,b: b] ← ColorModels.CIEToRGB[x: x, y: y,Y: Y, calibration: Cal[cal]];
[r,g,b] ← Defuzz[r,g,b];
[h: h,s: s,l: l] ← ColorModels.RGBToHSL[r: r, g: g, b: b];
RETURN[h: h,s: s,l: l];
};
ColorToRGB: PUBLIC PROC[color: ConstantColor, cal: Calibration ← NIL] RETURNS[r, g, b: REAL] = {
x,y,Y: REAL;
x ← CardToReal[color.x]; y ← CardToReal[color.y]; Y ← CardToReal[color.Y];
[r: r,g: g,b: b] ← ColorModels.CIEToRGB[x: x, y: y,Y: Y, calibration: Cal[cal]];
[r,g,b] ← Defuzz[r,g,b];
RETURN[r: r, g: g, b: b];
};
Defuzz: PROC [a,b,c: REAL] RETURNS[REAL, REAL, REAL] = {
IF RealFns.AlmostZero[a,-10] THEN a ← 0;
IF RealFns.AlmostZero[b,-10] THEN b ← 0;
IF RealFns.AlmostZero[c,-10] THEN c ← 0;
IF RealFns.AlmostZero[a-1.0,-10] THEN a ← 1;
IF RealFns.AlmostZero[b-1.0,-10] THEN b ← 1;
IF RealFns.AlmostZero[c-1.0,-10] THEN c ← 1;
RETURN[a,b,c];
};
ColorToCIE: PROC [color: ConstantColor, cal: Calibration ← NIL] RETURNS [x,y,Y: REAL] = {
x ← CardToReal[color.x]; y ← CardToReal[color.y]; Y ← CardToReal[color.Y];
};
ColorToName: PUBLIC PROC [color: ConstantColor, cal: Calibration ← NIL] RETURNS [name: Rope.ROPE] = {
h,s,l: REAL;
[h: h,s: s,l: l] ← ColorToHSL[color,cal];
name ← ColorNames.HSLToRope[h: h,s: s,l: l];
RETURN[name];
};
format utilities
Card: PROC [real: REAL] RETURNS [card: CARDINAL] ~ {
int: INT ← Real.RoundLI[real*LAST[CARDINAL]];
card ← MAX[MIN[int, LAST[CARDINAL]], 0];
};
CardToReal: PROC [card: CARDINAL] RETURNS [real: REAL] ~ {
real ← card;
RETURN[real / LAST[CARDINAL]];
};
MakeRep: PROC[x,y,Y: REAL] RETURNS[ConstantColor] = {
RETURN [NEW[ImagerBasic.ColorRep[constant] ← [constant[x: Card[x], y: Card[y], Y: Card[Y]]]]];
};
Cal: PROC[cal: ColorModels.Calibration] RETURNS [ColorModels.Calibration] = {
RETURN[IF cal=NIL THEN ColorModels.GetPhosphorCalibration[] ELSE cal];
};
END.