Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Structure SColour
- Dim da() As Boolean
- End Structure
- Structure NotRGB
- Dim red As Int16
- Dim green As Int16
- Dim blue As Int16
- End Structure
- Private _rGB As Integer
- Function toRGB(ByVal Red, ByVal Green, ByVal Blue) As NotRGB
- toRGB.red = Red
- toRGB.blue = Blue
- toRGB.green = Green
- End Function
- Function score(ByVal colour1, ByVal colour)
- Dim total As Integer
- total = ((colour1.red - colour.red) ^ 2) ^ 0.5
- total += ((colour1.green - colour.green) ^ 2) ^ 0.5
- total += ((colour1.blue - colour.blue) ^ 2) ^ 0.5
- score = total
- End Function
- Function ifgray(ByVal colour) As Boolean
- colour.red = Math.Round(colour.red / 10)
- colour.green = Math.Round(colour.green / 10)
- colour.blue = Math.Round(colour.blue / 10)
- If colour.red > colour.green Then 'R>G
- If colour.blue > colour.green Then 'R>G B>G
- If colour.blue > colour.red Then 'B>R>G
- If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.red = colour.green Or colour.green + 1 = colour.red) Then
- Return True 'gray
- Else
- Return False 'not gray
- End If
- Exit Function
- Else 'R>B>G
- If (colour.blue = colour.red Or colour.red - 1 = colour.blue) And (colour.blue = colour.green Or colour.green + 1 = colour.blue) Then
- Return True 'gray
- Else
- Return False 'not gray
- End If
- Exit Function
- End If
- Else 'R>G>B
- End If
- Else 'G>R
- If colour.blue > colour.green Then 'B>G>R
- If (colour.blue = colour.green Or colour.blue - 1 = colour.green) And (colour.red = colour.green Or colour.green - 1 = colour.red) Then
- Return True 'gray
- Else
- Return False 'not gray
- End If
- Exit Function
- Else 'G>R G>B
- If colour.red > colour.blue Then 'G>R>B
- If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.red = colour.green Or colour.green + 1 = colour.red) Then
- Return True 'gray
- Else
- Return False 'not gray
- End If
- Exit Function
- Else 'G>B>R
- If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.blue = colour.green Or colour.green - 1 = colour.blue) Then
- Return True 'gray
- Else
- Return False 'not gray
- End If
- Exit Function
- End If
- End If
- End If
- End Function
- Function ToSColour(ByVal colour)
- Dim scores As Int16 = 1000 'The largest score
- Dim cur As Int16 ' a hold for the current score
- Dim col As Int16 ' The colour as number
- If colour.red < 30 And colour.blue < 30 And colour.green < 30 Then
- col = 0 ' black
- ElseIf colour.red > 230 And colour.blue > 230 And colour.green > 230 Then
- col = 1 'white
- ElseIf ifgray(colour) Then
- col = 2 ' gray - This is because all of the primary colours are within 10 of each other
- ElseIf colour.red + colour.blue <= colour.green - 10 Then
- col = 6 'green
- ElseIf colour.green < 40 And colour.red > 100 And colour.red < 200 And colour.blue < 100 And colour.blue > 50 Then
- col = 3 'brown - to stop it making green brown
- Else
- For i = 4 To 7
- cur = score(colour, ASColour(i))
- If cur < scores Then
- scores = cur
- col = i
- End If
- 'MsgBox("R:" & colour.red & "G:" & colour.green & "B:" & colour.blue & "I:" & i & "cur:" & cur & "Col:" & col)
- Next
- End If
- Return col
- End Function 'which clour is closest
- Dim ASColour(10) As NotRGB
- Private Sub BTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTranslate.Click
- Dim image As Bitmap = PictureBox1.Image
- Dim colourHold As NotRGB 'holds the current pixles colour
- For h = 0 To image.Height - 1
- For w = 0 To image.Width - 1
- 'get the colours
- colourHold.red = image.GetPixel(w, h).R
- colourHold.green = image.GetPixel(w, h).G
- colourHold.blue = image.GetPixel(w, h).B
- 'prosses
- colourHold = ASColour(ToSColour(colourHold))
- 'MsgBox(colourHold.red)
- 'set colours
- image.SetPixel(w, h, ColorTranslator.FromOle(RGB(colourHold.red, colourHold.green, colourHold.blue)))
- Next
- Next
- PictureBox2.Image = image
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ASColour(0) = toRGB(255, 255, 255) 'white
- ASColour(1) = toRGB(0, 0, 0) 'black
- ASColour(2) = toRGB(128, 128, 128) 'gray
- ASColour(3) = toRGB(150, 75, 0) 'brown
- ASColour(4) = toRGB(255, 0, 0) 'red
- ASColour(5) = toRGB(255, 255, 0) 'yellow
- ASColour(6) = toRGB(0, 255, 0) 'green
- ASColour(7) = toRGB(0, 0, 255)
- End Sub
- Private Sub fetch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fetch.Click
- Try
- Dim coordinates() As String = Split(TextBox1.Text, ":")
- Dim imagean As Bitmap = PictureBox1.Image
- MsgBox(imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).R & ":" & imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).B & ":" & imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).G)
- Catch
- MsgBox("Error!!!")
- End Try
- End Sub
- Private Sub BLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BLoad.Click
- OpenFileDialog1.ShowDialog()
- Dim path As String = OpenFileDialog1.FileName
- Dim Bitload As New Bitmap(path)
- PictureBox1.Image = Bitload
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment