Gradients

bouquet.gradients module provides various types of gradients. Currently, only linear (LinearGradient) and radial (RadialGradient) gradients are supported. Other types of gradients will be added later.

How to use gradients with Vertex Instructions?

This is possible to use gradient textures with Vertex Instructions via the render_texture() function. You can take a look at code examples in the Examples section below.

Examples

LinearGradient:

from kivy.app import App
from bouquet.gradients import LinearGradient

class MyApp(App):

    def build(self):
        return LinearGradient(
            top_left_color='purple',
            top_right_color='purple',
            bottom_left_color='orange',
            bottom_right_color='orange',
        )

MyApp().run()

RadialGradient:

from kivy.app import App
from kivy.lang import Builder

import bouquet.gradients

KV = """
RadialGradient:
    center_color: (1.0, 0.0, 0.0, 1.0)
    border_color: (1.0, 0.0, 0.0, 0.0)
"""

class MyApp(App):

    def build(self):
        return Builder.load_string(KV)

 MyApp().run()

LinearGradient.render_texture():

from kivy.app import App
from kivy.lang import Builder

KV = """
#: import LinearGradient bouquet.gradients.LinearGradient
Widget:
    canvas:
        Color:
            rgb: 1.0, 1.0, 1.0
        Ellipse:
            pos: self.pos
            size: self.size
            texture: LinearGradient.render_texture(size=self.size)
"""

class MyApp(App):

    def build(self):
        return Builder.load_string(KV)

MyApp().run()

RadialGradient.render_texture():

from kivy.app import App
from kivy.graphics import Ellipse
from kivy.uix.widget import Widget

from bouquet.gradients import RadialGradient

class EllipseWithRadialGradient(Widget):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.center_color = "#ff0000"   # red
        self.border_color = "#ffff00"   # yellow
        with self.canvas:
            self.ellipse = Ellipse(
                pos=self.pos,
                size=self.size,
                texture=RadialGradient.render_texture(
                    size=self.size,
                    center_color=self.center_color,
                    border_color=self.border_color
                )
            )

    def on_size(self, widget, size):
        self.ellipse.size = size
        self.ellipse.texture = RadialGradient.render_texture(
            size=self.size,
            center_color=self.center_color,
            border_color=self.border_color
        )

class MyApp(App):

    def build(self):
        return EllipseWithRadialGradient()

MyApp().run()
class bouquet.gradients.LinearGradient(**kwargs)

Bases: AnchorLayout

Widget for creating a linear gradient background with customizable colors for each corner.

Hint

LinearGradient is an AnchorLayout subclass, so you can put any widget inside it.

bottom_left_color

Color of the top bottom corner of gradient.

bottom_left_color is an ColorProperty and defaults to black.

bottom_right_color

Color of the bottom right corner of gradient.

bottom_right_color is an ColorProperty and defaults to red.

static render_texture(**kwargs) Texture

Renders gradient at FBO and returns the texture.

Parameters:

kwargs – Any LinearGradient properties.

top_left_color

Color of the top left corner of gradient.

top_left_color is an ColorProperty and defaults to green.

top_right_color

Color of the top right corner of gradient.

top_right_color is an ColorProperty and defaults to yellow.

class bouquet.gradients.RadialGradient(**kwargs)

Bases: Widget

Widget for creating a radial gradient effect, in which color is linearly interpolated from the center to the edges of the widget.

Note

Currently, it is possible to set only two colors: the color of the center (center_color) and the color of the border (border_color).

border_color

Color of the widget borders.

border_color is an ColorProperty and defaults to black.

center_color

Color of the widget center.

center_color is an ColorProperty and defaults to white.

static render_texture(**kwargs) Texture

Renders gradient at FBO and returns the texture.

Parameters:

kwargs – Any RadialGradient properties.