Skip to content

Instantly share code, notes, and snippets.

@tito
Forked from tshirtman/radial_gradient.py
Created December 10, 2012 12:36
Show Gist options
  • Save tito/4250317 to your computer and use it in GitHub Desktop.
Save tito/4250317 to your computer and use it in GitHub Desktop.
radial gradient texture for kivy
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.clock import Clock
class RadialGradient(App):
def build(self):
Clock.schedule_once(self.create_tex, 1)
self.w = Widget()
return self.w
def create_tex(self, *args):
center_color = 255, 255, 0
border_color = 100, 0, 0
size = (64, 64)
tex = Texture.create(size=size)
sx_2 = size[0] / 2
sy_2 = size[1] / 2
buf = ''
for x in xrange(-sx_2, sx_2):
for y in xrange(-sy_2, sy_2):
a = x / (1.0 * sx_2)
b = y / (1.0 * sy_2)
d = (a ** 2 + b ** 2) ** .5
for c in (0, 1, 2):
buf += chr(max(0,
min(255,
int(center_color[c] * (1 - d)) +
int(border_color[c] * d))))
tex.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
with self.w.canvas:
Rectangle(texture=tex, size=(500, 500))
RadialGradient().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment