Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cadquery/occ_impl/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ def __init__(self, *args, **kwargs):
self.wrapped = Quantity_ColorRGBA()
elif len(args) == 1:
self.wrapped = Quantity_ColorRGBA()
exists = Quantity_ColorRGBA.ColorFromName_s(args[0], self.wrapped)
exists = Quantity_ColorRGBA.ColorFromName_s(
args[0], self.wrapped
) or Quantity_ColorRGBA.ColorFromHex_s(args[0], self.wrapped)
if not exists:
raise ValueError(f"Unknown color name: {args[0]}")
elif len(args) == 3:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,36 @@ def test_color():
assert c4.wrapped.GetRGB().Green() == pytest.approx(0.2)
assert c4.wrapped.Alpha() == 0.5

# test for hex string for short sRGB color
c5 = cq.Color("#FF0")
assert c5.wrapped.GetRGB().Red() == 1
assert c5.wrapped.GetRGB().Green() == 1
assert c5.wrapped.GetRGB().Blue() == 0
with pytest.raises(AttributeError):
c5.wrapped.GetRGB().Alpha()

# test for hex string for short sRGBA color
c6 = cq.Color("#FF0F")
assert c6.wrapped.GetRGB().Red() == 1
assert c6.wrapped.GetRGB().Green() == 1
assert c6.wrapped.GetRGB().Blue() == 0
assert c6.wrapped.Alpha() == 1.0

# test for hex string for RGB color
c7 = cq.Color("#FFFF00")
assert c7.wrapped.GetRGB().Red() == 1
assert c7.wrapped.GetRGB().Green() == 1
assert c7.wrapped.GetRGB().Blue() == 0
with pytest.raises(AttributeError):
c7.wrapped.GetRGB().Alpha()

# test for hex string for RGBA color
c8 = cq.Color("#FFFF00FF")
assert c8.wrapped.GetRGB().Red() == 1
assert c8.wrapped.GetRGB().Green() == 1
assert c8.wrapped.GetRGB().Blue() == 0
assert c8.wrapped.Alpha() == 1.0

with pytest.raises(ValueError):
cq.Color("?????")

Expand Down