- Сообщения
- 5,379
- Репутация
- 2,724
Функции для перевода цвета из цветовой модели RGB (Red, Green, Blue) в цветовую модель HSB (Hue, Saturation, Brightness) и наобоот (aka Photoshop). В обоих случаях входным и выходным параметрами служит массив, содержащий три составляющие соответствующего цвета. В качестве наглядного примера можно рассматривать ColorChooser.au3.
RGB -> HSB
HSB -> RGB
RGB -> HSB
Код:
Func _RGB2HSB($aRGB)
Local $Min = 255, $Max = 0
Local $HSB[3], $D, $H
For $i = 0 To 2
If $aRGB[$i] > $Max Then
$Max = $aRGB[$i]
EndIf
If $aRGB[$i] < $Min Then
$Min = $aRGB[$i]
EndIf
Next
If $Min = $Max Then
$HSB[0] = 0
$HSB[1] = 0
$HSB[2] = $Max / 255
Else
If $aRGB[0] = $Min Then
$D = $aRGB[1] - $aRGB[2]
$H = 3
Else
If $aRGB[1] = $Min Then
$D = $aRGB[2] - $aRGB[0]
$H = 5
Else
$D = $aRGB[0] - $aRGB[1]
$H = 1
EndIf
EndIf
$HSB[0] = ($H - ($D / ($Max - $Min))) / 6
$HSB[1] = ($Max - $Min) / $Max
$HSB[2] = $Max / 255
EndIf
$HSB[0] = Round($HSB[0] * 360)
If $HSB[0] = 360 Then
$HSB[0] = 0
EndIf
For $i = 1 To 2
$HSB[$i] = Round($HSB[$i] * 100)
Next
Return $HSB
EndFunc ;==>_RGB2HSB
HSB -> RGB
Код:
Func _HSB2RGB($aHSB)
Local $RGB[3], $H, $L, $F, $P, $Q, $T
For $i = 1 To 2
$aHSB[$i] /= 100
Next
If $aHSB[1] = 0 Then
For $i= 0 To 2
$RGB[$i] = $aHSB[2]
Next
Else
$H = $aHSB[0] / 60
$L = Floor($H)
$F = $H - $L
$P = $aHSB[2] * (1 - $aHSB[1])
$Q = $aHSB[2] * (1 - $aHSB[1] * $F)
$T = $aHSB[2] * (1 - $aHSB[1] * (1 - $F))
Switch $L
Case 1
$RGB[0] = $Q
$RGB[1] = $aHSB[2]
$RGB[2] = $P
Case 2
$RGB[0] = $P
$RGB[1] = $aHSB[2]
$RGB[2] = $T
Case 3
$RGB[0] = $P
$RGB[1] = $Q
$RGB[2] = $aHSB[2]
Case 4
$RGB[0] = $T
$RGB[1] = $P
$RGB[2] = $aHSB[2]
Case 5
$RGB[0] = $aHSB[2]
$RGB[1] = $P
$RGB[2] = $Q
Case Else
$RGB[0] = $aHSB[2]
$RGB[1] = $T
$RGB[2] = $P
EndSwitch
EndIf
For $i = 0 To 2
$RGB[$i] = Round($RGB[$i] * 255)
Next
Return $RGB
EndFunc ;==>_HSB2RGB