Skip to content

Commit 7de3a5a

Browse files
Merge pull request #2 from jposada202020/adding_example
adding_example
2 parents c4b2d8e + d342de3 commit 7de3a5a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

examples/colorfader_example.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-FileCopyrightText: 2023 Jose D. Montoya
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This is an animation to demonstrate the use of color_fader function.
6+
This example is heavily based in adafruit_display_shapes/examples/display_shapes_circle_animation.py
7+
8+
"""
9+
10+
import time
11+
import gc
12+
import board
13+
import displayio
14+
from adafruit_display_shapes.circle import Circle
15+
from cedargrove_colorfader import color_fader
16+
17+
display = board.DISPLAY
18+
main_group = displayio.Group()
19+
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
20+
color_palette = displayio.Palette(1)
21+
color_palette[0] = 0xFFFFFF
22+
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
23+
main_group.append(bg_sprite)
24+
25+
posx = 0
26+
posy = 0
27+
28+
# Define Circle characteristics
29+
circle_radius = 20
30+
circle = Circle(posx, posy, circle_radius, fill=0x00FF00, outline=0xFF00FF)
31+
main_group.append(circle)
32+
33+
# Define Circle Animation Steps
34+
delta_x = 2
35+
delta_y = 2
36+
37+
# Showing the items on the screen
38+
display.show(main_group)
39+
40+
circle.fill = 0xFF0000
41+
brightness = 0
42+
43+
bright_delta = 1 / display.width
44+
while True:
45+
if circle.y + circle_radius >= display.height - circle_radius:
46+
delta_y = -1
47+
if circle.x + circle_radius >= display.width - circle_radius:
48+
delta_x = -1
49+
if circle.x - circle_radius <= 0 - circle_radius:
50+
delta_x = 1
51+
if circle.y - circle_radius <= 0 - circle_radius:
52+
delta_y = 1
53+
54+
circle.x = circle.x + delta_x
55+
circle.y = circle.y + delta_y
56+
brightness = brightness + bright_delta * delta_x
57+
# pylint: disable=consider-using-max-builtin, consider-using-min-builtin
58+
if brightness > 1:
59+
brightness = 1
60+
if brightness < 0:
61+
brightness = 0
62+
circle.fill = color_fader(source_color=0x00FF00, brightness=brightness, gamma=1.0)
63+
time.sleep(0.02)
64+
gc.collect()

0 commit comments

Comments
 (0)