MantisIDE

Lesson 2: Styles and colours · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Lesson 2 - Styles: colours, gradients, radius, shadows Goal: make things look good. A style is a reusable bundle of properties you add to any object, like a CSS class.

You’ll learn: lv.style_t, gradients, shadows, states

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 1 · Foundations. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Lesson 2 - Styles: colours, gradients, radius, shadows
# Goal: make things look good. A style is a reusable bundle of properties you add
# to any object, like a CSS class.
import math

import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
TEXT = 0xE3EFE6
MUTED = 0x8EA999
keep = []                             # styles, buffers and callbacks LVGL uses must stay referenced

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb


def dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
    """A filled circle: a plain lv.obj with every theme style removed."""
    o = lv.obj(parent)
    o.remove_style_all()
    size = max(1, int(size))
    o.set_size(size, size)
    o.set_style_radius(lv.RADIUS_CIRCLE, 0)
    o.set_style_bg_color(lv.color_hex(color), 0)
    o.set_style_bg_opa(lv.OPA.COVER, 0)
    o.remove_flag(lv.obj.FLAG.CLICKABLE)
    o.align(align, int(x), int(y))
    return o


def polar(radius, degrees):
    """(x, y) from the centre for a point on a circle; 0 deg = 12 o'clock, clockwise."""
    r = math.radians(degrees - 90)
    return int(radius * math.cos(r)), int(radius * math.sin(r))

scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

# A reusable "card" style
card = lv.style_t()
card.init()
card.set_radius(24)
card.set_bg_color(lv.color_hex(0x1B3A28))
card.set_bg_grad_color(lv.color_hex(0x0C1A12))
card.set_bg_grad_dir(lv.GRAD_DIR.VER)          # vertical gradient
card.set_border_width(1)
card.set_border_color(lv.color_hex(0x46E07C))
card.set_shadow_width(24)
card.set_shadow_color(lv.color_hex(0x46E07C))
card.set_shadow_opa(lv.OPA._40)
card.set_pad_all(10)

box = lv.obj(scr)
box.add_style(card, 0)
box.set_size(150, 90)
box.center()
box.remove_flag(lv.obj.FLAG.SCROLLABLE)
label(box, "Styled card", 0xFFFFFF, 16, y=-12)
label(box, "gradient + glow", MUTED, 12, y=14)

# A pressed state: the same object restyles itself while you touch it
box.set_style_bg_color(lv.color_hex(0x46E07C), lv.STATE.PRESSED)
box.set_style_transform_scale(240, lv.STATE.PRESSED)   # 256 = 100 %

# Colour swatches around the edge using LVGL's palette
palette = [lv.PALETTE.RED, lv.PALETTE.ORANGE, lv.PALETTE.AMBER, lv.PALETTE.GREEN,
           lv.PALETTE.TEAL, lv.PALETTE.BLUE, lv.PALETTE.INDIGO, lv.PALETTE.PINK]
for i, p in enumerate(palette):
    x, y = polar(95, i * 45)
    d = dot(scr, 22, 0, x, y)
    d.set_style_bg_color(lv.palette_main(p), 0)

# Keep the style object alive while it is in use
keep.append(card)
print("Press the card and hold it: the PRESSED state changes its colour and size.")
Loading post…

Dialog