{# shortcut macro to raise an exception from jinja #}
{% macro exception(message) %}
  {{ None['Error: ' + message] }}
{% endmacro %}

{# given an environment variable and an opacity, construct a hex color code for CSS #}
{# parameters: #}
{# color_var: an environment variable containing an RGB hex color. #}
{#            the envvar *must* be prefixed with "LEKTOR_", but the color_var *must not* have this prefix. #}
{#            ie `LEKTOR_COLOR=112233 ... lektor s` -> `theme_color('COLOR')` #}
{# opacity: the opacity of the color, as an int or float representing a percent. #}
{#          ie 20% -> `theme_color('COLOR', 20)` OR 7.5% -> `theme_color('COLOR', 7.5)` #}
{% macro theme_color(color_var, opacity=100) %}
  {%- set hex_color = envvars(color_var).lstrip('#').rstrip(';') -%}
  {%- if not hex_color %}
    {{ exception('environment variable ' + color_var + ' not set') }}
  {% elif hex_color|length != 6 %}
    {{ exception('hex color must be exactly 6 characters') }}
  {% elif opacity|int(base=16) > 255 or opacity|int(base=16) < 0 %}
    {{ exception('opacity: ' + opacity + ' outside allowed values of 00-FF') }}
  {% endif -%}
  {{- '#{hex_color}{opacity:x};'.format(hex_color=hex_color, opacity=(opacity / 100 * 255)|round|int) -}}
{% endmacro %}

{# generate a series of invisible spans that contain the translated readable OS name. #}
{# the data-os-name property ensures this is easily selectable from javascript. #}
{# this macro exists to get the "For {your operating system}" text into translations, since we dont have a way to translate JS strings at runtime for this project #}
{# parameters: #}
{#     names dict[str, str]: a dict mapping a sequence of normalized platform names to user-readable names #}
{# example: `os_names({ 'ipad iphone': 'iOS'})` #}
{% macro os_names(names) %}
  <div id="os-span-data">
    <span id="ios-browser-name">{{ _('Onion Browser') }}</span>
    {% for normalized_names, readable_name in names.items() %}
      <span data-os-name="{{ normalized_names }}">{{ _('For {}'.format(readable_name)) }}</span>
    {% endfor %}
  </div>
{% endmacro %}
