From 49c0a60eecce2debd6dff0954a332d14afcf2290 Mon Sep 17 00:00:00 2001 From: gumyr Date: Sun, 19 Nov 2023 09:06:29 -0500 Subject: [PATCH] Adding hex color codes to Color --- src/build123d/geometry.py | 30 +++++++++++++++++++++++++++++- tests/test_direct_api.py | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/build123d/geometry.py b/src/build123d/geometry.py index 8a69829f..b99b8151 100644 --- a/src/build123d/geometry.py +++ b/src/build123d/geometry.py @@ -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: @@ -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) diff --git a/tests/test_direct_api.py b/tests/test_direct_api.py index 4c10b286..b54889c6 100644 --- a/tests/test_direct_api.py +++ b/tests/test_direct_api.py @@ -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):