forked from tstriker/hamster-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop_shadow.py
executable file
·158 lines (105 loc) · 4.46 KB
/
drop_shadow.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauģis <toms.baugis at gmail.com>
"""Base template"""
import gtk
from lib import graphics
import cairo
import struct
class DropShadow(graphics.Sprite):
def __init__(self, sprite):
graphics.Sprite.__init__(self)
self.original_sprite = sprite
self.us = False
#self.original_sprite.connect("on-render", self.on_prev_sprite_render)
def render(self):
if self.us:
return
if self.original_sprite.graphics.extents:
x, y, x2, y2 = self.original_sprite.graphics.extents
print x, y, x2, y2
self.us = True
# first we will measure extents (lame)
image_surface = cairo.ImageSurface(cairo.FORMAT_A1, 0, 0)
image_context = gtk.gdk.CairoContext(cairo.Context(image_surface))
self.original_sprite._draw(image_context)
extents = self.original_sprite.get_extents()
width = int(extents[2] - extents[0]) + 10
height = int(extents[3] - extents[1]) + 10
# creating a in-memory image of current context
image_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
image_context = gtk.gdk.CairoContext(cairo.Context(image_surface))
self.original_sprite._draw(image_context)
self.us = False
# now create shadow
self.blur(image_surface)
self.graphics.clear()
self.graphics.set_source_surface(image_surface)
self.graphics.paint()
def blur(self, surface):
buffer = surface.get_data()
v = 1.0 / 9.0
kernel = ((v, v, v),
(v, v, v),
(v, v, v))
kernel_range = range(-1, 2) # surrounding the pixel
extents = self.original_sprite.get_extents()
width = int(extents[2] - extents[0]) + 10
height = int(extents[3] - extents[1]) + 10
# we will need all the pixel colors anyway, so let's grab them once
pixel_colors = struct.unpack_from('BBBB' * width * height, buffer)
new_pixels = [0] * width * height * 4 # target matrix
for x in range(1, width - 1):
for y in range(1, height - 1):
r,g,b,a = 0,0,0,0
pos = (y * width + x) * 4
for ky in kernel_range:
for kx in kernel_range:
k = kernel[kx][ky]
k_pos = pos + ky * width * 4 + kx * 4
pixel_r,pixel_g,pixel_b,pixel_a = pixel_colors[k_pos:k_pos + 4]
r += k * pixel_r
g += k * pixel_g
b += k * pixel_b
a += k * pixel_a
avg = min((r + g + b) / 3.0 * 0.6, 255)
new_pixels[pos:pos+4] = (avg, avg, avg, a)
struct.pack_into('BBBB' * width * height, buffer, 0, *new_pixels)
class SomeShape(graphics.Sprite):
def __init__(self):
graphics.Sprite.__init__(self, draggable = True)
#self.graphics.circle(25, 25, 15)
label = graphics.Label("Drag me around!", 24, "#fff")
self.add_child(label)
self.graphics.rectangle(0, 0, label.width, label.height)
self.graphics.new_path()
#self.graphics.fill_area(5, 5, 80, 80, "#fff")
self.connect("on-render", self.on_render)
def on_render(self, sprite):
self.shadow.render()
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.shape = SomeShape()
shadow = DropShadow(self.shape)
shadow.x, shadow.y = -4, -4
self.shape.shadow = shadow
self.add_child(shadow)
self.add_child(self.shape)
self.connect("on-drag", self.on_sprite_drag)
#self.animate(self.shape, opacity=1, duration=1.0)
self.redraw()
def on_sprite_drag(self, scene, sprite, event):
extents = sprite.get_extents()
width, height = extents[2] - extents[0], extents[3] - extents[1]
sprite.shadow.x = sprite.x + (sprite.x + (width - self.width) / 2.0) / float(self.width) * 8
sprite.shadow.y = sprite.y + (sprite.y + (height - self.height) / 2.0) / float(self.height) * 8
class BasicWindow:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 500)
window.connect("delete_event", lambda *args: gtk.main_quit())
window.add(Scene())
window.show_all()
example = BasicWindow()
gtk.main()