# HSV to RGB conversion

Discussion in 'Pascal Logic Code Examples' started by Ashley, Aug 21, 2012.

1. ### Ashley

Joined:
Dec 1, 2005
Messages:
1,021
80
Location:
RGB led light are becoming very popular, but controlling them by Red, Green and Blue groups is not very intuitive. This logic allows the use of the HSV (Hue, Saturation and Value) colour model to control RGB lights. Hue sets the actual colour, Saturation is the amout of white (i.e. colour intensity), and Value is the overall light level.

On a touch screen you can use 3 sliders to set the SystemIO values. On a Wiser you're stuck with the SystemIO widget, or with minor mods to the HSV modeul you could use 3 phantom groups and a Dimmer widget. In this case you would need to scale the Hue from 0-255 up to 0-359.

Code:
```{SystemIO's}
Hue: Integer, Min:0, Max:359
Saturation: Integer, Min:0, Max:255
Value: Integer, Min:0, Max:255

{Global Variables}
hue, saturation, value: integer;
red, green, blue: integer;

{Procedure : hsv_to_rgb}
//
// Convert HSV colour space to RGB and set groups
// H = hue. 0-359 degrees
// S = Saturation. 0-255
// V = Value. 0-255
// R, G, B return values 0-255
//
procedure hsv_to_rgb(h,s,v: integer; var r: integer; var g: integer; var b: integer);
var
hQuadrant, hOffset, vs, p, q, t: integer;
begin
if s = 0 then // monochrome
begin
r := v;
g := v;
b := v;
end
else
begin
hQuadrant := h div 60;     // Hue quadrant 0 - 5 (60deg)
hOffset := h mod 60;     // Hue position in quadrant
vs := v * s;
p := v - (vs div 255);
q := v - (vs div 255 * hOffset) div 60;
t := v - (vs div 255 * (60 - hOffset)) div 60;
0:  begin r := v; g := t; b := p; end;
1:  begin r := q; g := v; b := p; end;
2:  begin r := p; g := v; b := t; end;
3:  begin r := p; g := q; b := v; end;
4:  begin r := t; g := p; b := v; end;
5:  begin r := v; g := p; b := q; end;
end;
end;
end;

{Modules: HSV}
//
// Look for HSV slider changes and set RGb outputs
// Uses 3 System IO's: Hue: 0-359, Saturation: 0-255, Value: 0-255
//
hue := GetIntSystemIO("Hue");
saturation := GetIntSystemIO("Saturation");
value := GetIntSystemIO("Value");
if hasChanged(hue) or hasChanged(Saturation) or hasChanged(Value) then
begin
hsv_to_rgb(hue, saturation, value, red, green, blue);
SetCBusLevel("Local", "Theatre Leds", "Led 0/RGB 0R", red, "0s");
SetCBusLevel("Local", "Theatre Leds", "Led 1/RGB 0G", green, "0s");
SetCBusLevel("Local", "Theatre Leds", "Led 2/RGB 0B", blue, "0s");
end;```

Last edited by a moderator: Jun 20, 2014
Ashley, Aug 21, 2012
TravF likes this.

2. ### Ashley

Joined:
Dec 1, 2005
Messages:
1,021
80
Location:
I am posting this as a reply to a private message I received which I could not reply to and the e-mail address supplied bounced back The question was regarding changing the main HSV module to control different group addresses of a DMX system. The revised code is as follows:

Code:
```{Modules: HSV}
//
// Look for HSV slider changes and set RGb outputs // Uses 3 System IO's: Hue: 0-359, Saturation: 0-255, Value: 0-255
//
hue := GetIntSystemIO("Hue");
saturation := GetIntSystemIO("Saturation");
value := GetIntSystemIO("Value");
if hasChanged(hue) or hasChanged(Saturation) or hasChanged(Value) then
begin
hsv_to_rgb(hue, saturation, value, red, green, blue);
SetLightingLevel("DMX Red", red, "0s");
SetLightingLevel("DMX Green", green, "0s");
SetLightingLevel("DMX Blue", blue, "0s");
end;
```

Ashley, Mar 21, 2017

3. Joined:
Jun 29, 2012
Messages:
9