From 03cc48c0ac04b4b3a58a3bc4112795caea70f8fa Mon Sep 17 00:00:00 2001 From: Jonathan Neuhauser Date: Fri, 28 May 2021 23:46:06 +0200 Subject: [PATCH 1/2] randomize shapes with unset opacity as well, add unit tests --- color_randomize.py | 2 + inkex/extensions.py | 8 ++-- ...__layer_group_path__--id__root_rect_uu.out | 40 +++++++++++++++++ ...--id__r3__--id__r4__--id__r5__--id__r6.out | 43 +++++++++++++++++++ tests/test_color_randomize.py | 25 +++++++++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 tests/data/refs/color_randomize__-y__0__-t__0__-m__0__-o__100__--id__layer_group_rect_uu2__--id__layer_group_path__--id__root_rect_uu.out create mode 100644 tests/data/refs/color_randomize__-y__50__-t__50__-m__50__-o__100__--id__r1__--id__r2__--id__r3__--id__r4__--id__r5__--id__r6.out diff --git a/color_randomize.py b/color_randomize.py index bf07de4b..4e9f8b41 100755 --- a/color_randomize.py +++ b/color_randomize.py @@ -44,6 +44,8 @@ class Randomize(inkex.ColorExtension): return hsl.to_rgb() def modify_opacity(self, name, opacity): + if name != "opacity": + return opacity try: opacity = float(opacity) except ValueError: diff --git a/inkex/extensions.py b/inkex/extensions.py index 6f72f808..ed6029d6 100644 --- a/inkex/extensions.py +++ b/inkex/extensions.py @@ -35,6 +35,7 @@ from .elements import load_svg, BaseElement, ShapeElement, Group, Layer, Grid, \ from .elements._utils import CloningVat from .base import InkscapeExtension, SvgThroughMixin, SvgInputMixin, SvgOutputMixin, TempDirMixin from .transforms import Transform +from .properties import all_properties # All the names that get added to the inkex API itself. __all__ = ('EffectExtension', 'GenerateExtension', 'InputExtension', @@ -310,9 +311,10 @@ class ColorExtension(EffectExtension): pass # bad color value, don't touch. # Then opacities (usually does nothing) for name in elem.style.opacity_props: - value = style.get(name) - if value is not None: - elem.style[name] = self.modify_opacity(name, value) + value = style(name) + result = self.modify_opacity(name, value) + if result != value and result != 1: + elem.style[name] = result def _ref_cloned(self, old_id, new_id, style, name): self._renamed[old_id] = new_id diff --git a/tests/data/refs/color_randomize__-y__0__-t__0__-m__0__-o__100__--id__layer_group_rect_uu2__--id__layer_group_path__--id__root_rect_uu.out b/tests/data/refs/color_randomize__-y__0__-t__0__-m__0__-o__100__--id__layer_group_rect_uu2__--id__layer_group_path__--id__root_rect_uu.out new file mode 100644 index 00000000..60b426ba --- /dev/null +++ b/tests/data/refs/color_randomize__-y__0__-t__0__-m__0__-o__100__--id__layer_group_rect_uu2__--id__layer_group_path__--id__root_rect_uu.out @@ -0,0 +1,40 @@ + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/data/refs/color_randomize__-y__50__-t__50__-m__50__-o__100__--id__r1__--id__r2__--id__r3__--id__r4__--id__r5__--id__r6.out b/tests/data/refs/color_randomize__-y__50__-t__50__-m__50__-o__100__--id__r1__--id__r2__--id__r3__--id__r4__--id__r5__--id__r6.out new file mode 100644 index 00000000..480b60b9 --- /dev/null +++ b/tests/data/refs/color_randomize__-y__50__-t__50__-m__50__-o__100__--id__r1__--id__r2__--id__r3__--id__r4__--id__r5__--id__r6.out @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/test_color_randomize.py b/tests/test_color_randomize.py index 153f9db1..044e88ec 100644 --- a/tests/test_color_randomize.py +++ b/tests/test_color_randomize.py @@ -1,6 +1,7 @@ # coding=utf-8 from color_randomize import Randomize from .test_inkex_extensions import ColorBaseCase +from inkex.tester import ComparisonMixin, TestCase class ColorRandomizeTest(ColorBaseCase): effect_class = Randomize @@ -33,8 +34,32 @@ class ColorRandomizeTest(ColorBaseCase): (1.0, 0.43, ['-o 100']), # Other units are available ('0.5', 0.654, ['-o 54']), + # Test no opacity + # The opacity value should be lesser than 1 ] def test_bad_opacity(self): """Bad opacity error handled""" self.effect.modify_opacity('opacity', 'hello') + +class TestRandomizeGradients(ComparisonMixin, TestCase): + """Direct tests for color mechanisms""" + effect_class = Randomize + compare_file = 'svg/colors.svg' + python3_only = True + + comparisons = [ + ('-y 50', '-t 50', '-m 50', '-o 100', "--id=r1", "--id=r2", "--id=r3", "--id=r4", + "--id=r5", "--id=r6"), + ] + +class TestRandomizeOpacity(ComparisonMixin, TestCase): + """Direct tests for color mechanisms""" + effect_class = Randomize + compare_file = 'svg/dpiswitcher_96dpi.svg' + python3_only = True + + comparisons = [ + ('-y 0', '-t 0', '-m 0', '-o 100', "--id=layer_group_rect_uu2", "--id=layer_group_path", + "--id=root_rect_uu"), + ] \ No newline at end of file -- GitLab From 458d9163bc8986d80558b2abdfca60bbcc157795 Mon Sep 17 00:00:00 2001 From: Jonathan Neuhauser Date: Fri, 28 May 2021 23:55:24 +0200 Subject: [PATCH 2/2] fix unit tests --- inkex/extensions.py | 2 +- tests/data/refs/inkex_extensions_color.out | 12 ++++++------ .../refs/inkex_extensions_color__--id__color_svg.out | 12 ++++++------ tests/data/refs/inkex_extensions_color__--id__r1.out | 2 +- .../inkex_extensions_color__--id__r1__--id__r2.out | 4 ++-- tests/data/refs/inkex_extensions_color__--id__r2.out | 2 +- tests/data/refs/inkex_extensions_color__--id__r3.out | 2 +- tests/data/refs/inkex_extensions_color__--id__r4.out | 2 +- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/inkex/extensions.py b/inkex/extensions.py index ed6029d6..1058dce5 100644 --- a/inkex/extensions.py +++ b/inkex/extensions.py @@ -313,7 +313,7 @@ class ColorExtension(EffectExtension): for name in elem.style.opacity_props: value = style(name) result = self.modify_opacity(name, value) - if result != value and result != 1: + if result != value and result != 1: # only modify if not equal to old or default elem.style[name] = result def _ref_cloned(self, old_id, new_id, style, name): diff --git a/tests/data/refs/inkex_extensions_color.out b/tests/data/refs/inkex_extensions_color.out index 00971ed4..f2d7ded0 100644 --- a/tests/data/refs/inkex_extensions_color.out +++ b/tests/data/refs/inkex_extensions_color.out @@ -24,15 +24,15 @@ ]]> - + - + - + - + - + - + \ No newline at end of file diff --git a/tests/data/refs/inkex_extensions_color__--id__color_svg.out b/tests/data/refs/inkex_extensions_color__--id__color_svg.out index 00971ed4..f2d7ded0 100644 --- a/tests/data/refs/inkex_extensions_color__--id__color_svg.out +++ b/tests/data/refs/inkex_extensions_color__--id__color_svg.out @@ -24,15 +24,15 @@ ]]> - + - + - + - + - + - + \ No newline at end of file diff --git a/tests/data/refs/inkex_extensions_color__--id__r1.out b/tests/data/refs/inkex_extensions_color__--id__r1.out index b14a1fb8..2a106d1d 100644 --- a/tests/data/refs/inkex_extensions_color__--id__r1.out +++ b/tests/data/refs/inkex_extensions_color__--id__r1.out @@ -24,7 +24,7 @@ ]]> - + diff --git a/tests/data/refs/inkex_extensions_color__--id__r1__--id__r2.out b/tests/data/refs/inkex_extensions_color__--id__r1__--id__r2.out index 0e560286..176992b4 100644 --- a/tests/data/refs/inkex_extensions_color__--id__r1__--id__r2.out +++ b/tests/data/refs/inkex_extensions_color__--id__r1__--id__r2.out @@ -24,9 +24,9 @@ ]]> - + - + diff --git a/tests/data/refs/inkex_extensions_color__--id__r2.out b/tests/data/refs/inkex_extensions_color__--id__r2.out index 6c9b7f5f..430e485b 100644 --- a/tests/data/refs/inkex_extensions_color__--id__r2.out +++ b/tests/data/refs/inkex_extensions_color__--id__r2.out @@ -26,7 +26,7 @@ - + diff --git a/tests/data/refs/inkex_extensions_color__--id__r3.out b/tests/data/refs/inkex_extensions_color__--id__r3.out index 81170c81..f663d51b 100644 --- a/tests/data/refs/inkex_extensions_color__--id__r3.out +++ b/tests/data/refs/inkex_extensions_color__--id__r3.out @@ -28,7 +28,7 @@ - + diff --git a/tests/data/refs/inkex_extensions_color__--id__r4.out b/tests/data/refs/inkex_extensions_color__--id__r4.out index 6983733e..9c93172e 100644 --- a/tests/data/refs/inkex_extensions_color__--id__r4.out +++ b/tests/data/refs/inkex_extensions_color__--id__r4.out @@ -34,7 +34,7 @@ - + -- GitLab