예제 #1
0
    def _write_fill_type(self, fill: FillFormat):
        if self.fill_type == FillType.NOFILL:
            fill.background()

        elif self.fill_type == FillType.SOLID:
            if (self.fore_color_rgb is not None) or (self.fore_color_mso_theme
                                                     is not None):
                fill.solid()
                self._write_fore_color(fill)
            else:
                print(
                    "Warning: Cannot set FillType.SOLID without a valid fore_color_*."
                )

        elif self.fill_type == FillType.PATTERNED:
            fill.patterned()
            if self.pattern is not None:
                fill.pattern = self.pattern
            if (self.fore_color_rgb is not None) or (self.fore_color_mso_theme
                                                     is not None):
                self._write_fore_color(fill)
            if (self.back_color_rgb is not None) or (self.back_color_mso_theme
                                                     is not None):
                self._write_back_color(fill)

        elif self.fill_type == FillType.GRADIENT:
            print("FillType.GRADIENT not implemented jet.")
예제 #2
0
def fill_color(fill: FillFormat, val: Union[str, tuple, list, None]) -> None:
    '''
    Set the FillFormat color to value specified as a:

    - a named color, like ``black``
    - a hex value, like ``#f80`` or ``#ff8800``
    - an RGB value, like ``rgb(255, 255, 0)`` or ``rgb(1, 0.5, 0.1)``
    - a tuple or list of RGB values, like ``(255, 255, 0)`` or ``[255, 255, 0]``
    - a theme color, like ``ACCENT_1``, ``ACCENT_2``, ``BACKGROUND_1``, ``DARK_1``, ``LIGHT_2``
    - a theme color with a brightness modifier, like ``ACCENT_1+40``, which is 40% brighter than
      Accent 1, or ``ACCENT_2-20`` which is 20% darker than Accent 2
    - ``'none'`` clears the color, i.e. makes it transparent
    '''
    fill.solid()
    if val == 'none':
        fill.background()
    elif isinstance(val, (list, tuple)):
        val = val[:3]
        if any(isinstance(v, float) for v in val) and all(0 <= v <= 1
                                                          for v in val):
            fill.fore_color.rgb = RGBColor(*(int(v * 256 if v < 1 else 255)
                                             for v in val))  # noqa
        else:
            fill.fore_color.rgb = RGBColor(*val)
    elif isinstance(val, str):
        val = color_map.get(val, val).upper()
        if val.startswith('#'):
            if len(val) == 7:
                fill.fore_color.rgb = RGBColor.from_string(val[1:])
            elif len(val) == 4:
                fill.fore_color.rgb = RGBColor.from_string(val[1] * 2 +
                                                           val[2] * 2 +
                                                           val[3] * 2)
        elif val.startswith('RGB('):
            parts = re.findall(r'\d+', val)
            fill.fore_color.rgb = RGBColor(int(parts[0]), int(parts[1]),
                                           int(parts[2]))
        else:
            match = theme_color.match(val)
            if match:
                theme = match.group(1) + ('_' + match.group(2)
                                          if match.group(2) else '')
                fill.fore_color.theme_color = getattr(MSO_THEME_COLOR, theme)
                if match.group(3):
                    fill.fore_color.brightness = float(match.group(3)) / 100
                # else: No brightness adjustment required
            else:
                raise ValueError('Invalid color: %r' % val)