Skip to main content
How Can We Help?
< All Topics
Print

Change The RGB Color of an Object in Code using $rgb()

Conditional Color Assignment Using $rgb()

Purpose

In Zim, you can use conditional logic to dynamically change the color of form objects based on runtime conditions such as user input, system status, or validation results. This is achieved using the if, elseif, else, and endif control structures in combination with the $rgb function.


Syntax

 

if condition
    action
elseif condition
    action
else
    action
endif

 


Example: Status-Based Field Coloring

 

if $status = "error"
    form set (fillcolor rgb $rgb(255, 0, 0)) MyForm.MyFormField
elseif $status = "warning"
    form set (fillcolor rgb $rgb(255, 165, 0)) MyForm.MyFormField
else
    form set (fillcolor rgb $rgb(0, 255, 0)) MyForm.MyFormField
endif

 

Explanation

  • If the status is "error", the field is colored red.
  • If the status is "warning", the field is colored orange.
  • For all other statuses, the field is colored green.

Use Cases

  • Form validation feedback (e.g., highlight invalid fields).
  • User role–based UI customization (e.g., different colors for admin vs. guest).
  • System alerts and notifications (e.g., color-coded warnings).
  • Real-time sensor feedback (e.g., temperature thresholds triggering color changes).

Best Practices

  • Keep RGB values consistent with your UI design system.
  • Use descriptive variable names for conditions.
  • Avoid deeply nested conditionals—break logic into reusable procedures if needed.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

pt_BRPortuguese