diff --git a/pybadges/__init__.py b/pybadges/__init__.py index 4bf2075..757dbd4 100644 --- a/pybadges/__init__.py +++ b/pybadges/__init__.py @@ -122,7 +122,7 @@ def badge( logo: Optional[str] = None, left_color: str = '#555', right_color: str = '#007ec6', - center_color: str = '#555', + center_color: Optional[str] = None, measurer: Optional[text_measurer.TextMeasurer] = None, left_title: Optional[str] = None, right_title: Optional[str] = None, @@ -193,11 +193,13 @@ def badge( raise ValueError( 'whole_link may not bet set with left_link, right_link, or center_link' ) + + # NOTE: Can only have a center_* element if also a right_* element + if center_image and not (right_image or right_text): + raise ValueError('cannot have a center_image without a right element') - if right_image and center_image: - raise ValueError('cannot have both right_image and center_image') - - template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg') + if (center_image and not center_color) or (not center_image and center_color): + raise ValueError('must have both a center_image and a center_color') if logo and embed_logo: logo = _embed_image(logo) @@ -208,10 +210,15 @@ def badge( if center_image and embed_center_image: center_image = _embed_image(center_image) + if center_color: + center_color = _NAME_TO_COLOR.get(center_color, center_color) + right_text_width = None if right_text: right_text_width = measurer.text_width(right_text) / 10.0 + template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg') + svg = template.render( left_text=left_text, right_text=right_text, @@ -224,7 +231,7 @@ def badge( logo=logo, left_color=_NAME_TO_COLOR.get(left_color, left_color), right_color=_NAME_TO_COLOR.get(right_color, right_color), - center_color=_NAME_TO_COLOR.get(center_color, center_color), + center_color=center_color, left_title=left_title, right_title=right_title, center_title=center_title, diff --git a/pybadges/__main__.py b/pybadges/__main__.py index dcc6c99..c47aa48 100644 --- a/pybadges/__main__.py +++ b/pybadges/__main__.py @@ -22,6 +22,7 @@ import sys import tempfile import webbrowser +sys.path.append('/home/nick/git/pybadges/') import pybadges from pybadges.version import __version__ @@ -71,7 +72,7 @@ def main(): help='the background color of the right-hand-side of the badge') parser.add_argument( '--center-color', - default='#555', + default=None, help='the background color of the right-hand-side of the badge') parser.add_argument('--browser', action='store_true', diff --git a/pybadges/badge-template-full.svg b/pybadges/badge-template-full.svg index fa97090..339494c 100644 --- a/pybadges/badge-template-full.svg +++ b/pybadges/badge-template-full.svg @@ -1,8 +1,9 @@ {% set logo_width = 14 if logo else 0 %} {% set logo_padding = 3 if (logo and left_text) else 0 %} +{% set image_width = 107 if center_image else 0 %} {% set left_width = left_text_width + 10 + logo_width + logo_padding %} -{% set right_width = right_text_width + 10 if right_text_width else 0 %} -{% set image_width = 107 if right_image else 0 %} +{% set center_width = image_width + 10 if center_image else 0 %} +{% set right_width = center_width + right_text_width + 10 if right_text else 0 %} {% set id_smooth = 'smooth' + id_suffix %} {% set id_round = 'round' + id_suffix %} @@ -19,31 +20,43 @@ - + + {% if left_title %} {{ left_title }} {% endif %} - + + {% if center_image %} + + {% if center_title %} + {{ center_title }} + {% endif %} + + {% endif %} + + {% if right_title %} {{ right_title }} {% endif %} + + {% if logo %} - + {% endif %} {{ left_text }} {{ left_text }} - {% if right_image %} - + {% if center_image %} + {% endif %} {% if right_text %} - {{ right_text }} - {{ right_text }} + {{ right_text }} + {{ right_text }} {% endif %} {% if left_link or whole_link %}