(Howto) define a color : Menu, RGB and HSV

Share Tips, Code Samples, etc. with the Visual Prolog community.
Gildas Menier
VIP Member
Posts: 78
Joined: 8 Jun 2004 23:01

(Howto) define a color : Menu, RGB and HSV

Unread post by Gildas Menier » 18 Mar 2006 10:53

1. MENU : A very simple way to insert a color is the use of the VP menu item Insert->Insert RGB value : it opens a Color Dialog and let you insert the right color value. This is an easy way, since you see/choose the color to insert.

2. RGB : Second is the use of Res = vpi::composeRGB(R,G,B) : this predicates computes a color with the R,G,B values (note that R,G,B should be integers in [0,255]). Of course, you have to know the RVB value that should blend to the wished color.

3. HSV : Depending of what you want to acheive, a third way is sometimes more interesting than RGB : HSV. (See the following picture). Practically, H is the Hue or the 'real' color. 0 -> Red and then, increasing H, your color blends to Green (for H=120) then to blue (H=240) then back to Red (359.99999 ;) ). So if you want to have a yellow blending from red to green, H should vary between H = 30 and H = 90.

H should be in [0,360] and points to a color in the color circle : S stands for the saturation, or exactly how intense the color should be (between white and full satured color). S is in [0,1]. Same thing for V, or value, V is in [0,1].

Here is a simple code to get a color given H,S,V :

Code: Select all

class predicates     colorFromHSV:(real, real, real) -> color procedure (i,i,i). clauses     colorFromHSV(H,S,Val) = Res :-         int_hsv2rgb(H,S,Val, R,V,B),         Res = vpi::composeRGB(R,V,B)     .%       class predicates         int_hsv2rgb: (real, real, real, integer, integer, integer) procedure (i,i,i,o,o,o).     clauses         int_hsv2rgb(HH,SS,VV,R,V,B) :-             hsv2rgb(HH, SS, VV, RR, RV, RB),             R = math::trunc(255*RR),             V = math::trunc(255*RV),             B = math::trunc(255*RB)         .%         class predicates         hsv2rgb: (real, real, real, real, real, real) procedure (i,i,i,o,o,o).     clauses         hsv2rgb(_,0,Val,Val,Val,Val) :- !.         hsv2rgb(H,S,Val,R,V,B) :-         HV = H/60,         I = math::trunc(HV),         F = HV-I,         P = Val*(1-S),         Q = Val*(1-(S*F)),         T = Val*(1-S*(1-F)),         cvtTo(I,R,V,B, P, Q, T, Val)     .%       class predicates         cvtTo: (integer, real, real, real, real, real, real, real) procedure (i,o,o,o,i,i,i,i).     clauses         cvtTo(0, V,T,P  ,P,_Q,T,V ) :- !.         cvtTo(1, Q,V,P  ,P,Q,_T,V ) :- !.         cvtTo(2, P,V,T  ,P,_Q,T,V ) :- !.         cvtTo(3, P,Q,V  ,P,Q,_T,V ) :- !.         cvtTo(4, T,P,V  ,P,_Q,T,V ) :- !.         cvtTo(5, V,P,Q ,P,Q,_T,V ) :- !.         cvtTo(_, 1.0,1.0,1.0 ,_,_,_,_ ).
and should be used this way :

Color = colorFromHSV(H,S,V),

Regards

Gildas
Attachments
HSV.jpg
HSV.jpg (11.31 KiB) Viewed 5339 times

Post Reply