Skip to content

Commit

Permalink
Adding hex color codes to Color
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Nov 19, 2023
1 parent c3ede07 commit 49c0a60
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/build123d/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,14 +876,26 @@ def __init__(self, red: float, green: float, blue: float, alpha: float = 1.0):
alpha (float, optional): 0.0 <= alpha <= 1.0. Defaults to 0.0.
"""

@overload
def __init__(self, color_code: int):
"""Color from a hexidecimal color code with an optional alpha value
Args:
color_code (hexidecial int): 0xRRGGBB or 0xRRGGBBAA
"""

def __init__(self, *args, **kwargs):
red, green, blue, alpha, name = 1.0, 1.0, 1.0, 1.0, None
red, green, blue, alpha, name, hex = 1.0, 1.0, 1.0, 1.0, None, None
if len(args) >= 1:
if isinstance(args[0], str):
name = args[0]
if isinstance(args[0], float):
red = args[0]
else:
hex = args[0]
red = args[0]
if len(args) >= 2:
hex = None
if name:
alpha = args[1]
else:
Expand All @@ -892,11 +904,27 @@ def __init__(self, *args, **kwargs):
blue = args[2]
if len(args) == 4:
alpha = args[3]

hex = kwargs.get("color_code", hex)
red = kwargs.get("red", red)
green = kwargs.get("green", green)
blue = kwargs.get("blue", blue)
alpha = kwargs.get("alpha", alpha)

if hex is not None and isinstance(hex, int):
if hex > 256**3:
red, remainder = divmod(hex, 256**3)
green, remainder = divmod(remainder, 256**2)
blue, alpha = divmod(remainder, 256)
else:
red, remainder = divmod(hex, 256**2)
green, blue = divmod(remainder, 256)
alpha = 255
red = red / 255
green = green / 255
blue = blue / 255
alpha = alpha / 255

if name:
self.wrapped = Quantity_ColorRGBA()
exists = Quantity_ColorRGBA.ColorFromName_s(args[0], self.wrapped)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,27 @@ def test_to_tuple(self):
self.assertEqual(c.to_tuple()[2], 1.0)
self.assertEqual(c.to_tuple()[3], 0.5)

def test_hex(self):
c = Color(0x996692)
self.assertAlmostEqual(c.to_tuple()[0], 153 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[1], 102 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[2], 146 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[3], 1.0, 5)

c = Color(color_code=0x996692CC)
self.assertAlmostEqual(c.to_tuple()[0], 153 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[1], 102 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[2], 146 / 255, 5)
self.assertAlmostEqual(c.to_tuple()[3], 204 / 255, 5)

def test_copy(self):
c = Color(0.1, 0.2, 0.3, alpha=0.4)
c_copy = copy.copy(c)
self.assertAlmostEqual(c_copy.to_tuple()[0], 0.1, 5)
self.assertAlmostEqual(c_copy.to_tuple()[1], 0.2, 5)
self.assertAlmostEqual(c_copy.to_tuple()[2], 0.3, 5)
self.assertAlmostEqual(c_copy.to_tuple()[3], 0.4, 5)


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

0 comments on commit 49c0a60

Please sign in to comment.