1. currentgray
Returns current color as gray value.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
– |
| Level | Object |
|---|---|
0 |
|
1.3. Description
currentgray returns the gray value of the current color parameter in the graphics state.
-
If the current color space is DeviceGray,
currentgrayreturns the color value most recently specified tosetgrayor [setcolor]. -
If the current color space is DeviceRGB or DeviceCMYK,
currentgrayconverts the current color to a gray value using standard conversion formulas. -
For any other color space,
currentgrayreturns 0.0 (black).
The returned value is always in the range 0.0 (black) to 1.0 (white).
1.5. Examples
0.5 setgray
currentgray
% Stack: 0.5
1 0 0 setrgbcolor % Red
currentgray
% Stack: 0.3 (approximate gray equivalent)
currentgray % Save current
/savedGray exch def
0 setgray % Change to black
drawShape
savedGray setgray % Restore
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.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.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:
gray = 0.3 × red + 0.59 × green + 0.11 × blue
gray = 1 - min(1, 0.3×cyan + 0.59×magenta + 0.11×yellow + black)
gray = 0.0
1.11. See Also
-
setgray- Set gray color -
currentrgbcolor- Get RGB color -
currentcmykcolor- Get CMYK color (Level 2) -
currenthsbcolor- Get HSB color