1#!/opt/bin/lv_micropython -i
2import usys as sys
3import lvgl as lv
4import display_driver
5from imagetools import get_png_info, open_png
6
7# Register PNG image decoder
8decoder = lv.img.decoder_create()
9decoder.info_cb = get_png_info
10decoder.open_cb = open_png
11
12# Create an image from the png file
13try:
14    with open('../../assets/img_cogwheel_argb.png','rb') as f:
15        png_data = f.read()
16except:
17    print("Could not find img_cogwheel_argb.png")
18    sys.exit()
19
20img_cogwheel_argb = lv.img_dsc_t({
21  'data_size': len(png_data),
22  'data': png_data
23})
24
25def create_slider(color):
26    slider = lv.slider(lv.scr_act())
27    slider.set_range(0, 255)
28    slider.set_size(10, 200)
29    slider.set_style_bg_color(color, lv.PART.KNOB)
30    slider.set_style_bg_color(color.color_darken(lv.OPA._40), lv.PART.INDICATOR)
31    slider.add_event_cb(slider_event_cb, lv.EVENT.VALUE_CHANGED, None)
32    return slider
33
34def slider_event_cb(e):
35    # Recolor the image based on the sliders' values
36    color  = lv.color_make(red_slider.get_value(), green_slider.get_value(), blue_slider.get_value())
37    intense = intense_slider.get_value()
38    img1.set_style_img_recolor_opa(intense, 0)
39    img1.set_style_img_recolor(color, 0)
40
41#
42# Demonstrate runtime image re-coloring
43#
44# Create 4 sliders to adjust RGB color and re-color intensity
45red_slider = create_slider(lv.palette_main(lv.PALETTE.RED))
46green_slider = create_slider(lv.palette_main(lv.PALETTE.GREEN))
47blue_slider = create_slider(lv.palette_main(lv.PALETTE.BLUE))
48intense_slider = create_slider(lv.palette_main(lv.PALETTE.GREY))
49
50red_slider.set_value(lv.OPA._20, lv.ANIM.OFF)
51green_slider.set_value(lv.OPA._90, lv.ANIM.OFF)
52blue_slider.set_value(lv.OPA._60, lv.ANIM.OFF)
53intense_slider.set_value(lv.OPA._50, lv.ANIM.OFF)
54
55red_slider.align(lv.ALIGN.LEFT_MID, 25, 0)
56green_slider.align_to(red_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
57blue_slider.align_to(green_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
58intense_slider.align_to(blue_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
59
60# Now create the actual image
61img1 = lv.img(lv.scr_act())
62img1.set_src(img_cogwheel_argb)
63img1.align(lv.ALIGN.RIGHT_MID, -20, 0)
64
65lv.event_send(intense_slider, lv.EVENT.VALUE_CHANGED, None)
66
67
68
69
70
71