Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created August 24, 2024 16:33
Show Gist options
  • Save Cheaterman/8484a7f9864bb7934bd5fe219e52776d to your computer and use it in GitHub Desktop.
Save Cheaterman/8484a7f9864bb7934bd5fe219e52776d to your computer and use it in GitHub Desktop.
A radial progress bar with a gradient!
from kivy.animation import Animation
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import AliasProperty, ColorProperty, NumericProperty
from kivy.uix.effectwidget import AdvancedEffectBase, EffectWidget
class TangentGradient(AdvancedEffectBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.glsl = '''
uniform vec4 start_color;
uniform vec4 end_color;
float pi = 3.141592653589793;
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
{
float alpha = (.33 * color.r + .5 * color.g + .17 * color.b) * color.a;
return vec4(mix(start_color, end_color, atan(
tex_coords.x * 2.0 - 1.0,
tex_coords.y * 2.0 - 1.0
) / (2. * pi) + .5).rgb, alpha);
}
'''
class RadialProgressBar(EffectWidget):
progress = NumericProperty()
start_color = ColorProperty()
end_color = ColorProperty()
bar_width = NumericProperty(25)
def _get_uniforms(self):
return {
'start_color': tuple(float(rgba) for rgba in self.start_color),
'end_color': tuple(float(rgba) for rgba in self.end_color),
}
uniforms = AliasProperty(_get_uniforms, bind=['start_color', 'end_color'])
def __init__(self, **kwargs):
super().__init__(**kwargs)
gradient = TangentGradient(uniforms=self.uniforms)
self.bind(uniforms=gradient.setter('uniforms'))
self.effects = [gradient]
KV = '''
RadialProgressBar:
progress: app.progress
start_color: 0, 1, 0, 1
end_color: 0, 0, 1, 1
Label:
text: 'Radial progress:'
font_size: 64
<RadialProgressBar>:
Widget:
pos: root.pos
size: root.size
canvas:
Color:
Line:
circle:
[
*root.center,
min(root.size) / 2 - root.bar_width,
-180,
360 * root.progress - 180,
]
width: root.bar_width
cap: 'none'
'''
class Test(App):
progress = NumericProperty()
def build(self):
animation = (
Animation(progress=1, t='out_expo')
+ Animation(progress=0, t='out_expo')
)
animation.repeat = True
animation.start(self)
return Builder.load_string(KV)
Test().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment