无法将函数从PHP转换为VB.NET

时间:2011-03-25 05:48:07

标签: php vb.net

我更像是一个PHP编码器,然后是VB.net程序员,所以我做了这个,并想知道是否有人可以帮助我转换这个功能。

<?php
function grabshade($r,$g,$b){
$colors = array(array(0, 0, 0), array(255, 255, 255));
$differencearray = array();
$colors2 = array(
'black' => array(0, 0, 0),
'white' => array(255, 255, 255));

foreach ($colors as $col2) {
$delta_r = $r - $col2[0];
$delta_g = $g - $col2[1];
$delta_b = $b - $col2[2];
$distance = $delta_r * $delta_r + $delta_g * $delta_g + $delta_b * $delta_b;
array_push($differencearray, $distance);
}
$smallest = min($differencearray);
$key = array_search($smallest, $differencearray);
return $key = array_search($colors[$key], $colors2);
}
?>

PHP脚本将采用r,g,b值并返回,如果它更接近白色或黑色。

这应该有效,但它没有返回任何东西。

Public Function findshade(ByVal r, ByVal g, ByVal b) As Integer
Dim colors As New ArrayList()
colors.Add("0:0:0")
colors.Add("255:255:255")
Dim differencearray As New ArrayList()

Dim colors2 As New ArrayList()
colors2.Add("0:0:0")
colors2.Add("255:255:255")
Dim i As Integer
For i = 0 To colors.Count - 1 Step i + 1
    Dim colorsparts As String() = colors(i).Split(":")
    Dim delta_r As Integer = r - colorsparts(0)
    Dim delta_g As Integer = g - colorsparts(1)
    Dim delta_b As Integer = b - colorsparts(2)
    Dim distance As Integer = delta_r * delta_r + delta_g * delta_g + delta_b * delta_b
    differencearray.Add(distance)
Next
differencearray.Sort()
Dim minValue As Integer = Convert.ToInt32(differencearray(0))

Dim result As New ArrayList()
For ii As Integer = 0 To differencearray.Count
    If differencearray(ii).Contains(minValue) Then result.Add(i)
Next
Dim key As Integer = result(0)
Dim result2 As New ArrayList()
For ii As Integer = 0 To colors2.Count
    If colors2(ii).Contains(key) Then result2.Add(i)
Next

Return result2(0)
End Function

3 个答案:

答案 0 :(得分:0)

我刚刚在VB.NET中编写了一个快速函数。希望它可以帮到你。

''' <summary>
''' Find the array index of the value if found in the String array. Else returns -1
''' </summary>
''' <param name="StringArray"></param>
''' <param name="Value"></param>
''' <returns></returns>
''' <remarks></remarks>
Function ArraySearch(ByVal StringArray As String(), ByVal Value As String) As Integer
    Dim i As Integer
    For i = 0 To StringArray.Length
        If StringArray(i) = Value Then
            Return i
        End If
    Next
    Return -1
End Function

PHP array_search可以在VB.NET中实现

How can I search an array in VB.NET?

答案 1 :(得分:0)

对于更通用的方法,您可以使用Phalanger将PHP代码编译为CLR,然后使用Reflector将其转换为VB.Net代码。

我在想,以防你需要翻译大量代码。输出代码不适合人眼,但可以执行。

答案 2 :(得分:0)

我采取了单独的方法,我制作的PHP代码意味着更多2种颜色,它的意思是(最初)20。它用于确定从阵列的颜色输入的最接近的颜色,但是因为现在我只做了黑与白最近我简化了一切。

 Public Function findshade(ByVal r, ByVal g, ByVal b) As Integer
        Dim delta_r As Integer = r - 255
        Dim delta_g As Integer = g - 255
        Dim delta_b As Integer = b - 255
        Dim distance As Integer = delta_r * delta_r + delta_g * delta_g + delta_b * delta_b


        Return distance
    End Function
    Public Function findshade2(ByVal r, ByVal g, ByVal b) As Integer
        Dim delta_r As Integer = r - 0
        Dim delta_g As Integer = g - 0
        Dim delta_b As Integer = b - 0
        Dim distance As Integer = delta_r * delta_r + delta_g * delta_g + delta_b * delta_b


        Return distance
    End Function

函数findshade将查找您的条目与白色的接近程度,并且findshade2将查找您的条目与黑色的接近程度。

要确定像素应该是黑色还是白色,您可以同时获得两个并且找到最小的值,这个值并不难。

TextBox1.Text = findshade(255, 255, 255).ToString
TextBox2.Text = findshade2(255, 255, 255).ToString