Write Modify Draw Area Gradient

The ooodev.format.draw.modify.area.Gradient class is used to modify the values seen in Fig. 879 of a style.

Setup

from __future__ import annotations
import uno
from ooodev.draw import Draw, DrawDoc, ZoomKind
from ooodev.loader.lo import Lo
from ooodev.format.draw.modify.area import Gradient, PresetGradientKind
from ooodev.format.draw.modify import FamilyGraphics, DrawStyleFamilyKind


def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = DrawDoc(Draw.create_draw_doc())
        doc.set_visible()
        Lo.delay(500)
        doc.zoom(ZoomKind.ZOOM_75_PERCENT)

        slide = doc.get_slide()

        width = 100
        height = 50
        x = 10
        y = 10

        rect = slide.draw_rectangle(x=x, y=y, width=width, height=height)
        rect.set_string("Hello World!")
        style_modify = Gradient.from_preset(
            preset=PresetGradientKind.MAHOGANY,
            style_name=FamilyGraphics.DEFAULT_DRAWING_STYLE,
            style_family=DrawStyleFamilyKind.GRAPHICS,
        )
        doc.apply_styles(style_modify)

        Lo.delay(1_000)
        doc.close_doc()
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Apply gradient to a style

Before applying Style

Draw dialog Area Gradient style default

Fig. 879 Draw dialog Area Gradient style default

Apply style

The gradient can be loaded from a preset using the PresetGradientKind class as a lookup.

# ... other code

style_modify = Gradient.from_preset(
    preset=PresetGradientKind.MAHOGANY,
    style_name=FamilyGraphics.DEFAULT_DRAWING_STYLE,
    style_family=DrawStyleFamilyKind.GRAPHICS,
)
doc.apply_styles(style_modify)

After applying style

Dialog after applying style.

Draw dialog Area Gradient style changed

Fig. 880 Draw dialog Area Gradient style changed

Shape after applying style.

Shape after Style applied

Fig. 881 Shape after Style applied

Getting the area image from a style

We can get the area image from the document.

# ... other code
f_style = Gradient.from_style(
    doc=doc.component,
    style_name=FamilyGraphics.DEFAULT_DRAWING_STYLE,
    style_family=DrawStyleFamilyKind.GRAPHICS,
)
assert f_style is not None