HP Forums

Full Version: HSV "Color Space Function"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HSV Color Space Function
HSV. The HSV (Hue, Saturation, Value) color space corresponds better to how people experience color than the RGB color space does. For example, this color space is often used by people who are selecting colors, such as paint or ink color, from a color wheel or palette.

PPL
Code:
EXPORT HSV(h,s,v)
BEGIN
h MOD 360/60▶h;
MIN(MAX(s,0),100)/100▶s;
MIN(MAX(v,0),100)/100▶v;
LOCAL f,p,q,t,m;
h-FLOOR(h)▶f;
v*(1-s)▶p;
v*(1-s*f)▶q;
v*(1-s*(1-f))▶t;
LOCAL r,g,b;
FLOOR(h)▶m;
IF m=0 THEN v▶r;t▶g;p▶b;END;
IF m=1 THEN q▶r;v▶g;p▶b;END;
IF m=2 THEN p▶r;v▶g;t▶b;END;
IF m=3 THEN p▶r;q▶g;v▶b;END;
IF m=4 THEN t▶r;p▶g;v▶b;END;
IF m=5 THEN v▶r;p▶g;q▶b;END;
RETURN RGB(r*255,g*255,b*255);
END;

P+ Source Code
Code:
#pragma ( minify -1, reduce, newline )

#include <prime>

export HSV(h:hue, s:saturation, v:value)
begin
    hue = hue % 360 / 60;
    saturation = MIN(MAX(saturation, 0), 100) / 100;
    value = MIN(MAX(value, 0), 100) / 100;
    
    var f, p, q, t, m;
    f = hue - math::floor(h);
    p = value * (1 - saturation);
    q = value * (1 - saturation * f);
    t = value * (1 - saturation * (1 - f));

    var r:red, g:green, b:blue;
    m = math::floor(h);
    
    if m==0 then red = value; green = t; blue = p; end;
    if m==1 then red = q; green = value; blue = p; end;
    if m==2 then red = p; green = value; blue = t; end;
    if m==3 then red = p; green = q; blue = value; end;
    if m==4 then red = t; green = p; blue = value; end;
    if m==5 then red = value; green = p; blue = q; end;
  
    return RGB(red * 255, green * 255, blue * 255);
end

P+
GitHub
Reference URL's