Colour: colorspace conversion & colour operators¶
Pure-Python port of BOSL2’s color.scad – the HSL/HSV -> RGB conversions, the
rainbow() helper, and the colour operators added onto
Bosl2Solid via the Colorable mixin. Each operator
resolves to the native PythonSCAD calls: color(), highlight() (the # modifier) and
background() (the % / ghost modifier):
cuboid([20, 20, 10]).color("red")
cuboid([20, 20, 10]).hsv(210, 0.8, 0.9) # colour from an HSV hue
cuboid([20, 20, 10]).color([0.2, 0.5, 0.9, 0.4]) # RGBA
part.highlight() # # debug modifier
part.ghost() # % transparent, non-interacting
hsl() / hsv() are pinned to the real BOSL2 output in tests/test_bosl2_reorient.py.
Because the toolkit builds native geometry rather than a BOSL2 $color attachment tree,
recolor() and color_this() both apply the
colour directly – an object’s already-coloured children keep their colour (OpenSCAD color()
semantics), and there is no $color scheme to revert to, so a "default" colour is a no-op.
Coverage of BOSL2 color.scad¶
BOSL2 function |
Status |
Notes |
|---|---|---|
|
ported |
|
|
ported |
object methods; both apply the colour natively (no |
|
ported |
|
|
ported |
|
|
ported |
|
|
not ported |
a debug module that intersects every pair of children; niche – build it explicitly with the CSG operators if needed. |
Examples¶
A stack of blocks, each a different hue from HSV:
blocks = [s3.cuboid([20, 20, 4]).up(i * 5).hsv(i * 40, 0.8, 0.95) for i in range(5)]
reduce(lambda a, b: a | b, blocks).show()
Rainbow-colouring a list of parts to tell them apart:
parts = [s3.cyl(height=20, radius=4).right(i * 12) for i in range(6)]
reduce(lambda a, b: a | b, rainbow(parts)).show()
API reference¶
- bosl2.color.hsl(height, s=1.0, length=0.5, a=None)[source]¶
Convert HSL to an
[R, G, B]colour (or[R, G, B, A]if a is given) – BOSL2 hsl().- Parameters:
height (float) – hue in degrees (0=red, 60=yellow, 120=green, 180=cyan, 240=blue, 300=magenta)
s (float) – saturation 0..1 (0 = grey, 1 = vivid). Default 1
length (float) – lightness 0..1 (0 = black, 0.5 = bright, 1 = white). Default 0.5
a (float | None) – optional alpha 0..1; when given the result is
[R, G, B, A]
- Returns:
[R, G, B](each 0..1), or[R, G, B, A]when a is given.- Return type:
list[float]
- bosl2.color.hsv(height, s=1.0, v=1.0, a=None)[source]¶
Convert HSV to an
[R, G, B]colour (or[R, G, B, A]if a is given) – BOSL2 hsv().- Parameters:
height (float) – hue in degrees (0=red, 60=yellow, 120=green, 180=cyan, 240=blue, 300=magenta)
s (float) – saturation 0..1 (0 = grey, 1 = vivid). Default 1
v (float) – value 0..1 (0 = black, 1 = bright). Default 1
a (float | None) – optional alpha 0..1; when given the result is
[R, G, B, A]
- Returns:
[R, G, B](each 0..1), or[R, G, B, A]when a is given.- Return type:
list[float]
- bosl2.color.rainbow(items, stride=1, maxhues=None, shuffle=False, seed=None)[source]¶
Colour each object in items a different hue, returning the coloured list (BOSL2 rainbow()).
Each item must support
.color([r, g, b])(aBosl2Solidor a native solid). Useful for telling apart the parts of a multi-piece model or debugging a list of paths.- Parameters:
items (Sequence) – the objects to colour
stride (int) – consecutive colours stride this many steps around the wheel
maxhues (int | None) – cap the number of distinct hues (default:
len(items))shuffle (bool) – shuffle the hue order
seed – seed for the shuffle
- Return type:
list
- bosl2.color.rainbow_colors(sides, stride=1, maxhues=None, shuffle=False, seed=None)[source]¶
The list of
sides[R, G, B]colours stepped around the ROYGBIV wheel (BOSL2 rainbow()).- Parameters:
sides (int) – how many colours to generate
stride (int) – consecutive colours stride this many steps around the wheel
maxhues (int | None) – cap the number of distinct hues (default: sides)
shuffle (bool) – shuffle the hue order
seed – seed for the shuffle
- Return type:
list[list[float]]
- class bosl2.color.Colorable[source]¶
Bases:
ABCMixin adding the color.scad colour operators as methods.
Inherited by
Bosl2Solid. Every operator resolves to the host’s native colour primitives, which the host provides as_color_native(PythonSCADcolor()),_highlight_native(the#modifier) and_ghost_native(the%modifier). Because the toolkit builds native geometry rather than a BOSL2$colorattachment tree,recolor()andcolor_this()both apply the colour directly (an object’s already-coloured children keep their colour, matching OpenSCAD’scolor()semantics).- color(c=None, alpha=None)[source]¶
Colour this object. c is a name (
"red"),[R, G, B], or[R, G, B, A].- Parameters:
alpha (float | None)
- recolor(c='default', alpha=None)[source]¶
Set the colour of this object and its uncoloured descendants (BOSL2 recolor()).
"default"/Noneleaves the colour unchanged (there is no$colorscheme to revert to in the native backend).- Parameters:
alpha (float | None)
- color_this(c='default', alpha=None)[source]¶
Colour just this object (BOSL2 color_this()); equivalent to
color()in the native backend, where there is no$colorattachment tree to preserve separately.- Parameters:
alpha (float | None)
- hsl(height, s=1.0, length=0.5, a=None)[source]¶
Colour this object from an HSL hue/saturation/lightness (BOSL2 hsl()).
- Parameters:
height (float)
s (float)
length (float)
a (float | None)
- hsv(height, s=1.0, v=1.0, a=None)[source]¶
Colour this object from an HSV hue/saturation/value (BOSL2 hsv()).
- Parameters:
height (float)
s (float)
v (float)
a (float | None)