Skip to content

Commit 2802ca7

Browse files
author
Evan Jones
committed
Fix for #158, bringing SP up to date on OpenSCAD's built-in color and alpha handling. Bumping version to 1.0.4.
1 parent 22aee15 commit 2802ca7

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

README.rst

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -387,38 +387,18 @@ See
387387
`solid/examples/path_extrude_example.py <https://github.com/SolidCode/SolidPython/blob/master/solid/examples/path_extrude_example.py>`__
388388
for use.
389389

390-
Basic color library
390+
Color & Transparency Settings
391391
-------------------
392392

393393
You can change an object's color by using the OpenSCAD
394-
``color([rgba_array])`` function:
394+
``color([rgba_array | rgba hex | SVG color name], alpha)`` function:
395395

396396
.. code:: python
397397
398398
transparent_blue = color([0,0,1, 0.5])(cube(10)) # Specify with RGB[A]
399-
red_obj = color(Red)(cube(10)) # Or use predefined colors
400-
401-
These colors are pre-defined in solid.utils:
402-
403-
+------------+---------+--------------+
404-
| Red | Green | Blue |
405-
+------------+---------+--------------+
406-
| Cyan | Magenta | Yellow |
407-
+------------+---------+--------------+
408-
| Black | White | Transparent |
409-
+------------+---------+--------------+
410-
| Oak | Pine | Birch |
411-
+------------+---------+--------------+
412-
| Iron | Steel | Stainless |
413-
+------------+---------+--------------+
414-
| Aluminum | Brass | BlackPaint |
415-
+------------+---------+--------------+
416-
| FiberBoard | | |
417-
+------------+---------+--------------+
418-
419-
They're a conversion of the materials in the `MCAD OpenSCAD
420-
library <https://github.com/openscad/MCAD>`__, as seen [here]
421-
(https://github.com/openscad/MCAD/blob/master/materials.scad).
399+
purple_obj = color('MediumPurple')(cube(10)) # Or use predefined SVG colors
400+
401+
OpenSCAD natively accepts the W3C's `SVG Color Names <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color>`__
422402

423403
Bill Of Materials
424404
-----------------

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "solidpython"
3-
version = "1.0.3"
3+
version = "1.0.4"
44
description = "Python interface to the OpenSCAD declarative geometry language"
55
authors = ["Evan Jones <evan_t_jones@mac.com>"]
66
homepage = "https://github.com/SolidCode/SolidPython"

solid/objects.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,14 @@ class color(OpenSCADObject):
376376
not specified.
377377
378378
:param c: RGB color + alpha value.
379-
:type c: sequence of 3 or 4 numbers between 0 and 1
379+
:type c: sequence of 3 or 4 numbers between 0 and 1, OR 3-, 4-, 6-, or 8-digit RGB/A hex code, OR string color name as described at https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color
380+
381+
:param alpha: Alpha value from 0 to 1
382+
:type alpha: float
380383
"""
381384

382-
def __init__(self, c: Vec34) -> None:
383-
super().__init__('color', {'c': c})
385+
def __init__(self, c: Union[Vec34, str], alpha: float = 1.0) -> None:
386+
super().__init__('color', {'c': c, 'alpha': alpha})
384387

385388

386389
class minkowski(OpenSCADObject):

solid/test/test_solidpython.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
{'name': 'mirror', 'class': 'mirror' , 'kwargs': {}, 'expected': '\n\nmirror(v = [0, 0, 1]);', 'args': {'v': [0, 0, 1]}, },
3737
{'name': 'resize', 'class': 'resize' , 'kwargs': {'newsize': [5, 5, 5], 'auto': [True, True, False]}, 'expected': '\n\nresize(auto = [true, true, false], newsize = [5, 5, 5]);', 'args': {}, },
3838
{'name': 'multmatrix', 'class': 'multmatrix' , 'kwargs': {}, 'expected': '\n\nmultmatrix(m = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]);', 'args': {'m': [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]}, },
39-
{'name': 'color', 'class': 'color' , 'kwargs': {}, 'expected': '\n\ncolor(c = [1, 0, 0]);', 'args': {'c': [1, 0, 0]}, },
4039
{'name': 'minkowski', 'class': 'minkowski' , 'kwargs': {}, 'expected': '\n\nminkowski();', 'args': {}, },
4140
{'name': 'offset', 'class': 'offset' , 'kwargs': {'r': 1}, 'expected': '\n\noffset(r = 1);', 'args': {}, },
4241
{'name': 'offset_segments', 'class': 'offset' , 'kwargs': {'r': 1, 'segments': 12}, 'expected': '\n\noffset($fn = 12, r = 1);', 'args': {}, },
@@ -114,7 +113,7 @@ def test_infix_intersection(self):
114113

115114
def test_parse_scad_callables(self):
116115
test_str = """
117-
module hex (width=10, height=10,
116+
module hex (width=10, height=10,
118117
flats= true, center=false){}
119118
function righty (angle=90) = 1;
120119
function lefty(avar) = 2;
@@ -215,11 +214,11 @@ def test_import_scad(self):
215214
self.assertTrue(hasattr(examples.scad_to_include, 'steps'))
216215

217216
# TODO: we should test that:
218-
# A) scad files in the designated OpenSCAD library directories
219-
# (path-dependent, see: solid.objects._openscad_library_paths())
217+
# A) scad files in the designated OpenSCAD library directories
218+
# (path-dependent, see: solid.objects._openscad_library_paths())
220219
# are imported correctly. Not sure how to do this without writing
221220
# temp files to those directories. Seems like overkill for the moment
222-
221+
223222
def test_imported_scad_arguments(self):
224223
include_file = self.expand_scad_path("examples/scad_to_include.scad")
225224
mod = import_scad(include_file)
@@ -229,7 +228,7 @@ def test_imported_scad_arguments(self):
229228
abs_path = points._get_include_path(include_file)
230229
expected = f'use <{abs_path}>\n\n\npolygon(points = scad_points());'
231230
self.assertEqual(expected, actual)
232-
231+
233232
def test_use_reserved_words(self):
234233
scad_str = '''module reserved_word_arg(or=3){\n\tcube(or);\n}\nmodule or(arg=3){\n\tcube(arg);\n}\n'''
235234

@@ -297,6 +296,25 @@ def test_root(self):
297296
actual = scad_render(root(a))
298297
self.assertEqual(expected, actual)
299298

299+
def test_color(self):
300+
all_args = [
301+
{'c': [1, 0, 0]},
302+
{'c': [1, 0, 0], 'alpha': 0.5},
303+
{'c': "#66F"},
304+
{'c': "Teal", 'alpha': 0.5},
305+
]
306+
307+
expecteds = [
308+
'\n\ncolor(alpha = 1.0000000000, c = [1, 0, 0]);',
309+
'\n\ncolor(alpha = 0.5000000000, c = [1, 0, 0]);',
310+
'\n\ncolor(alpha = 1.0000000000, c = "#66F");',
311+
'\n\ncolor(alpha = 0.5000000000, c = "Teal");',
312+
]
313+
for args, expected in zip(all_args, expecteds):
314+
col = color(**args)
315+
actual = scad_render(col)
316+
self.assertEqual(expected, actual)
317+
300318
def test_explicit_hole(self):
301319
a = cube(10, center=True) + hole()(cylinder(2, 20, center=True))
302320
expected = '\n\ndifference(){\n\tunion() {\n\t\tcube(center = true, size = 10);\n\t}\n\t/* Holes Below*/\n\tunion(){\n\t\tcylinder(center = true, h = 20, r = 2);\n\t} /* End Holes */ \n}'

solid/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
# ==========
5353
# = Colors =
5454
# ==========
55+
# Deprecated, but kept for backwards compatibility . Note that OpenSCAD natively
56+
# accepts SVG Color names, as seen here: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color
5557
# From Hans Häggström's materials.scad in MCAD: https://github.com/openscad/MCAD
5658
Red = (1, 0, 0)
5759
Green = (0, 1, 0)

0 commit comments

Comments
 (0)