def it_can_change_the_angle_of_a_linear_gradient(self, grad_fill_, type_prop_): type_prop_.return_value = MSO_FILL.GRADIENT fill = FillFormat(None, grad_fill_) fill.gradient_angle = 42.24 assert grad_fill_.gradient_angle == 42.24
def it_raises_on_non_gradient_fill(self, grad_fill_, type_prop_): type_prop_.return_value = None fill = FillFormat(None, grad_fill_) with pytest.raises(TypeError): fill.gradient_angle with pytest.raises(TypeError): fill.gradient_angle = 123.4 with pytest.raises(TypeError): fill.gradient_stops
def fill(self): """|FillFormat| instance for this background. This |FillFormat| object is used to interrogate or specify the fill of the slide background. Note that accessing this property is potentially destructive. A slide background can also be specified by a background style reference and accessing this property will remove that reference, if present, and replace it with NoFill. This is frequently the case for a slide master background. This is also the case when there is no explicitly defined background (background is inherited); merely accessing this property will cause the background to be set to NoFill and the inheritance link will be interrupted. This is frequently the case for a slide background. Of course, if you are accessing this property in order to set the fill, then these changes are of no consequence, but the existing background cannot be reliably interrogated using this property unless you have already established it is an explicit fill. If the background is already a fill, then accessing this property makes no changes to the current background. """ bgPr = self._cSld.get_or_add_bgPr() return FillFormat.from_fill_parent(bgPr)
def fill(self): """ |FillFormat| instance for this cell, providing access to fill properties such as foreground color. """ tcPr = self._tc.get_or_add_tcPr() return FillFormat.from_fill_parent(tcPr)
def it_knows_the_angle_of_a_linear_gradient(self, grad_fill_, type_prop_): grad_fill_.gradient_angle = 42.0 type_prop_.return_value = MSO_FILL.GRADIENT fill = FillFormat(None, grad_fill_) angle = fill.gradient_angle assert angle == 42.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.")
def it_provides_access_to_the_gradient_stops(self, type_prop_, grad_fill_, gradient_stops_): type_prop_.return_value = MSO_FILL.GRADIENT grad_fill_.gradient_stops = gradient_stops_ fill = FillFormat(None, grad_fill_) gradient_stops = fill.gradient_stops assert gradient_stops is gradient_stops_
def set_solid_fixture_(self, request, xPr_bldr): fill_type_str = request.param xFill_bldr = self._xFill_bldr(fill_type_str) if xFill_bldr is not None: xPr_bldr.with_child(xFill_bldr) xPr = xPr_bldr.element fill_format = FillFormat.from_fill_parent(xPr) xPr_with_solidFill_xml = (xPr_bldr.clear().with_nsdecls().with_child( a_solidFill()).xml()) return fill_format, xPr_with_solidFill_xml
def solid_fixture(self, request, solid_fill_): cxml, expected_cxml = request.param fill = FillFormat.from_fill_parent(element(cxml)) # --mock must be after FillFormat call to avoid poss. contructor call _SolidFill_ = class_mock( request, "pptx.dml.fill._SolidFill", return_value=solid_fill_, autospec=True ) expected_xml = xml(expected_cxml) return fill, _SolidFill_, expected_xml, solid_fill_
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)
def background_fixture(self, request, no_fill_): cxml, expected_cxml = request.param fill = FillFormat.from_fill_parent(element(cxml)) # --mock must be after FillFormat call to avoid poss. contructor call _NoFill_ = class_mock(request, 'pptx.dml.fill._NoFill', return_value=no_fill_, autospec=True) expected_xml = xml(expected_cxml) return fill, _NoFill_, expected_xml, no_fill_
def solid_fixture(self, request, solid_fill_): cxml, expected_cxml = request.param fill = FillFormat.from_fill_parent(element(cxml)) # --mock must be after FillFormat call to avoid poss. contructor call _SolidFill_ = class_mock(request, "pptx.dml.fill._SolidFill", return_value=solid_fill_, autospec=True) expected_xml = xml(expected_cxml) return fill, _SolidFill_, expected_xml, solid_fill_
def fore_color_fixture_(self, request, xPr_bldr): mapping = { 'solid': ColorFormat, } fill_type_str = request.param fore_color_type = mapping[fill_type_str] xFill_bldr = self._xFill_bldr(fill_type_str) if xFill_bldr is not None: xPr_bldr.with_child(xFill_bldr) xPr = xPr_bldr.element fill_format = FillFormat.from_fill_parent(xPr) return fill_format, fore_color_type
def set_solid_fixture_(self, request, xPr_bldr): fill_type_str = request.param xFill_bldr = self._xFill_bldr(fill_type_str) if xFill_bldr is not None: xPr_bldr.with_child(xFill_bldr) xPr = xPr_bldr.element fill_format = FillFormat.from_fill_parent(xPr) xPr_with_solidFill_xml = ( xPr_bldr.clear() .with_nsdecls() .with_child(a_solidFill()) .xml() ) return fill_format, xPr_with_solidFill_xml
def gradient_fixture(self, request, grad_fill_): cxml, expected_cxml = request.param fill = FillFormat.from_fill_parent(element(cxml)) # --mock must be after FillFormat call to avoid poss. contructor call _GradFill_ = class_mock( request, "pptx.dml.fill._GradFill", return_value=grad_fill_, autospec=True ) expected_xml = xml( expected_cxml + "{rotWithShape=1}/(a:gsLst/(a:gs{pos=0}/a:scheme" "Clr{val=accent1}/(a:tint{val=100000},a:shade{val=100000},a:satM" "od{val=130000}),a:gs{pos=100000}/a:schemeClr{val=accent1}/(a:ti" "nt{val=50000},a:shade{val=100000},a:satMod{val=350000})),a:lin{" "scaled=0})" ) return fill, _GradFill_, expected_xml, grad_fill_
def gradient_fixture(self, request, grad_fill_): cxml, expected_cxml = request.param fill = FillFormat.from_fill_parent(element(cxml)) # --mock must be after FillFormat call to avoid poss. contructor call _GradFill_ = class_mock(request, "pptx.dml.fill._GradFill", return_value=grad_fill_, autospec=True) expected_xml = xml( expected_cxml + "{rotWithShape=1}/(a:gsLst/(a:gs{pos=0}/a:scheme" "Clr{val=accent1}/(a:tint{val=100000},a:shade{val=100000},a:satM" "od{val=130000}),a:gs{pos=100000}/a:schemeClr{val=accent1}/(a:ti" "nt{val=50000},a:shade{val=100000},a:satMod{val=350000})),a:lin{" "scaled=0})") return fill, _GradFill_, expected_xml, grad_fill_
def fore_color_raise_fixture_(self, request, xPr_bldr): mapping = { 'none': TypeError, 'blip': TypeError, 'grad': NotImplementedError, 'grp': TypeError, 'no': TypeError, 'patt': NotImplementedError, } fill_type_str = request.param exception_type = mapping[fill_type_str] xFill_bldr = self._xFill_bldr(fill_type_str) if xFill_bldr is not None: xPr_bldr.with_child(xFill_bldr) xPr = xPr_bldr.element fill_format = FillFormat.from_fill_parent(xPr) return fill_format, exception_type
def fill_type_fixture_(self, request, xPr_bldr): mapping = { 'none': None, 'blip': MSO_FILL.PICTURE, 'grad': MSO_FILL.GRADIENT, 'grp': MSO_FILL.GROUP, 'no': MSO_FILL.BACKGROUND, 'patt': MSO_FILL.PATTERNED, 'solid': MSO_FILL.SOLID, } fill_type_str = request.param fill_type = mapping[fill_type_str] xFill_bldr = self._xFill_bldr(fill_type_str) if xFill_bldr is not None: xPr_bldr.with_child(xFill_bldr) xPr = xPr_bldr.element fill_format = FillFormat.from_fill_parent(xPr) return fill_format, fill_type
def back_color_fixture(self, fill_, color_): fill_.back_color = color_ fill_format = FillFormat(None, fill_) return fill_format, color_
def pattern_set_fixture(self, fill_): pattern = MSO_PATTERN.DIVOT fill = FillFormat(None, fill_) return fill, pattern
def pattern_get_fixture(self, fill_): expected_value = fill_.pattern = MSO_PATTERN.WAVE fill = FillFormat(None, fill_) return fill, expected_value
def fill_type_fixture(self, fill_): expected_value = fill_.type = 42 fill = FillFormat(None, fill_) return fill, expected_value
def fill(self): """ |FillFormat| instance for this font, providing access to fill properties such as fill color. """ return FillFormat.from_fill_parent(self._rPr)
def fill(self): """ |FillFormat| instance for this shape, providing access to fill properties such as fill color. """ return FillFormat.from_fill_parent(self._sp.spPr)