HSV to RGB conversion

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

  1. Ashley

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    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;
        case hQuadrant of
          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
    #1
    TravF likes this.
  2. Ashley

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    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
    #2
  3. Ashley

    eha002

    Joined:
    Jun 29, 2012
    Messages:
    9
    Likes Received:
    0
    Location:
    Alstonville
    Any Idea How you would go about running this on a SHAC?
     
    eha002, Mar 13, 2020
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.