diff --git a/cadquery/occ_impl/assembly.py b/cadquery/occ_impl/assembly.py index 4369a9f8c..11d1f8dc8 100644 --- a/cadquery/occ_impl/assembly.py +++ b/cadquery/occ_impl/assembly.py @@ -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: diff --git a/tests/test_assembly.py b/tests/test_assembly.py index 6dc2255d0..2b0b3180d 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -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("?????")