Draw Direct Shape Area Gradient

The ooodev.format.draw.direct.area.Gradient class is used to modify the values seen in Fig. 836 of a shape.

Setup

from __future__ import annotations
import uno
from ooodev.format.draw.direct.area import Gradient as ShapeGradient
from ooodev.format.draw.direct.area import PresetGradientKind
from ooodev.office.draw import Draw
from ooodev.gui import GUI
from ooodev.loader.lo import Lo


def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = Draw.create_draw_doc()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        GUI.zoom(GUI.ZoomEnum.ZOOM_75_PERCENT)

        slide = Draw.get_slide(doc)

        width = 36
        height = 36
        x = int(width / 2)
        y = int(height / 2) + 20

        rec = Draw.draw_rectangle(slide=slide, x=x, y=y, width=width, height=height)
        style = ShapeGradient.from_preset(preset=PresetGradientKind.DEEP_OCEAN)
        style.apply(rec)

        f_style = ShapeGradient.from_obj(rec)
        assert f_style.prop_inner == style.prop_inner

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


if __name__ == "__main__":
    raise SystemExit(main())
Area Gradient dialog

Fig. 836 Area Gradient dialog

Add a gradient to the shape

Adding a gradient to the shape is done by using the ShapeGradient class. The ShapeGradient class has a method from_preset() takes a preset as a parameter. The PresetGradientKind class is used to get the preset of the gradient.

from ooodev.format.draw.direct.area import Gradient as ShapeGradient
from ooodev.format.draw.direct.area import PresetGradientKind
# ... other code

rec = Draw.draw_rectangle(slide=slide, x=x, y=y, width=width, height=height)
style = ShapeGradient.from_preset(preset=PresetGradientKind.DEEP_OCEAN)
style.apply(rec)

The results of the setting the shape gradient can be seen in Fig. 837.

Shape with gradient

Fig. 837 Shape with gradient

Get Shape Gradient

We can get the gradient of the shape by using the ShapeGradient.from_obj() method.

from ooodev.format.draw.direct.area import Gradient as ShapeGradient
# ... other code

# get the gradient from the shape
f_style = ShapeGradient.from_obj(rec)
# assert the color is the same
assert f_style.prop_inner == style.prop_inner