' ' =======================================================
' Librairie gestion couleur
' =======================================================
' Procedures publiques
label TSLtoRVB
label RVBtoTSL : ' pas encore...
label RVBtoTSV
label TSVtoRVB
' variables publiques
dim color_RVB(3), color_i
dim color_TSL(3) : ' teinte saturation luminosite
dim color_TSV(3)
' Procedures privÃ
es
label CalcCompo
' variables privÃ
es
dim color_TSL2RVB(5) : ' tr, tv, tb, p, q
dim color_Calc_p, color_Calc_q, color_Calc_tc, color_Calc_result
dim TsvByRgbCode(3) : ' 0:rouge, 1:vert, 2:bleu, 3:maxVal, 4:minVal
dim RgbByHsv(8) : ' 0:T 1:S 2:V 3:Hi 4:f 5:p 6:q 7:t
label lib_Color : goto lib_Color
' -------------------------------------------------------
' Conversion TSL (Teinte,Saturation,Luminosite) -> RVB
' -------------------------------------------------------
TSLtoRVB:
' Si la saturation est nulle, on a du gris, la teinte n'a pas d'importance
' on applique la valeur de luminosite de partout
if color_TSL(1) = 0
color_RVB(0) = color_TSL(2) * 255
color_RVB(1) = color_RVB(0)
color_RVB(2) = color_RVB(1)
else
' Ici on a deux cas : pour L<0.5 on assombrit
' et pour L>0.5 on Ã
claircit
if color_TSL(2) < 0.5
color_TSL2RVB(4) = color_TSL(2) * (1 + color_TSL(1))
else
color_TSL2RVB(4) = color_TSL(2) + color_TSL(1) - (color_TSL(2) * color_TSL(1))
end_if
color_TSL2RVB(3) = 2 * color_TSL(2) - color_TSL2RVB(4)
color_TSL2RVB(1) = color_TSL(0)/360
color_TSL2RVB(0) = color_TSL2RVB(1) + (1/3)
color_TSL2RVB(2) = color_TSL2RVB(1) - (1/3)
for color_i=0 to 2
if color_TSL2RVB(color_i) < 0 then color_TSL2RVB(color_i) = color_TSL2RVB(color_i) + 1
if color_TSL2RVB(color_i) > 1 then color_TSL2RVB(color_i) = color_TSL2RVB(color_i) - 1
next color_i
color_Calc_p = color_TSL2RVB(3) : color_Calc_q = color_TSL2RVB(4) : color_Calc_tc = color_TSL2RVB(0)
gosub CalcCompo: color_RVB(0) = int(color_Calc_result * 255)
color_Calc_p = color_TSL2RVB(3) : color_Calc_q = color_TSL2RVB(4) : color_Calc_tc = color_TSL2RVB(1)
gosub CalcCompo: color_RVB(1) = int(color_Calc_result * 255)
color_Calc_p = color_TSL2RVB(3) : color_Calc_q = color_TSL2RVB(4) : color_Calc_tc = color_TSL2RVB(2)
gosub CalcCompo: color_RVB(2) = int(color_Calc_result * 255)
end_if
return
CalcCompo:
if color_Calc_tc < (1/6)
color_Calc_result = color_Calc_p + ((color_Calc_q-color_Calc_p)*6*color_Calc_tc)
else
if color_Calc_tc < 0.5
color_Calc_result = color_Calc_q
else
if color_Calc_tc < (2/3)
color_Calc_result = color_Calc_p + ((color_Calc_q - color_Calc_p)*6*((2/3)-color_Calc_tc))
else
color_Calc_result = color_Calc_p
end_if
end_if
end_if
return
' -------------------------------------------------------
' Conversion RVB -> TSL (Teinte,Saturation,Luminosite)
' -------------------------------------------------------
RVBtoTSL:
r=color_RVB(0)/255
v=color_RVB(1)/255
b=color_RVB(2)/255
return
RVBtoTSV:
if (color_RVB(0) + color_RVB(1) + color_RVB(2)) = 0
color_TSV(0) = 0 : color_TSV(1) = 0 : color_TSV(2) = 0
return
end_if
' conversion composantes en float
for color_i = 0 to 2 : TsvByRgbCode(color_i) = color_RVB(color_i) / 255 : next color_i
' MaxVal
TsvByRgbCode(3) = TsvByRgbCode(0)
if TsvByRgbCode(1) > TsvByRgbCode(3) then TsvByRgbCode(3) = TsvByRgbCode(1)
if TsvByRgbCode(2) > TsvByRgbCode(3) then TsvByRgbCode(3) = TsvByRgbCode(2)
' MinVal
TsvByRgbCode(4) = TsvByRgbCode(0)
if TsvByRgbCode(1) < TsvByRgbCode(4) then TsvByRgbCode(4) = TsvByRgbCode(1)
if TsvByRgbCode(2) < TsvByRgbCode(4) then TsvByRgbCode(4) = TsvByRgbCode(2)
' Calcul de la teinte
color_TSV(0) = 0
if TsvByRgbCode(3)=TsvByRgbCode(4)
color_TSV(0)=0:color_TSV(1)=0
else
' rouge = MaxVal ?
if TsvByRgbCode(0) = TsvByRgbCode(3)
color_TSV(0) = (TsvByRgbCode(1)-TsvByRgbCode(2))/(TsvByRgbCode(3)-TsvByRgbCode(4)) : ' H = (V - B) / (Max - Min)
else
' vert = MaxVal ?
if TsvByRgbCode(1) = TsvByRgbCode(3)
color_TSV(0) = 2 + (TsvByRgbCode(2)-TsvByRgbCode(0)) / (TsvByRgbCode(3)-TsvByRgbCode(4)) : ' H = 2+ (B - R) / (Max - Min)
else
' bleu = MaxVal ?
if TsvByRgbCode(2) = TsvByRgbCode(3)
color_TSV(0) = 4 + (TsvByRgbCode(0)-TsvByRgbCode(1)) /(TsvByRgbCode(3)-TsvByRgbCode(4)) : ' H = 4 + (R - V) / (Max - Min)
end_if
end_if
end_if
color_TSV(1) = (TsvByRgbCode(3)-TsvByRgbCode(4)) / TsvByRgbCode(3) : ' Saturation = (Max - Min) / Max
end_if
color_TSV(0) = color_TSV(0) * 60 : ' Teinte
color_TSV(2) = TsvByRgbCode(3) : ' Val
if color_TSV(0) < 0 then color_TSV(0) = color_TSV(0) + 360
return
TSVtoRVB:
for color_i=0 to 2: RgbByHsv(color_i) = color_TSV(color_i) : next color_i
RgbByHsv(3) = int(RgbByHsv(0)/60) : ' faire un floor
if (RgbByHsv(0) = 360) then RgbByHsv(0) = 0
RgbByHsv(4) = RgbByHsv(0)/ 60 - RgbByHsv(3)
if (RgbByHsv(1)>1) then RgbByHsv(1) = RgbByHsv(1) / 100
if (RgbByHsv(2)>1) then RgbByHsv(2) = RgbByHsv(2) / 100
RgbByHsv(5) = RgbByHsv(2) * (1-RgbByHsv(1))
RgbByHsv(6) = RgbByHsv(2) * (1-(RgbByHsv(4)*RgbByHsv(1)))
RgbByHsv(7) = RgbByHsv(2) * (1 - ((1-RgbByHsv(4))*RgbByHsv(1)))
' par defaut :
color_RVB(0)=RgbByHsv(2):color_RVB(1)=RgbByHsv(5):color_RVB(2)=RgbByHsv(6)
select RgbByHsv(3)
case 0: color_RVB(0)=RgbByHsv(2):color_RVB(1)=RgbByHsv(7):color_RVB(2)=RgbByHsv(5)
case 1: color_RVB(0)=RgbByHsv(6):color_RVB(1)=RgbByHsv(2):color_RVB(2)=RgbByHsv(5)
case 2: color_RVB(0)=RgbByHsv(5):color_RVB(1)=RgbByHsv(2):color_RVB(2)=RgbByHsv(7)
case 3: color_RVB(0)=RgbByHsv(5):color_RVB(1)=RgbByHsv(6):color_RVB(2)=RgbByHsv(2)
case 4: color_RVB(0)=RgbByHsv(7):color_RVB(1)=RgbByHsv(5):color_RVB(2)=RgbByHsv(2)
end_select
if (RgbByHsv(1) = 0)
color_RVB(0)=RgbByHsv(2):color_RVB(1)=color_RVB(0):color_RVB(2)=color_RVB(0)
end_if
for color_i=0 to 2
color_RVB(color_i) = int(color_RVB(color_i) * 255)
if (color_RVB(color_i) > 255) then color_RVB(color_i) = 255
next color_i
return
lib_Color: