docs: def color_to_webcolor(color, allow_css3_colors=True, optimize=True):
      """\
      Returns either a hexadecimal code or a color name.
  
      :param color: A web color name (i.e. ``darkblue``) or a hexadecimal value
              (``#RGB`` or ``#RRGGBB``) or a RGB(A) tuple (i.e. ``(R, G, B)`` or
              ``(R, G, B, A)``)
      :param bool allow_css3_colors: Indicates if a CSS3 color value like
              rgba(R G, B, A) is an acceptable result.
      :param bool optimize: Inidcates if the shortest possible color value should
              be returned (default: ``True``).
      :rtype: str
      :return: The provided color as web color: ``#RGB``, ``#RRGGBB``,
              ``rgba(R, G, B, A)``, or web color name.
      """
      if color_is_black(color):
          return '#000'
      elif color_is_white(color):
          return '#fff'
      clr = color_to_rgb_or_rgba(color)
      alpha_channel = None
      if len(clr) == 4:
          if allow_css3_colors:
              return 'rgba({0},{1},{2},{3})'.format(*clr)
          alpha_channel = clr[3]
          clr = clr[:3]
      hx = '#{0:02x}{1:02x}{2:02x}'.format(*clr)
      if optimize:
          if hx == '#d2b48c':
              hx = 'tan'  # shorter
          elif hx == '#ff0000':
              hx = 'red'  # shorter
          elif hx[1] == hx[2] and hx[3] == hx[4] and hx[5] == hx[6]:
              hx = '#{0}{1}{2}'.format(hx[1], hx[3], hx[5])
      return hx if alpha_channel is None else (hx, alpha_channel)