Public Class IngamePaletteEditor
    Friend Palette552(6) As PALETTE
    Friend Palette360 As New PALETTE
    Friend Palette328 As New PALETTE

    Public Sub init()
        Dim i As Integer, j As Integer

        For i = 1 To 6
            Me.Palette552(i) = New PALETTE()
            For j = 1 To 16
                Me.Palette552(i).color(j) = App.graphics.f562.Palette552(i).color(j)
            Next
        Next
        For j = 1 To 16
            Me.Palette360.color(j) = App.graphics.f562.Palette360.color(j)
            Me.Palette328.color(j) = App.graphics.f562.Palette328.color(j)
        Next

        UpdateMyPal()
    End Sub

    Public Sub UpdateMyPal(Optional ByVal mask As Integer = 255)
        If mask And 1 Then PictureBox1.Image = Pal2Pic.Rasterize2(Palette552(1))
        If mask And 2 Then PictureBox2.Image = Pal2Pic.Rasterize2(Palette552(2))
        If mask And 4 Then PictureBox3.Image = Pal2Pic.Rasterize2(Palette552(3))
        If mask And 8 Then PictureBox4.Image = Pal2Pic.Rasterize2(Palette552(4))
        If mask And 16 Then PictureBox5.Image = Pal2Pic.Rasterize2(Palette552(5))
        If mask And 32 Then PictureBox6.Image = Pal2Pic.Rasterize2(Palette552(6))
        If mask And 64 Then PictureBox7.Image = Pal2Pic.Rasterize2(Palette360)
        If mask And 128 Then PictureBox8.Image = Pal2Pic.Rasterize2(Palette328)
    End Sub

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        Dim i As Integer

        For i = 1 To 6
            App.graphics.f562.SetPalette552(i, Me.Palette552(i))
        Next
        App.graphics.f562.SetPalette360(Me.Palette360)
        App.graphics.f562.SetPalette328(Me.Palette328)

        App.graphics.build_palettes()

        Hide()
    End Sub

    Class Pal2Pic
        Public Shared Function Rasterize2(ByVal p As PALETTE) As Drawing.Bitmap
            Dim pic As New Drawing.Bitmap(320, 20, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Using cv As Drawing.Graphics = Drawing.Graphics.FromImage(pic)
                cv.Clear(Color.Empty)
                Dim i As Integer
                For i = 0 To 15 Step 1
                    cv.DrawRectangle(Pens.Black, New Rectangle(20 * i + 1, +1, 20 - 3, 20 - 3))
                    cv.FillRectangle(New SolidBrush(MyWordClrUt.WordClrTo1Color(p.color(1 + i))), New Rectangle(20 * i + 2, 2, 20 - 4, 20 - 4))
                Next
            End Using
            Rasterize2 = pic
        End Function
    End Class

    Private Sub IngamePaletteEditor_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Select Case e.CloseReason
            Case CloseReason.UserClosing
                e.Cancel = True
                Hide()
        End Select
    End Sub

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

    ' EXPORT palettes -->
    Private Sub ButtonExportPal1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExportPal1.Click
        If sfdvp1.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As New Drawing.Bitmap(320, 200, Imaging.PixelFormat.Format4bppIndexed)
        Dim i As Integer
        Dim pal As Imaging.ColorPalette = pic.Palette
        For i = 0 To 15
            pal.Entries(i) = MyWordClrUt.WordClrTo1Color(Palette552(1).color(1 + i)) ' CHANGE HERE
        Next
        pic.Palette = pal

        pic.Save(sfdvp1.FileName, FFUt.Suggest(sfdvp1.FileName))
    End Sub
    Private Sub ButtonExportPal7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExportPal7.Click
        If sfdent.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As New Drawing.Bitmap(320, 200, Imaging.PixelFormat.Format4bppIndexed)
        Dim i As Integer
        Dim pal As Imaging.ColorPalette = pic.Palette
        For i = 0 To 15
            pal.Entries(i) = MyWordClrUt.WordClrTo1Color(Palette360.color(1 + i)) ' CHANGE HERE
        Next
        pic.Palette = pal

        pic.Save(sfdent.FileName, FFUt.Suggest(sfdent.FileName))
    End Sub
    Private Sub ButtonExportPal8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExportPal8.Click
        If sfdcre.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As New Drawing.Bitmap(320, 200, Imaging.PixelFormat.Format4bppIndexed)
        Dim i As Integer
        Dim pal As Imaging.ColorPalette = pic.Palette
        For i = 0 To 15
            pal.Entries(i) = MyWordClrUt.WordClrTo1Color(Palette328.color(1 + i)) ' CHANGE HERE
        Next
        pic.Palette = pal

        pic.Save(sfdcre.FileName, FFUt.Suggest(sfdcre.FileName))
    End Sub
    ' <-- EXPORT palettes

    ' IMPORT pals -->
    Private Sub ButtonImportPal1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonImportPal1.Click
        If ofdvp1.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As Drawing.Bitmap = MyPicUt.LoadPicFromFile(ofdvp1.FileName)
        Select Case pic.PixelFormat
            Case Imaging.PixelFormat.Format4bppIndexed
            Case Imaging.PixelFormat.Format8bppIndexed
            Case Else
                MsgBox("Specify 4bpp or 8bpp image file (.bmp/.gif/.png). Your's """ & pic.PixelFormat.ToString() & """" & vbCrLf & vbCrLf & "Import cancel.", MsgBoxStyle.Exclamation)
                Exit Sub
        End Select

        Dim pal As Imaging.ColorPalette = pic.Palette
        Dim i As Integer
        For i = 0 To 15
            Palette552(1).color(1 + i) = MyWordClrUt.OneColorToWordClr(pal.Entries(i)) ' CHANGE here
        Next
        UpdateMyPal(1) ' CHANGE here
    End Sub
    Private Sub ButtonImportPal7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonImportPal7.Click
        If ofdent.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As Drawing.Bitmap = MyPicUt.LoadPicFromFile(ofdent.FileName)
        Select Case pic.PixelFormat
            Case Imaging.PixelFormat.Format4bppIndexed
            Case Imaging.PixelFormat.Format8bppIndexed
            Case Else
                MsgBox("Specify 4bpp or 8bpp image file (.bmp/.gif/.png). Your's """ & pic.PixelFormat.ToString() & """" & vbCrLf & vbCrLf & "Import cancel.", MsgBoxStyle.Exclamation)
                Exit Sub
        End Select

        Dim pal As Imaging.ColorPalette = pic.Palette
        Dim i As Integer
        For i = 0 To 15
            Palette360.color(1 + i) = MyWordClrUt.OneColorToWordClr(pal.Entries(i)) ' CHANGE here
        Next
        UpdateMyPal(64) ' CHANGE here
    End Sub
    Private Sub ButtonImportPal8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonImportPal8.Click
        If ofdcre.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Exit Sub

        Dim pic As Drawing.Bitmap = MyPicUt.LoadPicFromFile(ofdcre.FileName)
        Select Case pic.PixelFormat
            Case Imaging.PixelFormat.Format4bppIndexed
            Case Imaging.PixelFormat.Format8bppIndexed
            Case Else
                MsgBox("Specify 4bpp or 8bpp image file (.bmp/.gif/.png). Your's """ & pic.PixelFormat.ToString() & """" & vbCrLf & vbCrLf & "Import cancel.", MsgBoxStyle.Exclamation)
                Exit Sub
        End Select

        Dim pal As Imaging.ColorPalette = pic.Palette
        Dim i As Integer
        For i = 0 To 15
            Palette328.color(1 + i) = MyWordClrUt.OneColorToWordClr(pal.Entries(i)) ' CHANGE here
        Next
        UpdateMyPal(128) ' CHANGE here
    End Sub
    ' <-- IMPORT pals

    Class FFUt ' File Format Utility
        Public Shared Function Suggest(ByVal fn As String) As Imaging.ImageFormat
            ' Return Bmp if no suggestion comes.
            If fn.ToLowerInvariant().EndsWith(".png") Then
                Suggest = Imaging.ImageFormat.Png
            ElseIf fn.ToLowerInvariant().EndsWith(".gif") Then
                Suggest = Imaging.ImageFormat.Gif
            Else
                Suggest = Imaging.ImageFormat.Bmp
            End If
        End Function
    End Class

    Private Sub ButtonAutocalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAutocalc.Click
        Dim c As Drawing.Color
        Dim j As Integer, Rv%, Gv%, Bv%
        Dim f!
        Dim fal!() = {0.9, 0.8, 0.7, 0.6, 0.5}
        Dim i As Integer
        For i = 2 To 6
            For j = 1 To 16
                c = MyWordClrUt.WordClrTo1Color(Me.Palette552(1).color(j))
                If j = 5 Then f = 1 Else f = fal(i - 2)
                Rv = c.R * f
                Gv = c.G * f
                Bv = c.B * f
                c = Color.FromArgb(Rv, Gv, Bv)
                Me.Palette552(i).color(j) = MyWordClrUt.OneColorToWordClr(c)
            Next
        Next
        UpdateMyPal(1 Or 2 Or 4 Or 8 Or 16 Or 32)
    End Sub

    Private Sub ButtonEditcsv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEditcsv.Click
        Dim form As New IPEcsv
        form.Show()
        form.init()
    End Sub

    Private Sub ButtonGrays1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGrays1.Click
        ' NTSC加重平均法
        ' Y = R × 0.298912 ＋ G × 0.586611 ＋ B × 0.114478
        ' http://www.r-nakai.com/archives/41

        Dim i As Integer
        Dim j As Integer
        Dim p As PALETTE
        Dim c As Drawing.Color
        Dim v As Byte
        For i = 0 To 7
            Select Case i
                Case 1 : p = Me.Palette552(2)
                Case 2 : p = Me.Palette552(3)
                Case 3 : p = Me.Palette552(4)
                Case 4 : p = Me.Palette552(5)
                Case 5 : p = Me.Palette552(6)
                Case 6 : p = Me.Palette360
                Case 7 : p = Me.Palette328
                Case Else : p = Me.Palette552(1)
            End Select
            For j = 1 To 16
                c = MyWordClrUt.WordClrTo1Color(p.color(j))
                v = Math.Max(0, Math.Min(255, Int(c.R * 0.298 + c.G * 0.586 + c.B * 0.114)))
                c = Color.FromArgb(v, v, v)
                p.color(j) = MyWordClrUt.OneColorToWordClr(c)
            Next
            Select Case i
                Case 1 : Me.Palette552(2) = p
                Case 2 : Me.Palette552(3) = p
                Case 3 : Me.Palette552(4) = p
                Case 4 : Me.Palette552(5) = p
                Case 5 : Me.Palette552(6) = p
                Case 6 : Me.Palette360 = p
                Case 7 : Me.Palette328 = p
                Case Else : Me.Palette552(1) = p
            End Select
        Next
        UpdateMyPal()
    End Sub
End Class