Colours
TapHome script colour functions — HSVTORGB and RGBTOHSV — and the rgbcolor and hsvcolor value types used to control RGB lights from Packet parser and Modbus scripts.
Colour functions convert between the HSV model — hue, saturation and value (brightness) — and the RGB model of red, green and blue channels. A TapHome colour light works with hue and saturation; many devices driven from Packet parser or Modbus scripts expect red, green and blue instead. Both functions work in every script and need Core 2023.1 or newer.
HSVTORGB
HSVTORGB converts a colour given by hue, saturation and value into an rgbcolor value with red, green and blue channels from 0 to 255.
| |
| Name | Type | Description |
|---|---|---|
hue | number | Colour angle in degrees, 0 – 360 (0 red, 120 green, 240 blue). |
saturation | number | 0 – 1. 0 is white or grey, 1 the full colour. |
value | number | Brightness, 0 – 1. |
Returns — rgbcolor with the properties RED, GREEN and BLUE, each rounded to a whole number 0 – 255.
Remarks — The inputs are not limited to their range. Keep saturation and value between 0 and 1, otherwise the channels overflow the 0 – 255 range.
Example
VAR green := HSVTORGB(120, 1, 1);
green.GREEN;
# Result: 255Example — write script of an RGB light over HTTP
In the write script of a colour light, pass the light’s hue (Hd) and saturation (Sa) values and keep the brightness separate. Here they are local variables so the example runs on its own:
VAR hue := 30;
VAR saturation := 1;
VAR color := HSVTORGB(hue, saturation, 1);
"red=" + color.RED + "&green=" + color.GREEN + "&blue=" + color.BLUE;
# Result: "red=255&green=128&blue=0"See also — RGBTOHSV, SENDHTTPREQUEST.
RGBTOHSV
RGBTOHSV converts red, green and blue channels (0 – 255) into an hsvcolor value with hue in degrees and saturation and value from 0 to 1.
| |
| Name | Type | Description |
|---|---|---|
red, green, blue | number | 0 – 255. Each is converted to a whole number in that range. |
Returns — hsvcolor with HUE (0 – 360, with a fraction), SATURATION (0 – 1) and VALUE (0 – 1). For grey colours, where all channels are equal, HUE and SATURATION are 0.
Example — read the hue of a light that reports red, green and blue
VAR hsv := RGBTOHSV(255, 128, 0);
ROUND(hsv.HUE);
# Result: 30VAR hsv := RGBTOHSV(255, 128, 0);
ROUND(hsv.SATURATION * 100) / 100;
# Result: 1rgbcolor and hsvcolor values
rgbcolor and hsvcolor are the value types returned by the colour functions; their properties can be read and written.
| Type | Property | Range | Note |
|---|---|---|---|
rgbcolor | RED, GREEN, BLUE | 0 – 255 | Writing stores a byte: values above 255 wrap around. |
hsvcolor | HUE | 0 – 360 | Writing stores a byte, so a hue above 255 wraps around — hsv.HUE := 300 gives 44. Pass the hue to HSVTORGB instead. |
hsvcolor | SATURATION, VALUE | 0 – 1 | Decimal numbers. |
Both type names can declare a variable:
rgbcolor colour := HSVTORGB(240, 1, 0.5);
colour.BLUE;
# Result: 128See also — Data types, Variables and constants.