1
votes

I'm attempting to convert this piece of Javascript to VB.net code, and I'm having some trouble getting it to work. All the JS does is some math/byte operations, so I don't think I'm going beyond the scope of either language. Here the original code, sourced from:

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);}

And here's what I've attempted to convert it to:

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

I would greatly appreciate some help regarding my amateur conversion and whether it could work, I've researched the relevant JS syntax but I'm not sure whether I changed it properly. After running my code with these parameters:

LightColor("#2980b9", 20)

I obtain a 8-digit hex color (#00642865) which is not even a color-I think I have some problems with the formatting of the hex output.

1
Can you edit your post and add why your own (fair) attempt is not working? That way we don't have to actually run your program, try some inputs, and ponder what ones lead to other output than you seem to be expecting.Jongware
Thanks for the advice, and yup, just edited ^^Dystopic

1 Answers

1
votes

I'm not exactly sure why your code fails, but I do see some potential pitfalls. The most obvious is the percent parameter. This should be a double or a single as the accepted range is >= -1.0 and <= +1.0.

I've created a simple .net fiddle, available here: 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);

}