Skip to content
Snippets Groups Projects
Commit d92b1284 authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Allow custom color of the ID. Must be given in RGB values between 0 and 1.

parent 9a7622c5
No related branches found
No related tags found
6 merge requests!65Release v0.3.0,!64New year developments,!63Improve handling of long IDs,!62Export imported modules,!61Custom IDs,!60Enable customization of plotted IDs
Pipeline #877924 passed with warnings
...@@ -53,6 +53,9 @@ class PlotOptions: ...@@ -53,6 +53,9 @@ class PlotOptions:
file has to be given. file has to be given.
fontsize: int, optional fontsize: int, optional
Fontsize for the displayed ID. Default: 12. Fontsize for the displayed ID. Default: 12.
fontcolor: tuple, optional
Fontcolor for the ID. Must be given as tuple containing three rgb values with
each value between [0, 1]. Default: (0, 0, 0).
""" """
def __init__(self, figs, rotation, position, **kwargs): def __init__(self, figs, rotation, position, **kwargs):
...@@ -71,6 +74,7 @@ class PlotOptions: ...@@ -71,6 +74,7 @@ class PlotOptions:
if self.font: if self.font:
self.font = os.path.abspath(self.font) self.font = os.path.abspath(self.font)
self.fontsize = kwargs.get("fontsize", 12) self.fontsize = kwargs.get("fontsize", 12)
self.fontcolor = kwargs.get("fontcolor", (0, 0, 0))
def __str__(self): def __str__(self):
"""Representation if an object of this class is printed.""" """Representation if an object of this class is printed."""
......
...@@ -63,6 +63,9 @@ def tagplot(figs, engine, **kwargs): ...@@ -63,6 +63,9 @@ def tagplot(figs, engine, **kwargs):
file has to be given. file has to be given.
fontsize: int, optional fontsize: int, optional
Fontsize for the displayed ID. Default: 12. Fontsize for the displayed ID. Default: 12.
fontcolor: tuple, optional
Fontcolor for the ID. Must be given as tuple containing three rgb values with
each value between [0, 1]. Default: (0, 0, 0).
Raises Raises
------ ------
......
...@@ -41,7 +41,7 @@ def tagplot_image(plotid_object): ...@@ -41,7 +41,7 @@ def tagplot_image(plotid_object):
raise TypeError("File does not exist.") raise TypeError("File does not exist.")
# Check if figs is a valid file is done by pillow internally # Check if figs is a valid file is done by pillow internally
color = (128, 128, 128) # grey color = tuple(rgb_value * 255 for rgb_value in plotid_object.fontcolor)
font = ImageFont.load_default() font = ImageFont.load_default()
if plotid_object.font: if plotid_object.font:
......
...@@ -39,8 +39,6 @@ def tagplot_matplotlib(plotid_object): ...@@ -39,8 +39,6 @@ def tagplot_matplotlib(plotid_object):
if not isinstance(figure, matplotlib.figure.Figure): if not isinstance(figure, matplotlib.figure.Figure):
raise TypeError("Figure is not a valid matplotlib-figure.") raise TypeError("Figure is not a valid matplotlib-figure.")
color = "grey"
# Loop to create and position the IDs # Loop to create and position the IDs
for fig in plotid_object.figs: for fig in plotid_object.figs:
fig_id = create_id(plotid_object.id_method) fig_id = create_id(plotid_object.id_method)
...@@ -63,7 +61,7 @@ def tagplot_matplotlib(plotid_object): ...@@ -63,7 +61,7 @@ def tagplot_matplotlib(plotid_object):
wrap=True, wrap=True,
rotation=plotid_object.rotation, rotation=plotid_object.rotation,
fontsize=plotid_object.fontsize, fontsize=plotid_object.fontsize,
color=color, color=plotid_object.fontcolor,
font=font, font=font,
) )
......
...@@ -46,13 +46,13 @@ class TestTagplot(unittest.TestCase): ...@@ -46,13 +46,13 @@ class TestTagplot(unittest.TestCase):
id_method="random", id_method="random",
id_on_plot=False, id_on_plot=False,
) )
print(str(plot_obj))
self.assertEqual( self.assertEqual(
str(plot_obj), str(plot_obj),
"<class 'plotid.plotoptions.PlotOptions'>: {'figs': 'FIG', 'figure_ids': " "<class 'plotid.plotoptions.PlotOptions'>: {'figs': 'FIG', 'figure_ids': "
"[], 'rotation': 270, 'position': (100, 200), 'prefix': 'xyz', 'id_method':" "[], 'rotation': 270, 'position': (100, 200), 'prefix': 'xyz', 'id_method':"
" 'random', 'qrcode': False, 'qr_position': (1, 0), 'qr_size': 100, " " 'random', 'qrcode': False, 'qr_position': (1, 0), 'qr_size': 100, "
"'id_on_plot': False, 'font': False, 'fontsize': 12}", "'id_on_plot': False, 'font': False, 'fontsize': 12, 'fontcolor': "
"(0, 0, 0)}",
) )
def test_str_plotidtransfer(self): def test_str_plotidtransfer(self):
......
...@@ -48,6 +48,7 @@ class TestTagplotImage(unittest.TestCase): ...@@ -48,6 +48,7 @@ class TestTagplotImage(unittest.TestCase):
qrcode=True, qrcode=True,
fontsize=10, fontsize=10,
font="fonts/xolonium-fonts-4.1/ttf/Xolonium-Bold.ttf", font="fonts/xolonium-fonts-4.1/ttf/Xolonium-Bold.ttf",
fontcolor=(0, 1, 0),
) )
options.validate_input() options.validate_input()
figs_and_ids = tagplot_image(options) figs_and_ids = tagplot_image(options)
......
...@@ -38,6 +38,7 @@ class TestTagplotMatplotlib(unittest.TestCase): ...@@ -38,6 +38,7 @@ class TestTagplotMatplotlib(unittest.TestCase):
qrcode=True, qrcode=True,
fontsize=10, fontsize=10,
font="tests/fonts/xolonium-fonts-4.1/otf/Xolonium-Regular.otf", font="tests/fonts/xolonium-fonts-4.1/otf/Xolonium-Regular.otf",
fontcolor=(0, 0, 1),
) )
options.validate_input() options.validate_input()
figs_and_ids = tagplot_matplotlib(options) figs_and_ids = tagplot_matplotlib(options)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment