diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:24:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:24:48 +0000 |
commit | cca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch) | |
tree | 146f39ded1c938019e1ed42d30923c2ac9e86789 /share/extensions/color_randomize.py | |
parent | Initial commit. (diff) | |
download | inkscape-12fc8abae6d434cac7670a59ed3a67301cc2eb10.tar.xz inkscape-12fc8abae6d434cac7670a59ed3a67301cc2eb10.zip |
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rwxr-xr-x | share/extensions/color_randomize.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/share/extensions/color_randomize.py b/share/extensions/color_randomize.py new file mode 100755 index 0000000..e25dc6d --- /dev/null +++ b/share/extensions/color_randomize.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +"""Randomise the selected item's colours using hsl colorspace""" + +from random import randrange, uniform, seed +import inkex +from inkex.localization import inkex_gettext as _ + + +def _rand( + limit, value, roof=255, method=randrange, circular=False, deterministic=False +): + """Generates a random number which is less than limit % away from value, using the method + supplied.""" + if deterministic: + if isinstance(value, float): + seed(int(value * 1000)) + else: + seed(value) + limit = roof * float(limit) / 100 + limit /= 2 + max_ = type(roof)(value + limit) + min_ = type(roof)(value - limit) + if not (circular): + if max_ > roof: + min_ -= max_ - roof + max_ = roof + if min_ < 0: + max_ -= min_ + min_ = 0 + return method(min_, max_) + return method(min_, max_) % roof + + +class Randomize(inkex.ColorExtension): + """Randomize the colours of all objects""" + + deterministic_output = False + + def add_arguments(self, pars): + pars.add_argument("--tab") + pars.add_argument("-y", "--hue_range", type=int, default=0, help="Hue range") + pars.add_argument( + "-t", "--saturation_range", type=int, default=0, help="Saturation range" + ) + pars.add_argument( + "-m", "--lightness_range", type=int, default=0, help="Lightness range" + ) + pars.add_argument( + "-o", "--opacity_range", type=int, default=0, help="Opacity range" + ) + + def _rand(self, limit, value, roof=255, method=randrange, circular=False): + return _rand( + limit, + value, + roof, + method, + circular, + deterministic=self.deterministic_output, + ) + + def modify_color(self, name, color): + hsl = color.to_hsl() + if self.options.hue_range > 0: + hsl.hue = int(self._rand(self.options.hue_range, hsl.hue, circular=True)) + if self.options.saturation_range > 0: + hsl.saturation = int( + self._rand(self.options.saturation_range, hsl.saturation) + ) + if self.options.lightness_range > 0: + hsl.lightness = int(self._rand(self.options.lightness_range, hsl.lightness)) + return hsl.to_rgb() + + def modify_opacity(self, name, opacity): + if name != "opacity": + return opacity + try: + opacity = float(opacity) + except ValueError: + self.msg(_("Ignoring unusual opacity value: {}").format(opacity)) + return opacity + orange = self.options.opacity_range + if orange > 0: + return self._rand(orange, opacity, roof=1.0, method=uniform) + return opacity + + +if __name__ == "__main__": + Randomize().run() |