Skip to content

Instantly share code, notes, and snippets.

@Poikilos
Forked from tshirtman/radial_gradient.py
Last active May 16, 2023 18:59
Show Gist options
  • Save Poikilos/83eabcb137690498c05c7293247e0d82 to your computer and use it in GitHub Desktop.
Save Poikilos/83eabcb137690498c05c7293247e0d82 to your computer and use it in GitHub Desktop.
radial gradient texture for kivy - Python 3 version (using bytearray)
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
from kivy.uix.widget import Widget
from kivy.app import App
class RadialGradient(App):
def build(self):
w = Widget()
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 = bytearray()
for x in range(-int(sx_2), int(sx_2)):
for y in range(-int(sy_2), int(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.append(max(0,
min(255,
int(center_color[c] * (1 - d)) +
int(border_color[c] * d))))
tex.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
with w.canvas:
Rectangle(texture=tex, size=(500, 500))
return w
RadialGradient().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment