我試圖這段JavaScript代碼轉換成VB.net代碼,我有一些麻煩它的工作。所有JS所做的都是一些數學/字節操作,所以我不認爲我超出了任何一種語言的範圍。這裏的原代碼,從來源:亮或者變暗十六進制顏色
Pimp Trizkit's Colour Shading Code
function shadeColor2(color, percent) {
var f=parseInt(color.slice(1),16),t=percent<0?0:255,p=percent<0?percent*-1:percent,R=f>>16,G=f>>8&0x00FF,B=f&0x0000FF;
return "#"+(0x1000000+(Math.round((t-R)*p)+R)*0x10000+(Math.round((t-G)*p)+G)*0x100+(Math.round((t-B)*p)+B)).toString(16).slice(1);}
這裏就是我試圖將其轉換爲:
Public Function LightColor(color As String, percent As Integer) As Color
Dim f As Integer = Convert.ToInt32(color.Substring(1), 16)
Dim t As Integer
If percent < 0 Then
t = 0
Else
t = 255
End If
Dim p As Integer
If percent < 0 Then
p = percent * -1
Else
p = percent
End If
Dim R As Integer = f >> 16
Dim G As Integer = f >> 8 And &HFF
Dim B As Integer = f And &HFF
Dim finalColor As String = "#" + (Convert.ToString(_
(&H1000000 + (Math.Round((t - R) * p) + R) * &H10000 + _
(Math.Round((t - G) * p) + G) * &H100 + _
(Math.Round((t - B) * p) + B))) _
).Substring(1)
Me.txtID.Text = finalColor
Return ColorTranslator.FromHtml(finalColor)
End Function
我將不勝感激關於我的業餘轉換一些幫助和是否可以工作,我已經研究了相關的JS語法,但我不確定我是否正確地更改了它。 運行代碼才能使用這些參數之後:
LightColor("#2980b9", 20)
我得到一個8位十六進制顏色(#00642865),它甚至不是一種顏色,我覺得我有一些問題與十六進制輸出的格式。
你能[編輯]您的文章,並添加,爲什麼你自己的(公平)嘗試不工作?這樣我們不必真正運行你的程序,嘗試一些輸入,並思考哪些導致其他輸出的東西比你期望的要多。 – usr2564301
感謝您的建議和喲,只是編輯^^ – Dystopic