1. setgray

Sets the current gray level for painting operations.

1.1. Syntax

num setgray → –

1.2. Stack Effects

Table 1. Before
Level Object

0

num (0.0 = black to 1.0 = white)

Table 2. After
Level Object

(empty)

Color space set to DeviceGray, color set to num

1.3. Description

setgray sets the color space to DeviceGray and sets the current color to a gray shade corresponding to num.

  • 0 = black

  • 1 = white

  • 0.5 = 50% gray

  • Intermediate values = intermediate grays

This establishes the color for subsequent painting operations (lines, fills, text).

Values outside 0-1 are clamped to nearest legal value (no error).

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic gray levels
0 setgray     % Black
1 setgray     % White
0.5 setgray   % 50% gray
Drawing with different grays
0 setgray
0 0 moveto 100 0 lineto stroke  % Black line

0.5 setgray
0 10 moveto 100 10 lineto stroke  % Gray line

1 setgray
0 20 moveto 100 20 lineto stroke  % White line
Grayscale fills
0.2 setgray
0 0 50 50 rectfill  % Dark gray rectangle

0.8 setgray
60 0 50 50 rectfill  % Light gray rectangle

1.6. Common Use Cases

1.6.1. Black and White Graphics

/drawBW {
  gsave
    0 setgray  % Black
    drawOutline stroke
    1 setgray  % White
    drawInterior fill
  grestore
} def

1.6.2. Grayscale Gradients

0 0.01 1 {
  dup setgray
  currentpoint translate
  drawBand
} for

1.6.3. Text Rendering

0 setgray
/Helvetica findfont 12 scalefont setfont
100 100 moveto (Black text) show

0.5 setgray
100 80 moveto (Gray text) show

1.7. Common Pitfalls

Changes Color Space - setgray changes color space to DeviceGray.
1 0 0 setrgbcolor  % Red (DeviceRGB)
0.5 setgray        % Gray (DeviceGray - RGB lost!)
Values Clamped - Out-of-range values are adjusted, not rejected.
1.5 setgray   % Becomes 1.0 (white)
-0.5 setgray  % Becomes 0.0 (black)
Use for Monochrome - Most efficient color operation for monochrome output.

1.8. Error Conditions

Error Condition

[stackunderflow]

No operand on stack

[typecheck]

Operand not a number

[undefined]

Operator disabled in certain contexts

1.9. Implementation Notes

  • Very fast operation

  • Most efficient color setting (single value)

  • Widely supported on all devices

  • Default color space for many operations

1.10. See Also


Back to top

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