Skip to content

Commit

Permalink
Improving Color str/repr Issue#596
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Mar 21, 2024
1 parent 046b366 commit 6eb00f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/build123d/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

# properties used to store mass calculation result
from OCP.GProp import GProp_GProps
from OCP.Quantity import Quantity_ColorRGBA
from OCP.Quantity import Quantity_Color, Quantity_ColorRGBA
from OCP.TopLoc import TopLoc_Location
from OCP.TopoDS import TopoDS_Face, TopoDS_Shape

Expand Down Expand Up @@ -959,7 +959,13 @@ def __deepcopy__(self, _memo) -> Color:

def __str__(self) -> str:
"""Generate string"""
return f"Color: {str(self.to_tuple())}"
quantity_color_enum = self.wrapped.GetRGB().Name()
quantity_color_str = Quantity_Color.StringName_s(quantity_color_enum)
return f"Color: {str(self.to_tuple())} ~ {quantity_color_str}"

def __repr__(self) -> str:
"""Color repr"""
return f"Color{str(self.to_tuple())}"


class Location:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ def test_copy(self):
self.assertAlmostEqual(c_copy.to_tuple()[2], 0.3, 5)
self.assertAlmostEqual(c_copy.to_tuple()[3], 0.4, 5)

def test_str_repr(self):
c = Color(1, 0, 0)
self.assertEqual(str(c), "Color: (1.0, 0.0, 0.0, 1.0) ~ RED")
self.assertEqual(repr(c), "Color(1.0, 0.0, 0.0, 1.0)")


class TestCompound(DirectApiTestCase):
def test_make_text(self):
Expand Down

0 comments on commit 6eb00f1

Please sign in to comment.