Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib

Public Class ChooseColor

   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   Private m_SetUpToolList As New ObservableCollection(Of ToolPos)

   Sub New(Owner As Window)
      Me.Owner = Owner
      InitializeComponent()
   End Sub

   Private Sub OpenFile_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' Posizione finestra
      Me.Top = Owner.Top + Owner.Height / 2 - Me.Height / 2
      Me.Left = Owner.Left + Owner.Width / 2 - Me.Width / 2

      ' Associazione del colore del bottone premuto nella OptionsPage al colore selezionato della finestra ChooseColor
      SelectedColorBtn.Background = m_MainWindow.m_brCurrentColor

      ' Associazione dei valori RGB del colore selezionato alle TextBox della finestra
      RTxBx.Text = (SelectedColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (SelectedColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (SelectedColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).B

      ' Messaggi
      RMsgTxBl.Text = "R"
      GMsgTxBl.Text = "G"
      BMsgTxBl.Text = "B"

   End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      ' Premendo il pulsante OK vengono presi i valori RGB (se ci sono campi vuoti 
      ' viene preso di deafult il valore 0) e viene costruito il colore da assegnare
      ' al bottone del colore premuto per aprire ChooseColor
      SelectedColorBtn.Background =
         New SolidColorBrush(Color.FromArgb(255,
                                            If(String.IsNullOrWhiteSpace(RTxBx.Text), "0", RTxBx.Text),
                                            If(String.IsNullOrWhiteSpace(GTxBx.Text), "0", GTxBx.Text),
                                            If(String.IsNullOrWhiteSpace(BTxBx.Text), "0", BTxBx.Text)))
      m_MainWindow.m_brCurrentColor = SelectedColorBtn.Background
      DialogResult = True
   End Sub

   Dim i As Integer = 0

   Private Sub RTxBx_TextChanged(sender As Object, e As EventArgs) Handles RTxBx.TextChanged
      ' Il valore da immettere nelle TextBox R, G e B deve essere un numero compreso tra 0 e 255, 
      ' altrimenti la TextBox tornerò ad essere vuota
      Try
         i = Integer.Parse(RTxBx.Text, Globalization.NumberStyles.Any)
      Catch ex As Exception
         RTxBx.Text = ""
      End Try
      If i >= 0 AndAlso i <= 255 Then
         If RTxBx.Text IsNot "" AndAlso GTxBx.Text IsNot "" AndAlso BTxBx.Text IsNot "" Then
            SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
         End If
         Exit Sub
         Else
            RTxBx.Text = ""
      End If
   End Sub

   Private Sub GTxBx_TextChanged(sender As Object, e As EventArgs) Handles GTxBx.TextChanged
      Try
         i = Integer.Parse(GTxBx.Text, Globalization.NumberStyles.Any)
      Catch ex As Exception
         GTxBx.Text = ""
      End Try
      If i >= 0 AndAlso i <= 255 Then
         If RTxBx.Text IsNot "" AndAlso GTxBx.Text IsNot "" AndAlso BTxBx.Text IsNot "" Then
            SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
         End If
         Exit Sub
      Else
         GTxBx.Text = ""
      End If
   End Sub

   Private Sub BTxBx_TextChanged(sender As Object, e As EventArgs) Handles BTxBx.TextChanged
      Try
         i = Integer.Parse(BTxBx.Text, Globalization.NumberStyles.Any)
      Catch ex As Exception
         BTxBx.Text = ""
      End Try
      If i >= 0 AndAlso i <= 255 Then
         If RTxBx.Text IsNot "" AndAlso GTxBx.Text IsNot "" AndAlso BTxBx.Text IsNot "" Then
            SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
         End If
         Exit Sub
      Else
         BTxBx.Text = ""
      End If
   End Sub

   ' Il click su uno qualsiasi dei 30 colori preimpostati riempirà le TextBox R, G e B coi valori corrispondenti
   ' e cambierà il colore selezionato con quello costruito dai nuovi valori RGB
   Private Sub Color1Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color1Btn.Click
      RTxBx.Text = (Color1Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color1Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color1Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color2Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color2Btn.Click
      RTxBx.Text = (Color2Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color2Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color2Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color3Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color3Btn.Click
      RTxBx.Text = (Color3Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color3Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color3Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color4Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color4Btn.Click
      RTxBx.Text = (Color4Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color4Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color4Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color5Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color5Btn.Click
      RTxBx.Text = (Color5Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color5Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color5Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color6Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color6Btn.Click
      RTxBx.Text = (Color6Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color6Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color6Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color7Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color7Btn.Click
      RTxBx.Text = (Color7Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color7Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color7Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color8Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color8Btn.Click
      RTxBx.Text = (Color8Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color8Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color8Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color9Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color9Btn.Click
      RTxBx.Text = (Color9Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color9Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color9Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color10Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color10Btn.Click
      RTxBx.Text = (Color10Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color10Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color10Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color11Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color11Btn.Click
      RTxBx.Text = (Color11Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color11Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color11Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color12Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color12Btn.Click
      RTxBx.Text = (Color12Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color12Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color12Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color13Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color13Btn.Click
      RTxBx.Text = (Color13Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color13Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color13Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color14Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color14Btn.Click
      RTxBx.Text = (Color14Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color14Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color14Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color15Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color15Btn.Click
      RTxBx.Text = (Color15Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color15Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color15Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color16Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color16Btn.Click
      RTxBx.Text = (Color16Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color16Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color16Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color17Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color17Btn.Click
      RTxBx.Text = (Color17Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color17Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color17Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color18Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color18Btn.Click
      RTxBx.Text = (Color18Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color18Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color18Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color19Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color19Btn.Click
      RTxBx.Text = (Color19Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color19Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color19Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color20Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color20Btn.Click
      RTxBx.Text = (Color20Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color20Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color20Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color21Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color21Btn.Click
      RTxBx.Text = (Color21Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color21Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color21Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color22Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color22Btn.Click
      RTxBx.Text = (Color22Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color22Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color22Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color23Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color23Btn.Click
      RTxBx.Text = (Color23Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color23Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color23Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color24Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color24Btn.Click
      RTxBx.Text = (Color24Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color24Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color24Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color25Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color25Btn.Click
      RTxBx.Text = (Color25Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color25Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color25Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color26Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color26Btn.Click
      RTxBx.Text = (Color26Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color26Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color26Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color27Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color27Btn.Click
      RTxBx.Text = (Color27Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color27Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color27Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color28Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color28Btn.Click
      RTxBx.Text = (Color28Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color28Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color28Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color29Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color29Btn.Click
      RTxBx.Text = (Color29Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color29Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color29Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

   Private Sub Color30Btn_Click(sender As Object, e As RoutedEventArgs) Handles Color30Btn.Click
      RTxBx.Text = (Color30Btn.Background.GetValue(SolidColorBrush.ColorProperty)).R
      GTxBx.Text = (Color30Btn.Background.GetValue(SolidColorBrush.ColorProperty)).G
      BTxBx.Text = (Color30Btn.Background.GetValue(SolidColorBrush.ColorProperty)).B
      SelectedColorBtn.Background = New SolidColorBrush(Color.FromArgb(255, RTxBx.Text, GTxBx.Text, BTxBx.Text))
   End Sub

End Class
