1. setgray
Sets the current gray level for painting operations.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| 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.5. Examples
0 setgray % Black
1 setgray % White
0.5 setgray % 50% gray
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
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.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 |
|---|---|
[ |
No operand on stack |
[ |
Operand not a number |
[ |
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
-
setrgbcolor- Set RGB color -
setcmykcolor- Set CMYK color (Level 2) -
sethsbcolor- Set HSB color -
currentgray- Get current gray value -
Color operations (to be documented) - Advanced color spaces