From 19d56db34384cf40c8e39efa95165e0f595ce7f9 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Mon, 18 Oct 2021 15:35:53 +0200 Subject: [PATCH] added grey to monoalpha --- .../grey_to_monoalpha/grey_to_monoalpha.inx | 48 +++++++++ .../grey_to_monoalpha/grey_to_monoalpha.py | 99 +++++++++++++++++++ .../grey_to_monoalpha/meta.json | 20 ++++ 3 files changed, 167 insertions(+) create mode 100644 extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.inx create mode 100644 extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.py create mode 100644 extensions/fablabchemnitz/grey_to_monoalpha/meta.json diff --git a/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.inx b/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.inx new file mode 100644 index 00000000..54d39779 --- /dev/null +++ b/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.inx @@ -0,0 +1,48 @@ + + + Grey to MonoAlpha + fablabchemnitz.de.grey_to_monoalpha + + + + + 0x000000ff + + + + + + + + + + + 0 + 1 + + + + + + + + + + + + path + + + + + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.py b/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.py new file mode 100644 index 00000000..1bbe1ae4 --- /dev/null +++ b/extensions/fablabchemnitz/grey_to_monoalpha/grey_to_monoalpha.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +# +# Copyright (C) [2021] [Matt Cottam], [mpcottam@raincloud.co.uk] +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +############################################################################## +# Grey To Mono Alpha *** Convert Greys to Monochrome with varying Opacity +############################################################################## + +import math + +import inkex +from inkex import Color + +# Python Standard Libary + +from statistics import mean + + +def get_attributes(self): + for att in dir(self): + inkex.errormsg((att, getattr(self, att))) + + +def rgba_to_bw_rgba(self, my_objects): + apply_to = self.options.apply_to_type_radio + mono_color = self.options.color_picker_mono.to_rgba() + opacity_lower_threshold = self.options.opacity_lower_threshold + opacity_upper_threshold = self.options.opacity_upper_threshold + opacity_range = opacity_upper_threshold - opacity_lower_threshold + + for my_object in my_objects: + + if 'fill' in apply_to and ('fill:none' not in str(my_object.style)): + my_fill_color = my_object.style.get_color(name='fill').to_rgba() + my_fill_color_red = my_fill_color[0] + my_fill_color_green = my_fill_color[1] + my_fill_color_blue = my_fill_color[2] + mean_fill_component_value = mean([my_fill_color_red, my_fill_color_blue, my_fill_color_green]) + + if mean_fill_component_value > 0: + mono_opacity = (1 - (mean_fill_component_value / 256)) * opacity_range + mono_opacity = mono_opacity + opacity_lower_threshold + else: + mono_opacity = opacity_upper_threshold + + my_object.style['fill'] = str(mono_color) + my_object.style['fill-opacity'] = str(mono_opacity) + + if 'stroke' in apply_to and (';stroke:none' not in str(my_object.style)) and ('stroke:' in str(my_object.style)): + my_stroke_color = my_object.style.get_color(name='stroke').to_rgba() + my_stroke_color_red = my_stroke_color[0] + my_stroke_color_green = my_stroke_color[1] + my_stroke_color_blue = my_stroke_color[2] + mean_stroke_component_value = mean([my_stroke_color_red, my_stroke_color_blue, my_stroke_color_green]) + + if mean_stroke_component_value > 0: + mono_opacity = (1 - (mean_stroke_component_value / 256)) * opacity_range + mono_opacity = mono_opacity + opacity_lower_threshold + else: + mono_opacity = opacity_upper_threshold + + my_object.style['stroke'] = str(mono_color) + my_object.style['stroke-opacity'] = str(mono_opacity) + + +class GreyToMonoAlpha(inkex.EffectExtension): + + def add_arguments(self, pars): + pars.add_argument("--tab") + pars.add_argument("--color_picker_mono", type=inkex.colors.Color, default=0) + pars.add_argument("--apply_to_type_radio", default=None) + pars.add_argument("--opacity_lower_threshold", type=float, default=0) + pars.add_argument("--opacity_upper_threshold", type=float, default=1) + + def effect(self): + my_objects = self.svg.selected + if len(my_objects) < 1: + self.msg('Please select some paths first.') + return + rgba_to_bw_rgba(self, my_objects) + + +if __name__ == '__main__': + GreyToMonoAlpha().run() diff --git a/extensions/fablabchemnitz/grey_to_monoalpha/meta.json b/extensions/fablabchemnitz/grey_to_monoalpha/meta.json new file mode 100644 index 00000000..9c32922e --- /dev/null +++ b/extensions/fablabchemnitz/grey_to_monoalpha/meta.json @@ -0,0 +1,20 @@ +[ + { + "name": "Grey To MonoAlpha", + "id": "fablabchemnitz.de.grey_to_monoalpha", + "path": "grey_to_monoalpha", + "original_name": "Grey to MonoAlpha", + "original_id": "org.inkscape.grey_to_monoalpha", + "license": "GNU GPL v3", + "license_url": "https://gitlab.com/inklinea/grey-to-mono-alpha/-/blob/main/LICENSE", + "comment": "", + "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/grey_to_monoalpha", + "fork_url": "https://gitlab.com/inklinea/grey-to-mono-alpha", + "documentation_url": "https://stadtfabrikanten.org/display/IFM/Grey+to+MonoAlpha", + "inkscape_gallery_url": null, + "main_authors": [ + "gitlab.com/inklinea", + "github.com/vmario89" + ] + } +] \ No newline at end of file