ConstantColorsImpl.mesa
Michael Plass, August 1, 1983 11:43 am
Last Edited by: Stone, February 10, 1984 6:17:37 pm PST
DIRECTORY
ConstantColors,
ColorModels,
ColorNames,
ImagerBasic USING [ConstantColor, ColorRep],
Rope USING [ROPE],
Real;
ConstantColorsImpl: CEDAR PROGRAM
IMPORTS Real, ColorNames, ColorModels
EXPORTS ConstantColors
~ BEGIN OPEN ConstantColors;
ConstantColor: TYPE ~ ImagerBasic.ConstantColor;
black: PUBLIC ConstantColor ← MakeRep[x: 0.3101, y: 0.3163, Y: 0];
white: PUBLIC ConstantColor ← MakeRep[x: 0.3101, y: 0.3163, Y: 1];
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;
names: ColorNames.Names ← ColorNames.ParseColorName[name];
[h: h,s: s,l: l] ← ColorNames.NamesToHSL[names];
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]];
[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]];
[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]];
RETURN[r: r, g: g, b: b];
};
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.NamesToRope[ColorNames.HSLToNames[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] ~ {
RETURN[card*LAST[CARDINAL]];
};
MakeRep: PROC[x,y,Y: REAL] RETURNS[ConstantColor] = {
RETURN [NEW[ImagerBasic.ColorRep[constant] ← [constant[x: Card[0.3101], y: Card[0.3163], Y: Card[Y]]]]];
};
Cal: PROC[cal: ColorModels.Calibration] RETURNS [ColorModels.Calibration] = {
RETURN[IF cal=NIL THEN ColorModels.GetDefaultCalibration[] ELSE cal];
};
END.