VisualHMI text color control: on_wgt_text_color batch theme switching

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Text color control (exclusive for customized firmware) providesa global text color batch setting interface: a callback function that dynamically changes the text foreground color of all text-based controls on the screen, eliminating the need for individual configuration. A typical scenario is switching between day/night modes - dark text on a bright background, and light text on a dark background, with a single variable controlling the global theme.

1. Controls that support batch setting

Not all controls can be colored by this interface; only controls withprogrammable text color/foreground color attributesparticipate:

Control typeDescription
Static textMost direct target object
Bit status indicator/multi-state indicatorText changes color based on theme
Position setting button / Text setting button / Function buttonButton text
Drop-down selection box / Scroll wheel selectorOption text
RTCTime display

VisualHMI text color control: on_wgt_text_color batch theme switching插图

2. Pre-configuration (two steps, both required)

Step 1: Globally enable the control grouping function.Path: Project Properties → Control Grouping → Enable. If not enabled, all controls will ignore grouping-related properties during runtime - even if you have assigned a grouping ID.

Step 2: Assign a valid grouping identifier to the control.For each control to be included in color management, set theControl ID in the control property panelmust not be equal to 0 (0 is the default invalid value, indicating "Ungrouped"). In the on_wgt_text_color callback, this ID is used to distinguish controls.

3. API: on_wgt_text_color(screen, control)

Dynamic callback for control text color. The system calls it when refreshing controls, and uses the return value as the text color for that control.

ParametersTypeDescription
screennumberCurrent screen ID, used to distinguish controls with the same name across multiple pages
controlnumberTarget control ID.The callback is only triggered when control is not equal to 0

Return value:color(number), a 16-bit color value in RGB565 format. The entire color theme logic is contained within this function.

4. Application case: Switching themes for multi-state lights

Screen Layout: Amulti-state indicator lightserves as a color toggle switch (states 0~4 correspond to 5 thematic colors), with the screen ID set to 0; below, a batch of controls supporting text coloring (text, buttons, indicator lights, dropdown boxes, scroll wheels, RTC) are placed, with their control IDs uniformly set to 5.

In the callback, Lua reads the value of register LW3000 as the color index and returns the corresponding RGB565 color value:

function on_wgt_text_color(screen, control)
  local color = 0x001f           -- 默认蓝色
  if screen == 0 then
    if control == 5 then
      local val = get_uint16(VT_LW, 0x3000)  -- 颜色索引
      if val == 0 then
        color = 0x00FF           -- 蓝
      elseif val == 1 then
        color = 0x0562           -- 绿
      elseif val == 2 then
        color = 0x0429           -- 深绿
      elseif val == 3 then
        color = 0x0177           -- 青
      elseif val == 4 then
        color = 0xF800           -- 红
      end
    end
  end
  print(string.format("on_wgt_text_color(%d,%d,%04X)", screen, control, color))
  return color
end

When running the virtual screen and clicking the color toggle control, the text color of all UI elements with control ID 5 is updated synchronously - this is the effect of "one-click theme switching".

Three pitfalls:① Control grouping must be enabled in the project properties, otherwise the assigned ID will not take effect;② The control ID cannot be 0, as 0 indicates not grouped and does not participate in the callback;③ The returned value is an RGB565 color value(high 5 bits for R/middle 6 bits for G/low 5 bits for B), using the same color gamut as the set_pen_color and text color attributes, so it is sufficient to directly copy the color constants from the drawing tool. This interface is only available in customized firmware, not in regular firmware.

Compiled from the VisualHMI development documentation of Guangzhou Dacai Technology (hmi-doc.gz-dc.com), the LUA tutorial "Text Color Control", copyright reserved by Dacai Technology.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.