I am more of a PHP Coder then VB.net Programmer so I made this and was wondering if someone could be so kind as to help me converting this function.
<?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);
}
?>
The PHP Script will take a r,g,b value and return if its closer to white or black.
This should be working but it isn't returning anything.
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 Array开发者_运维技巧List()
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
I just wrote a quick function in VB.NET. Hope it helps you.
''' <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 can be implemented in VB.NET
How can I search an array in VB.NET?
For a more generic approach you can use Phalanger to compile PHP code as CLR, then use Reflector to translate it into VB.Net code.
I'm thinking in case you have loads of code to translate. The output code will not be perfect for human eyes, but will be executable.
I took a seperate approach, The php code I made was meant for alot more then 2 colors, it was meant for (originally) 20. It was used to determine the closest color you input out of the colors of the array, but since Now I am only doing Black and White Closest I simplified everything.
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
Function findshade will find how close your entry is to white, and findshade2 will find how close your entry is to black.
To determine if the pixel should be black or white you get both entrys and find the smallest value which isn't hard from there.
TextBox1.Text = findshade(255, 255, 255).ToString
TextBox2.Text = findshade2(255, 255, 255).ToString
精彩评论