' Complementary Color
Sub GetComplementaryColor(color As Int) As Int
' แยกค่า RGB
Dim r As Int = Bit.And(Bit.UnsignedShiftRight(color, 16), 0xFF)
Dim g As Int = Bit.And(Bit.UnsignedShiftRight(color, 8), 0xFF)
Dim b As Int = Bit.And(color, 0xFF)
' แปลง RGB เป็น HSV
Dim hsv() As Float = RGBtoHSV(r, g, b)
' หมุนค่า Hue 180 องศา เพื่อหาสีตรงข้าม
Dim newHue As Float = hsv(0) + 180
If newHue >= 360 Then newHue = newHue - 360
' แปลงกลับเป็น RGB
Dim newRGB() As Int = HSVtoRGB(newHue, hsv(1), hsv(2))
Return xui.color_RGB(newRGB(0), newRGB(1), newRGB(2))
End Sub
' แปลง RGB เป็น HSV
Sub RGBtoHSV(r As Int, g As Int, b As Int) As Float()
Dim rf As Float = r / 255.0
Dim gf As Float = g / 255.0
Dim bf As Float = b / 255.0
Dim maxVal As Float = Max(Max(rf, gf), bf)
Dim minVal As Float = Min(Min(rf, gf), bf)
Dim delta As Float = maxVal - minVal
Dim h, s, v As Float
' คำนวณ Value
v = maxVal
' คำนวณ Saturation
If maxVal = 0 Then
s = 0
Else
s = delta / maxVal
End If
' คำนวณ Hue
If delta = 0 Then
h = 0
Else If maxVal = rf Then
h = 60 * (((gf - bf) / delta) Mod 6)
Else If maxVal = gf Then
h = 60 * (((bf - rf) / delta) + 2)
Else
h = 60 * (((rf - gf) / delta) + 4)
End If
If h < 0 Then h = h + 360
Return Array As Float(h, s, v)
End Sub
' แปลง HSV เป็น RGB
Sub HSVtoRGB(h As Float, s As Float, v As Float) As Int()
Dim c As Float = v * s
Dim x As Float = c * (1 - Abs(((h / 60) Mod 2) - 1))
Dim m As Float = v - c
Dim r1, g1, b1 As Float
If h >= 0 And h < 60 Then
r1 = c: g1 = x: b1 = 0
Else If h >= 60 And h < 120 Then
r1 = x: g1 = c: b1 = 0
Else If h >= 120 And h < 180 Then
r1 = 0: g1 = c: b1 = x
Else If h >= 180 And h < 240 Then
r1 = 0: g1 = x: b1 = c
Else If h >= 240 And h < 300 Then
r1 = x: g1 = 0: b1 = c
Else
r1 = c: g1 = 0: b1 = x
End If
Dim r As Int = (r1 + m) * 255
Dim g As Int = (g1 + m) * 255
Dim b As Int = (b1 + m) * 255
Return Array As Int(r, g, b)
End Sub