'============================================================
'------------- Функции преобразования цветов------------------
'============================================================

function decColor2rgb(dec as Integer) as Vertex
'Переводим из десятичного числа в формат RGB
	Dim r as Integer = dec mod 256
	Dim g as Integer = (dec/256) mod 256
	Dim b as Integer = (dec/256/256) mod 256
	decColor2rgb = Cvertex(r/255.0,g/255.0,b/255.0)
end function

function hsv2rgb(HSV as Vertex) as Vertex
'HSV и RGB значения 0..1
	Dim h = HSV.x
	Dim s = HSV.y
	Dim v = HSV.z
	
	
	if  s = 0 then 
		hsv2rgb = Cvertex(v)
	else
  
	    Dim var_h = h * 6
	    Dim var_i = floor( var_h )
	    DIm var_1 = v * ( 1 - s )
	    Dim var_2 = v * ( 1 - s * ( var_h - var_i ) )
	    Dim var_3 = v * ( 1 - s * ( 1 - ( var_h - var_i ) ) )
	 
	    if var_i = 0 then 
			hsv2rgb = Cvertex(v, var_3, var_1)
		elseif var_i = 1 then
			hsv2rgb = Cvertex(var_2, v, var_1)
		elseif var_i = 2 then
			hsv2rgb = Cvertex(var_1, v, var_3)
		elseif var_i= 3 then
			hsv2rgb = Cvertex(var_1, var_2, v)
		elseif var_i == 4 then
			hsv2rgb = Cvertex(var_3, var_1, v)
		else    
			hsv2rgb = Cvertex(v, var_1, var_2)
		end if
	end if
end function


function rgb2hsv(RGB as Vertex) as Vertex
'HSV и RGB значения 0..1
	Dim r as Double = RGB.x
	Dim g as Double = RGB.y
	Dim b as Double = RGB.z
	
	Dim h,s,v as double
    Dim mx as Double = max(r, g)
	mx = max(mx, b)
    Dim mn as Double = min(r, g)
	mn = min (mn, b)
    Dim df as Double = mx-mn

    if mx = mn then
        h = 0
    elseif mx = r and g>=B then
		h = (60 * ((g-b)/df) + 0)
	elseif mx = r and g<B then
		h = (60 * ((g-b)/df) + 360.0)
    elseif mx = g then
        h = (60 * ((b-r)/df) + 120.0)
    elseif mx = b then
        h = (60 * ((r-g)/df) + 240.0)
	end if
	
    if mx = 0 then
        s = 0
    else
        s = df/mx
	end if
    v = mx
    rgb2hsv = Cvertex(h/360.0,s,v)
end function