2015-08-16 46 views
1

我試圖這段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),它甚至不是一種顏色,我覺得我有一些問題與十六進制輸出的格式。

+0

你能[編輯]您的文章,並添加,爲什麼你自己的(公平)嘗試不工作?這樣我們不必真正運行你的程序,嘗試一些輸入,並思考哪些導致其他輸出的東西比你期望的要多。 – usr2564301

+0

感謝您的建議和喲,只是編輯^^ – Dystopic

回答

1

我不知道是什麼原因你的代碼沒有,但我確實看到一些潛在的隱患。最明顯的是percent參數。這應該是doublesingle,因爲可接受的範圍是>= -1.0<= +1.0

我創建了一個簡單的.NET小提琴,可在這裏:https://dotnetfiddle.net/QhowPP

Public Shared Function LightColor(htmlColor As String, percent As Double) As Color 

    If (String.IsNullOrEmpty(htmlColor)) Then 
     Throw New ArgumentNullException("htmlColor") 
    ElseIf ((percent < -1D) Or (percent > +1D)) Then 
     Throw New ArgumentOutOfRangeException("percent") 
    End If 

    Dim c = ColorTranslator.FromHtml(htmlColor) 
    Dim f = Int32.Parse(htmlColor.Substring(1), Globalization.NumberStyles.AllowHexSpecifier) 
    Dim t = If((percent < 0), 0, 255) 
    Dim p = If((percent < 0), (percent * -1), percent) 

    Dim result = ("#" & CInt(
     &H1000000 + (Math.Round((t - c.R) * p) + c.R) * 
     &H10000 + (Math.Round((t - c.G) * p) + c.G) * 
     &H100 + (Math.Round((t - c.B) * p) + c.B) 
    ).ToString("X").Substring(1)) 

    Return ColorTranslator.FromHtml(result) 

End Function 

C#:

public static Color LightColor(String htmlColor, Double percent) 
{ 

    if (String.IsNullOrEmpty(htmlColor)) 
    { 
     throw new ArgumentNullException("htmlColor"); 
    } 
    else if ((percent < -1D) | (percent > +1D)) 
    { 
     throw new ArgumentOutOfRangeException("percent"); 
    } 

    var c = ColorTranslator.FromHtml(htmlColor); 
    var f = Int32.Parse(htmlColor.Substring(1), System.Globalization.NumberStyles.AllowHexSpecifier); 
    var t = ((percent < 0) ? 0 : 255); 
    var p = ((percent < 0) ? (percent * -1) : percent); 

    var result = ("#" + ((Int32)(
     0x1000000 + (Math.Round((t - c.R) * p) + c.R) * 
     0x10000 + (Math.Round((t - c.G) * p) + c.G) * 
     0x100 + (Math.Round((t - c.B) * p) + c.B) 
    )).ToString("X").Substring(1)); 

    return ColorTranslator.FromHtml(result); 

} 
+0

非常感謝你的幫助!它完美的工作,我只需要輸入十進制百分比而不是我之前做的整數百分比,這也使得它更加合理。出於好奇,爲什麼你將它聲明爲共享函數?這樣做是否有內存/性能優勢? – Dystopic

+1

@Dystopic:不使用類實例中的任何成員的方法應該是靜態的(在VB中共享)。如果您將其設爲實例方法,則意味着它使用實例數據,並且您需要創建該類的實例以使用它。 – Guffa