gpt4 book ai didi

asp.net - 确定最接近的已知颜色

转载 作者:行者123 更新时间:2023-12-01 01:05:21 25 4
gpt4 key购买 nike

我允许我的应用程序的用户选择自定义颜色,并希望有一种方法可以为每种颜色显示一个友好的名称,而不是显示十六进制代码的文本表示。

我如何找到最近的 System.Drawing.Color对于给定的十六进制代码?

最佳答案

希望这可以帮助某人...

Public Function GetClosestColor(hex_code As String) As Color
Dim c As Color = ColorTranslator.FromHtml(hex_code)
Dim closest_distance As Double = Double.MaxValue
Dim closest As Color = Color.White

For Each kc In [Enum].GetValues(GetType(KnownColor)).Cast(Of KnownColor).Select(Function(x) Color.FromKnownColor(x))
'Calculate Euclidean Distance
Dim r_dist_sqrd As Double = Math.Pow(CDbl(c.R) - CDbl(kc.R), 2)
Dim g_dist_sqrd As Double = Math.Pow(CDbl(c.G) - CDbl(kc.G), 2)
Dim b_dist_sqrd As Double = Math.Pow(CDbl(c.B) - CDbl(kc.B), 2)
Dim d As Double = Math.Sqrt(r_dist_sqrd + g_dist_sqrd + b_dist_sqrd)

If d < closest_distance Then
closest_distance = d
closest = kc
End If
Next

Return closest
End Function

关于asp.net - 确定最接近的已知颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19323362/

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