1. currentgray

Returns current color as gray value.

1.1. Syntax

– currentgray → num

1.2. Stack Effects

Table 1. Before
Level Object

(empty)

Table 2. After
Level Object

0

num (0.0 = black to 1.0 = white)

1.3. Description

currentgray returns the gray value of the current color parameter in the graphics state.

  • If the current color space is DeviceGray, currentgray returns the color value most recently specified to setgray or [setcolor].

  • If the current color space is DeviceRGB or DeviceCMYK, currentgray converts the current color to a gray value using standard conversion formulas.

  • For any other color space, currentgray returns 0.0 (black).

The returned value is always in the range 0.0 (black) to 1.0 (white).

1.4. PostScript Level

Level 1 and later

1.5. Examples

Querying current gray
0.5 setgray
currentgray
% Stack: 0.5
Converting from RGB
1 0 0 setrgbcolor  % Red
currentgray
% Stack: 0.3 (approximate gray equivalent)
Saving and restoring gray
currentgray        % Save current
/savedGray exch def

0 setgray          % Change to black
drawShape

savedGray setgray  % Restore
Conditional drawing based on color
currentgray 0.5 lt {
  % Current color is dark
  drawWithWhiteOutline
} {
  % Current color is light
  drawWithBlackOutline
} ifelse

1.6. Common Use Cases

1.6.1. Color State Preservation

/DrawWithTemporaryGray {
  % Save current gray
  currentgray

  % Draw with specific gray
  0.7 setgray
  drawElement

  % Restore
  setgray
} def

1.6.2. Adaptive Rendering

/DrawAdaptive {
  currentgray
  dup 0.5 lt {
    % Dark color - use light decorations
    pop
    1 setgray
    drawDecorations
  } {
    % Light color - use dark decorations
    pop
    0 setgray
    drawDecorations
  } ifelse
} def

1.6.3. Color Space Detection

/IsGrayColorSpace {
  currentgray
  0.5 setgray
  currentgray 0.5 eq
  % If true, was already in DeviceGray
} def

1.6.4. Monochrome Validation

/EnsureGrayscale {
  currentgray setgray  % Force to gray
} def

1.7. Common Pitfalls

Color Space Conversion - Conversion from RGB/CMYK to gray may lose color information.
1 0 0 setrgbcolor  % Red
currentgray        % ~0.3
setgray            % Now gray, not red
Returns 0.0 for Unknown Spaces - Pattern and other color spaces return black.
/Pattern setcolorspace
myPattern setcolor
currentgray        % Returns 0.0
Approximate Conversion - RGB/CMYK to gray conversion is approximate.
0.5 0.5 0.5 setrgbcolor
currentgray
% May not be exactly 0.5 due to conversion
Use for Monochrome Output - Good for ensuring grayscale rendering.

1.8. Error Conditions

Error Condition

[stackoverflow]

No room on operand stack

1.9. Implementation Notes

  • Very fast query operation

  • No modification to graphics state

  • Always returns value in range 0.0 to 1.0

  • Conversion formulas are device-independent

  • Level 1 operator (widely supported)

1.10. Color Conversion Formulas

When converting from other color spaces:

RGB to Gray
gray = 0.3 × red + 0.59 × green + 0.11 × blue
CMYK to Gray (approximate)
gray = 1 - min(1, 0.3×cyan + 0.59×magenta + 0.11×yellow + black)
Other Spaces
gray = 0.0

1.11. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.