File: ui_gradient.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (91 lines) | stat: -rw-r--r-- 3,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import with_statement

import numpy as np

from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, View

class MyCanvas(Component):
    def draw(self, gc, **kwargs):
        w,h = gc.width(), gc.height()

        # colors are 5 doubles: offset, red, green, blue, alpha
        starting_color = np.array([0.0, 1.0, 1.0, 1.0, 1.0])
        ending_color = np.array([1.0, 0.0, 0.0, 0.0, 1.0])

        gc.clear()

        # radial reflected background
        with gc:
            gc.rect(0, 0, w, h)

            start = np.array([0.0, 1.0, 0.0, 0.0, 1.0])
            end = np.array([1.0, 1.0, 1.0, 1.0, 1.0])
            gc.radial_gradient(w/4, h/4, 200, w/4+100, h/4+100,
                               np.array([start, end]), 'reflect')
            gc.fill_path()

        # diagonal
        with gc:
            gc.rect(50,25,150,100)
            gc.linear_gradient(0,0,1,1,
                                np.array([starting_color, ending_color]),
                                "pad", 'objectBoundingBox')
            gc.fill_path()

        # vertical
        with gc:
            gc.rect(50,150,150,100)
            gc.linear_gradient(50,150,50,250,
                                np.array([starting_color, ending_color]),
                                "pad", 'userSpaceOnUse')
            gc.fill_path()

        # horizontal
        with gc:
            gc.rect(50,275,150,100)
            gc.linear_gradient(0,0,1,0,
                                np.array([starting_color, ending_color]),
                                "pad", 'objectBoundingBox')
            gc.fill_path()

        # radial
        with gc:
            gc.arc(325, 75, 50, 0.0, 2*np.pi)
            gc.radial_gradient(325, 75, 50, 325, 75,
                                np.array([starting_color, ending_color]),
                                "pad", 'userSpaceOnUse')
            gc.fill_path()

        # radial with focal point in upper left
        with gc:
            gc.arc(325, 200, 50, 0.0, 2*np.pi)
            gc.radial_gradient(0.5, 0.5, 0.5, 0.25, 0.75,
                            np.array([starting_color, ending_color]),
                            "pad", 'objectBoundingBox')
            gc.fill_path()

        # radial with focal point in bottom right
        with gc:
            gc.arc(325, 325, 50, 0.0, 2*np.pi)
            gc.radial_gradient(325, 325, 50, 350, 300,
                                np.array([starting_color, ending_color]),
                                "pad", 'userSpaceOnUse')
            gc.fill_path()

        return

class Demo(HasTraits):
    canvas = Instance(Component)

    traits_view = View(Item('canvas', editor=ComponentEditor(bgcolor="lightgray"),
                            show_label=False, width=500, height=500),
                       resizable=True, title="Gradient Example")

    def _canvas_default(self):
        return MyCanvas()


if __name__ == "__main__":
    Demo().configure_traits()