Skip to content

Commit

Permalink
Merge pull request #92 from meerk40t/1.4.6
Browse files Browse the repository at this point in the history
1.4.6 - Color(None) avoids crashing.
  • Loading branch information
tatarize authored Jan 20, 2021
2 parents 25433f8 + f73686f commit d561543
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
2 changes: 0 additions & 2 deletions __init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = svgelements
version = 1.4.5
version = 1.4.6
description = Svg Elements Parsing
long_description_content_type=text/markdown
long_description = file: README.md
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from setuptools import setup

setup()
53 changes: 43 additions & 10 deletions svgelements/svgelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
and the Arc can do exact arc calculations if scipy is installed.
"""

SVGELEMENTS_VERSION = "1.4.5"
SVGELEMENTS_VERSION = "1.4.6"

MIN_DEPTH = 5
ERROR = 1e-12
Expand Down Expand Up @@ -1426,20 +1426,24 @@ def parse_color_hsl(values):

@property
def opacity(self):
return self.alpha / 255.0
return self.alpha / 255.0 if self.value is not None else None

@opacity.setter
def opacity(self, opacity):
if self.value is None:
raise ValueError
a = int(round(opacity * 255.0))
a = Color.crimp(a)
self.alpha = a

@property
def alpha(self):
return (self.value >> 24) & 0xFF
return (self.value >> 24) & 0xFF if self.value is not None else None

@alpha.setter
def alpha(self, a):
if self.value is None:
raise ValueError
a = Color.crimp(a)
self.value &= 0xFFFFFF
self.value = int(self.value)
Expand All @@ -1454,49 +1458,57 @@ def alpha(self, a):

@property
def red(self):
return (self.value >> 16) & 0xFF
return (self.value >> 16) & 0xFF if self.value is not None else None

@red.setter
def red(self, r):
if self.value is None:
raise ValueError
r = int(r & 0xFF)
self.value &= ~0xFF0000
r <<= 16
self.value |= r

@property
def green(self):
return (self.value >> 8) & 0xFF
return (self.value >> 8) & 0xFF if self.value is not None else None

@green.setter
def green(self, g):
if self.value is None:
raise ValueError
g = int(g & 0xFF)
self.value &= ~0xFF00
g <<= 8
self.value |= g

@property
def blue(self):
return self.value & 0xFF
return self.value & 0xFF if self.value is not None else None

@blue.setter
def blue(self, b):
if self.value is None:
raise ValueError
b = int(b & 0xFF)
self.value &= ~0xFF
self.value |= b

@property
def hexa(self):
return '#%02x%02x%02x%02x' % (self.alpha, self.red, self.green, self.blue)
return '#%02x%02x%02x%02x' % (self.alpha, self.red, self.green, self.blue) if self.value is not None else None

@property
def hex(self):
if self.alpha == 0xFF:
return '#%02x%02x%02x' % (self.red, self.green, self.blue)
return '#%02x%02x%02x' % (self.red, self.green, self.blue) if self.value is not None else None
else:
return '#%02x%02x%02x%02x' % (self.alpha, self.red, self.green, self.blue)
return self.hexa

@property
def hue(self):
if self.value is None:
return None
r = self.red / 255.0
g = self.green / 255.0
b = self.blue / 255.0
Expand All @@ -1522,11 +1534,15 @@ def hue(self):

@hue.setter
def hue(self, v):
if self.value is None:
raise ValueError
h, s, l = self.hsl
self.hsl = v, s, l

@property
def saturation(self):
if self.value is None:
return None
r = self.red / 255.0
g = self.green / 255.0
b = self.blue / 255.0
Expand All @@ -1542,11 +1558,15 @@ def saturation(self):

@saturation.setter
def saturation(self, v):
if self.value is None:
raise ValueError
h, s, l = self.hsl
self.hsl = h, v, l

@property
def lightness(self):
if self.value is None:
return None
r = self.red / 255.0
g = self.green / 255.0
b = self.blue / 255.0
Expand All @@ -1556,18 +1576,24 @@ def lightness(self):

@lightness.setter
def lightness(self, v):
if self.value is None:
raise ValueError
h, s, l = self.hsl
self.hsl = h, s, v

@property
def intensity(self):
if self.value is None:
return None
r = self.red
g = self.green
b = self.blue
return (r + b + g) / 768.0

@property
def brightness(self):
if self.value is None:
return None
r = self.red
g = self.green
b = self.blue
Expand All @@ -1576,17 +1602,23 @@ def brightness(self):

@property
def blackness(self):
if self.value is None:
return None
return 1.0 - self.brightness

@property
def luminance(self):
if self.value is None:
return None
r = self.red / 255.0
g = self.green / 255.0
b = self.blue / 255.0
return r * 0.3 + g * 0.59 + b * 0.11

@property
def luma(self):
if self.value is None:
return None
r = self.red / 255.0
g = self.green / 255.0
b = self.blue / 255.0
Expand Down Expand Up @@ -1674,6 +1706,8 @@ def crimp(v):

@property
def hsl(self):
if self.value is None:
return None
return self.hue, self.saturation, self.lightness

@hsl.setter
Expand Down Expand Up @@ -1724,7 +1758,6 @@ def blend(self, other, opacity=None):
color.opacity = opacity
self.value = Color.over(color, self)


class Point:
"""Point is a general subscriptable point class with .x and .y as well as [0] and [1]
Expand Down
42 changes: 41 additions & 1 deletion test/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,44 @@ def test_parse_stroke_opacity(self):
''')
m = list(SVG.parse(q).elements())
r = m[1]
self.assertAlmostEqual(r.stroke.opacity, 0.2, delta=1.0/255.0)
self.assertAlmostEqual(r.stroke.opacity, 0.2, delta=1.0/255.0)

def test_color_none(self):
color = Color(None)
self.assertEqual(color, SVG_VALUE_NONE)
self.assertEqual(color.red, None)
self.assertEqual(color.green, None)
self.assertEqual(color.blue, None)
self.assertEqual(color.alpha, None)
self.assertEqual(color.opacity, None)
self.assertEqual(color.hexa, None)
self.assertEqual(color.hex, None)
self.assertEqual(color.blackness, None)
self.assertEqual(color.brightness, None)
self.assertEqual(color.hsl, None)
self.assertEqual(color.hue, None)
self.assertEqual(color.saturation, None)
self.assertEqual(color.lightness, None)
self.assertEqual(color.luma, None)
self.assertEqual(color.luminance, None)
self.assertEqual(color.intensity, None)

def set_red():
color.red = 0
self.assertRaises(ValueError, set_red)

def set_green():
color.green = 0
self.assertRaises(ValueError, set_green)

def set_blue():
color.blue = 0
self.assertRaises(ValueError, set_blue)

def set_alpha():
color.alpha = 0
self.assertRaises(ValueError, set_alpha)

def set_opacity():
color.opacity = 1
self.assertRaises(ValueError, set_opacity)

0 comments on commit d561543

Please sign in to comment.