Advertisement
Guest User

songtopicinator

a guest
Jan 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.93 KB | None | 0 0
  1. Public Class Form1
  2.     Public Class Colour
  3.         Public id As Char
  4.         Public r As Integer
  5.         Public g As Integer
  6.         Public b As Integer
  7.         Public Sub New(i As Char, red As Integer, green As Integer, blue As Integer)
  8.             id = i
  9.             If (r > 255 Or r < 0 Or g > 255 Or g < 0 Or b > 255 Or b < 0) Then
  10.                 Throw New ArgumentOutOfRangeException("r, g and b values must all be 0-255")
  11.             End If
  12.             r = red
  13.             g = green
  14.             b = blue
  15.         End Sub
  16.         Public Function getRed()
  17.             Return r
  18.         End Function
  19.         Public Function getGreen()
  20.             Return g
  21.         End Function
  22.         Public Function getBlue()
  23.             Return b
  24.         End Function
  25.         Public Function getId()
  26.             Return id
  27.         End Function
  28.         Public Function getColor()
  29.             Return Color.FromArgb(r, g, b)
  30.         End Function
  31.         Public Function diff(c As Color)
  32.             Dim rd = Math.Abs(r - c.R)
  33.             Dim gd = Math.Abs(g - c.G)
  34.             Dim bd = Math.Abs(b - c.B)
  35.             Dim d = rd + gd + bd
  36.             Dim s As String = "Comparing colour of (R:" & r & " G:" & g & " B:" & b & ") to colour of (R:" & c.R & " G:" & c.G & " B:" & c.B & ") produced rd of " & rd & ", gd of " & gd & " and bd of " & bd & " for a total diff of " & d
  37.             'System.Console.WriteLine(s)
  38.             Return d
  39.         End Function
  40.     End Class
  41.  
  42.     Public zero As Colour = New Colour("0"c, 0, 0, 0)
  43.     Public cola As Colour = New Colour("a"c, 9, 216, 157)
  44.     Public colb As Colour = New Colour("b"c, 19, 177, 59)
  45.     Public colc As Colour = New Colour("c"c, 29, 137, 216)
  46.     Public cold As Colour = New Colour("d"c, 39, 98, 118)
  47.     Public cole As Colour = New Colour("e"c, 49, 59, 19)
  48.     Public colf As Colour = New Colour("f"c, 59, 19, 177)
  49.     Public colg As Colour = New Colour("g"c, 68, 236, 78)
  50.     Public colh As Colour = New Colour("h"c, 78, 196, 236)
  51.     Public coli As Colour = New Colour("i"c, 88, 157, 137)
  52.     Public colj As Colour = New Colour("j"c, 98, 118, 39)
  53.     Public colk As Colour = New Colour("k"c, 108, 78, 196)
  54.     Public coll As Colour = New Colour("l"c, 118, 39, 98)
  55.     Public colm As Colour = New Colour("m"c, 128, 0, 0)
  56.     Public coln As Colour = New Colour("n"c, 137, 216, 157)
  57.     Public colo As Colour = New Colour("o"c, 147, 177, 59)
  58.     Public colp As Colour = New Colour("p"c, 157, 137, 216)
  59.     Public colq As Colour = New Colour("q"c, 167, 98, 118)
  60.     Public colr As Colour = New Colour("r"c, 177, 59, 19)
  61.     Public cols As Colour = New Colour("s"c, 187, 19, 177)
  62.     Public colt As Colour = New Colour("t"c, 196, 236, 78)
  63.     Public colu As Colour = New Colour("u"c, 206, 196, 236)
  64.     Public colv As Colour = New Colour("v"c, 216, 157, 137)
  65.     Public colw As Colour = New Colour("w"c, 226, 118, 39)
  66.     Public colx As Colour = New Colour("x"c, 236, 78, 196)
  67.     Public coly As Colour = New Colour("y"c, 246, 39, 98)
  68.     Public colz As Colour = New Colour("z"c, 255, 255, 255)
  69.  
  70.     Dim cs() As Colour = {zero, cola, colb, colc, cold, cole, colf, colg, colh, coli, colj, colk, coll, colm, coln, colo, colp, colq, colr, cols, colt, colu, colv, colw, colx, coly, colz}
  71.  
  72.     Public str As String = ""
  73.  
  74.     Public dlg As OpenFileDialog = New OpenFileDialog()
  75.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn.Click
  76.         dlg.ShowDialog()
  77.         If (dlg.FileName <> "") Then
  78.             dlg.FileName = dlg.FileName.ToLower()
  79.             If (dlg.FileName.EndsWith(".bmp") Or dlg.FileName.EndsWith(".jpg") Or dlg.FileName.EndsWith(".jpeg") Or dlg.FileName.EndsWith(".gif") Or dlg.FileName.EndsWith(".png") Or dlg.FileName.EndsWith(".tiff")) Then
  80.                 img.Image = Image.FromFile(dlg.FileName)
  81.                 Dim i As Bitmap = img.Image
  82.                 If (i.Width <> i.Height) Then
  83.                     MessageBox.Show("Image is not square, will not work properly with !songtopic. You may continue, but command won't produce desired image")
  84.                 ElseIf (i.Width > 44 Or i.Height > 44) Then
  85.                     MessageBox.Show("Image is too large to be sent via discord! You may continue, but command will be unusable (Command limit is 44x)")
  86.                 End If
  87.             End If
  88.         End If
  89.         calc()
  90.     End Sub
  91.  
  92.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  93.         Me.Text = "!songtopic-inator"
  94.         dlg.Title = "Open Image"
  95.         dlg.Filter = "Supported Image Files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.tiff"
  96.         img.SizeMode = PictureBoxSizeMode.Zoom
  97.     End Sub
  98.  
  99.     Private Sub tick_Tick(sender As Object, e As EventArgs) Handles tick.Tick
  100.         'calc()
  101.     End Sub
  102.  
  103.     Private Sub whitecb_CheckedChanged(sender As Object, e As EventArgs) Handles whitecb.CheckedChanged
  104.         calc()
  105.     End Sub
  106.  
  107.     Public Sub calc()
  108.         str = ""
  109.         If (dlg.FileName <> "") Then
  110.             dlg.FileName = dlg.FileName.ToLower()
  111.             If (dlg.FileName.EndsWith(".bmp") Or dlg.FileName.EndsWith(".jpg") Or dlg.FileName.EndsWith(".jpeg") Or dlg.FileName.EndsWith(".gif") Or dlg.FileName.EndsWith(".png") Or dlg.FileName.EndsWith(".tiff")) Then
  112.                 img.Image = Image.FromFile(dlg.FileName)
  113.                 Dim i As Bitmap = img.Image
  114.                 lbl.Text = i.Size.ToString()
  115.                 For y As Integer = 0 To i.Height - 1
  116.                     System.Console.WriteLine("y: " & y)
  117.                     For x As Integer = 0 To i.Width - 1
  118.                         Dim d = 255 + 255 + 255
  119.                         Dim c As Colour = zero
  120.                         If (i.GetPixel(x, y).A = 0) Then
  121.                             If (whitecb.Checked) Then
  122.                                 c = colz
  123.                             Else
  124.                                 c = zero
  125.                             End If
  126.                         Else
  127.                             For Each col In cs
  128.                                 Dim nd = col.diff(i.GetPixel(x, y))
  129.                                 'System.Console.WriteLine("x: " & x & " y: " & y & " testing colour " & col.getId() & " returned diff of " & nd)
  130.                                 If (nd < d) Then
  131.                                     d = nd
  132.                                     c = col
  133.                                 End If
  134.                             Next
  135.                         End If
  136.                         'System.Console.WriteLine("x: " & x & " y: " & y & " colour " & c.getId() & " passed with a diff of " & d)
  137.                         str = String.Concat(str, c.getId())
  138.                         'System.Console.WriteLine(str)
  139.                     Next
  140.                 Next
  141.                 test.Text = "!songtopic " & str
  142.             Else
  143.                 MessageBox.Show("File selected is not an image or is an unsupported file format.")
  144.                 dlg.ShowDialog()
  145.             End If
  146.         End If
  147.     End Sub
  148. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement