gpt4 book ai didi

javascript - 使十六进制颜色变亮或变暗

转载 作者:行者123 更新时间:2023-11-27 23:58:40 36 4
gpt4 key购买 nike

我正在尝试将这段 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),它甚至不是一种颜色 - 我认为我在十六进制输出的格式设置方面遇到了一些问题。

最佳答案

我不太确定您的代码失败的原因,但我确实看到了一些潜在的陷阱。最明显的是percent范围。这应该是 doublesingle因为接受的范围是 >= -1.0<= +1.0

我创建了一个简单的.net fiddle,可在此处获取: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);

}

关于javascript - 使十六进制颜色变亮或变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32032492/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com