1. sethsbcolor
Sets the current color using HSB (Hue-Saturation-Brightness) values.
1.2. Stack Effects
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
Color space set to DeviceRGB with HSB-derived color |
1.3. Description
sethsbcolor sets the color space to DeviceRGB and sets the current color using hue, saturation, and brightness components.
HSB components:
-
Hue (0.0 to 1.0): Color type on the color wheel
-
0.0 = red, 0.167 = yellow, 0.333 = green, 0.5 = cyan, 0.667 = blue, 0.833 = magenta, 1.0 = red (wraps)
-
-
Saturation (0.0 to 1.0): Color purity
-
0.0 = no color (gray), 1.0 = pure, vivid color
-
-
Brightness (0.0 to 1.0): Color intensity
-
0.0 = black, 1.0 = maximum brightness
-
HSB is not a color space in its own right, merely a means for entering RGB color values in a different coordinate system. The color value entered by sethsbcolor is immediately converted into the RGB model and used with the DeviceRGB color space.
|
sethsbcolor does not give a rangecheck error for values outside the range 0 to 1; it substitutes the nearest legal value.
1.5. Examples
0.0 1 1 sethsbcolor % Red
0.333 1 1 sethsbcolor % Green
0.667 1 1 sethsbcolor % Blue
0.0 0.3 1.0 sethsbcolor % Light pink
0.333 0.3 1.0 sethsbcolor % Light green
0.667 0.3 1.0 sethsbcolor % Light blue
0.0 1 0.3 sethsbcolor % Dark red
0.333 1 0.3 sethsbcolor % Dark green
0.667 1 0.3 sethsbcolor % Dark blue
0 0 0.0 sethsbcolor % Black
0 0 0.5 sethsbcolor % Mid gray
0 0 1.0 sethsbcolor % White
1.6. Common Use Cases
1.6.1. Color Wheel Generation
/DrawColorWheel {
0 10 360 {
% Convert degrees to hue
360 div
1.0 % Full saturation
1.0 % Full brightness
sethsbcolor
% Draw segment
drawWedge
} for
} def
1.6.2. Tint Generation
/GenerateTints {
% hue on stack
5 {
% Create 5 tints
dup % hue
0.2 mul % saturation decreases
1.0 % full brightness
sethsbcolor
drawSwatch
} repeat
pop
} def
1.6.3. Shade Generation
/GenerateShades {
% hue on stack
5 {
dup % hue
1.0 % full saturation
0.2 mul % brightness decreases
sethsbcolor
drawSwatch
} repeat
pop
} def
1.6.4. Color Harmony
/ComplementaryPair {
% hue
dup 1 1 sethsbcolor
drawShape1
0.5 add % Opposite on color wheel
dup 1.0 gt { 1.0 sub } if
1 1 sethsbcolor
drawShape2
} def
/TriadicScheme {
% hue
dup 1 1 sethsbcolor
drawShape1
dup 0.333 add 1 1 sethsbcolor
drawShape2
0.667 add 1 1 sethsbcolor
drawShape3
} def
1.7. Common Pitfalls
| Immediately Converted to RGB - Color is stored as RGB, not HSB. |
0.5 1 1 sethsbcolor % Cyan via HSB
currentrgbcolor % Returns 0 1 1 (RGB)
currenthsbcolor % Returns 0.5 1 1 (converted back)
| Values Clamped - Out-of-range values adjusted, not rejected. |
1.5 0.5 0.8 sethsbcolor % Hue becomes 0.5
0.5 1.5 0.8 sethsbcolor % Saturation becomes 1.0
0.5 0.5 1.5 sethsbcolor % Brightness becomes 1.0
| Hue Wraps - Hue values wrap at 0.0 and 1.0. |
1.2 1 1 sethsbcolor % Hue becomes 0.2
-0.1 1 1 sethsbcolor % Hue becomes 0.9
| Color Space Changes - Changes to DeviceRGB color space. |
0.5 setgray % DeviceGray
0.5 1 1 sethsbcolor % DeviceRGB (gray lost!)
| Intuitive Color Selection - HSB is more intuitive than RGB for selecting colors. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Fewer than 3 operands on stack |
[ |
Any operand not a number |
[ |
Disabled in certain contexts |
1.9. Implementation Notes
-
Immediately converts to RGB internally
-
Sets color space to DeviceRGB
-
Values clamped to 0.0-1.0 range
-
No actual HSB color space exists
-
Fast operation (simple conversion)
-
Widely supported (Level 1)
1.10. HSB to RGB Conversion
Conceptual algorithm:
% If saturation = 0 (achromatic)
brightness dup dup setrgbcolor
% Otherwise
% 1. Determine hue sector (0-5)
% 2. Calculate chroma and intermediate values
% 3. Map to RGB based on sector
% 4. Apply brightness scaling
1.11. HSB Color Model
Yellow
0.167
|
Green-+-Red
0.333 | 0.0/1.0
|
Cyan
0.5
|
Blue-+-Magenta
0.667| 0.833
0.0 = Gray (no color) 0.5 = Pastel (moderate color) 1.0 = Vivid (pure color)
0.0 = Black 0.5 = Medium 1.0 = Full brightness
1.12. Practical Color Selection
% Selecting colors intuitively:
% Vivid red
0.0 1.0 1.0 sethsbcolor
% Pastel blue
0.667 0.3 1.0 sethsbcolor
% Dark green
0.333 1.0 0.3 sethsbcolor
% Medium gray (saturation = 0)
0.0 0.0 0.5 sethsbcolor
1.13. See Also
-
setrgbcolor- Set RGB color -
setgray- Set grayscale -
setcmykcolor- Set CMYK color (Level 2) -
currenthsbcolor- Get HSB color