Public Class IPEcsv
    Public Sub init()
        Dim s$
        s = "#  0RGB 1RGB 2RGB 3RGB 4RGB 5RGB 6RGB 7RGB 8RGB 9RGB10rgb11rgb12rgb13rgb14rgb15rgb" & vbCrLf
        s += "vp1 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(1)) & vbCrLf
        s += "vp2 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(2)) & vbCrLf
        s += "vp3 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(3)) & vbCrLf
        s += "vp4 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(4)) & vbCrLf
        s += "vp5 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(5)) & vbCrLf
        s += "vp6 " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette552(6)) & vbCrLf
        s += "ent " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette360) & vbCrLf
        s += "cre " & Ut.Pal2s(App.dlg_ingamepaletteeditor.Palette328) & vbCrLf
        tbcsv.Text = s
    End Sub

    Class Ut
        Public Shared Function Pal2s(ByVal p As PALETTE) As String
            Dim s$ = ""
            Dim i As Integer
            For i = 1 To 16
                s += "  "
                s += ((p.color(i) >> 8) And 15).ToString("X1")
                s += ((p.color(i) >> 4) And 15).ToString("X1")
                s += ((p.color(i) >> 0) And 15).ToString("X1")
            Next
            Pal2s = s.TrimStart()
        End Function

        Public Shared Function s2Pal(ByVal s As String) As PALETTE
            s = System.Text.RegularExpressions.Regex.Replace(s, "\s+", " ").Trim()
            s2Pal = New PALETTE()
            Dim i As Integer = 1
            Dim v As Short
            For Each col As String In s.Split(" ")
                If (col.Length = 3) And System.Text.RegularExpressions.Regex.IsMatch(col, "^[0-9a-fA-F]{3}$") Then
                    v = 0
                    v = v Or (Convert.ToInt16(col.Chars(0).ToString(), 16) << 8)
                    v = v Or (Convert.ToInt16(col.Chars(1).ToString(), 16) << 4)
                    v = v Or (Convert.ToInt16(col.Chars(2).ToString(), 16) << 0)
                    s2Pal.color(i) = v
                    i += 1
                Else
                    Exit For
                End If
            Next
        End Function
    End Class

    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        Close()
    End Sub

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        For Each row As String In tbcsv.Lines
            Dim cols As String() = System.Text.RegularExpressions.Regex.Replace(row, "\s+", " ").Trim().Split(" ")
            If cols.Length = 17 Then
                Dim p As PALETTE
                p = New PALETTE()
                Dim v As Short
                Dim i As Integer
                For i = 1 To 16
                    If (cols(i).Length = 3) And System.Text.RegularExpressions.Regex.IsMatch(cols(i), "^[0-9a-fA-F]{3}$") Then
                        v = 0
                        v = v Or (Convert.ToInt16(cols(i).Chars(0).ToString(), 16) << 8)
                        v = v Or (Convert.ToInt16(cols(i).Chars(1).ToString(), 16) << 4)
                        v = v Or (Convert.ToInt16(cols(i).Chars(2).ToString(), 16) << 0)
                        p.color(i) = v
                    Else
                        Exit For
                    End If
                Next

                Select Case cols(0)
                    Case "vp1" : App.dlg_ingamepaletteeditor.Palette552(1) = p
                    Case "vp2" : App.dlg_ingamepaletteeditor.Palette552(2) = p
                    Case "vp3" : App.dlg_ingamepaletteeditor.Palette552(3) = p
                    Case "vp4" : App.dlg_ingamepaletteeditor.Palette552(4) = p
                    Case "vp5" : App.dlg_ingamepaletteeditor.Palette552(5) = p
                    Case "vp6" : App.dlg_ingamepaletteeditor.Palette552(6) = p
                    Case "ent" : App.dlg_ingamepaletteeditor.Palette360 = p
                    Case "cre" : App.dlg_ingamepaletteeditor.Palette328 = p
                End Select
            End If
        Next
        App.dlg_ingamepaletteeditor.UpdateMyPal()
    End Sub

    Private Sub IPEcsv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class