原始碼:http://down.gogobox.com.tw/t2329175/6r2m2
之前比賽時因為VB6沒有反Cos函數而飲恨,
現在有了2005不一樣了!
但我發現題目上的公式根本寫錯了!
就因為這個小錯,算出來的答案就差了255倍!
真是嚇死人了!
程式碼如下:
Public Class Form1
Dim pi As Double = 4 * Math.Atan(1)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim R, G, B, tm1, tm2, h, ds, di As Double
Dim Rs, Gs, Bs, S As Double
R = Val(TB1.Text)
G = Val(TB2.Text)
B = Val(TB3.Text)
S = R + G + B
Rs = R / S
Gs = G / S
Bs = B / S
tm1 = 0.5 * (2 * Rs - Gs - Bs)
tm2 = ((Rs - Gs) ^ 2 + (Rs - Bs) * (Gs - Bs)) ^ 0.5
If Bs <= Gs Then
h = Math.Acos(tm1 / tm2)
ElseIf Bs > Gs Then
h = 2 * pi - Math.Acos(tm1 / tm2)
End If
If tm2 < 0.01 And tm2 > -0.01 Then h = 0
TB11.Text = Format(h * 180 / pi, "0.00000")
ds = 1 - 3 * min(R, G, B) / S
TB22.Text = Format(ds * 255, "0.00000")
di = (R + G + B) / 3
TB33.Text = Format(di, "0.00000")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Dh, H, Ds, S, Di, I, xx, yy, zz, R, G, B, tm1, tm2 As Double
Dim j As Integer
H = Val(TB11.Text)
S = Val(TB22.Text)
I = Val(TB33.Text)
Dh = H * pi / 180
Ds = S / 255
Di = I / 255
If H > 120 And H <= 240 Then
Dh -= 2 * pi / 3
ElseIf H > 240 And H <= 360 Then
Dh -= -4 * pi / 3
End If
xx = Di * (1 - Ds)
tm1 = Ds * Math.Cos(Dh)
tm2 = Math.Cos(pi / 3 - Dh)
yy = Di * (1 + tm1 / tm2)
zz = 3 * Di - (xx + yy)
If H <= 120 Then
R = yy
G = zz
B = xx
ElseIf H > 120 And H <= 240 Then
R = xx
G = yy
B = zz
ElseIf H > 240 And H <= 360 Then
R = zz
G = xx
B = yy
End If
TB1.Text = Format(R * 255, "0.00000")
TB2.Text = Format(G * 255, "0.00000")
TB3.Text = Format(B * 255, "0.00000")
End Sub

Function min(ByVal r As Double, ByVal g As Double, ByVal b As Double) As Double
min = r
If min > g Then min = g
If min > b Then min = b
End Function
End Class

讓地獄深紅的天亮 發表在 痞客邦 PIXNET 留言(0) 人氣()