Skip to content

Commit

Permalink
Merge pull request #165 from meerk40t/bgr-color
Browse files Browse the repository at this point in the history
Add Support for BGR colors.
  • Loading branch information
tatarize authored Jan 31, 2022
2 parents 9372cbe + a6613f6 commit 476f6ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion svgelements/svgelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
and the Arc can do exact arc calculations if scipy is installed.
"""

SVGELEMENTS_VERSION = "1.6.7"
SVGELEMENTS_VERSION = "1.6.8"

MIN_DEPTH = 5
ERROR = 1e-12
Expand Down Expand Up @@ -1068,6 +1068,8 @@ def __init__(self, *args, **kwargs):
self.blue = kwargs["b"]
if "rgb" in kwargs:
self.rgb = kwargs["rgb"]
if "bgr" in kwargs:
self.bgr = kwargs["bgr"]
if "argb" in kwargs:
self.argb = kwargs["argb"]
if "rgba" in kwargs:
Expand Down Expand Up @@ -1618,6 +1620,20 @@ def rgb(self, rgb):
rgb |= 0xFF
self.value = rgb

@property
def bgr(self):
if self.value is None:
return None
return self.blue << 16 | self.green << 8 | self.red

@bgr.setter
def bgr(self, bgr):
self.value = 0
self.alpha = 0xFF
self.red = bgr & 0xFF
self.green = (bgr >> 8) & 0xFF
self.blue = (bgr >> 16) & 0xFF

@property
def rgba(self):
return self.value
Expand Down
10 changes: 10 additions & 0 deletions test/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_color_red(self):
color.rgb = 0xFF0000
self.assertEqual(reference, color)
self.assertEqual(reference, Color(rgb=0xFF0000))
self.assertEqual(reference, Color(bgr=0x0000FF))
self.assertEqual(reference, Color(argb=0xFFFF0000))
self.assertEqual(reference, Color(rgba=0xFF0000FF))
self.assertEqual(reference, Color(0xFF0000, 1.0))
Expand All @@ -50,6 +51,7 @@ def test_color_green(self):
color.rgb = 0x00FF00
self.assertEqual(reference, color)
self.assertEqual(reference, Color(rgb=0x00FF00))
self.assertEqual(reference, Color(bgr=0x00FF00))
self.assertEqual(reference, Color(argb=0xFF00FF00))
self.assertEqual(reference, Color(rgba=0x00FF00FF))
self.assertEqual(reference, Color(0x00FF00, 1.0))
Expand All @@ -74,10 +76,18 @@ def test_color_blue(self):
color.rgb = 0x0000FF
self.assertEqual(reference, color)
self.assertEqual(reference, Color(rgb=0x0000FF))
self.assertEqual(reference, Color(bgr=0xFF0000))
self.assertEqual(reference, Color(argb=0xFF0000FF))
self.assertEqual(reference, Color(rgba=0x0000FFFF))
self.assertEqual(reference, Color(0x0000FF, 1.0))

def test_color_bgr(self):
reference = Color("#26A")
self.assertEqual(reference, Color(bgr=0xAA6622))
reference.bgr = 0x2468AC
self.assertEqual(reference, Color(rgb=0xAC6824))
self.assertEqual(reference.alpha, 0xFF)

def test_color_red_half(self):
half_ref = Color("rgba(100%, 0%, 0%, 0.5)")
self.assertNotEqual(half_ref, 'red')
Expand Down

0 comments on commit 476f6ab

Please sign in to comment.