Public Class IngamePaletteEditorSub
    Dim clrs(16) As Drawing.Color

    Public Sub init()
        graphics.curPalette = graphics.gPALETTE_MAIN

        Dim i As Integer
        For i = 0 To 15
            clrs(i) = graphics.getpalette(graphics.curPalette, i)
        Next

        NumericUpDownP1.Value = App.graphics.f562.Byte1386(1) And 15
        NumericUpDownP2.Value = App.graphics.f562.Byte1386(2) And 15
        NumericUpDownP3.Value = App.graphics.f562.Byte1386(3) And 15
        NumericUpDownP4.Value = App.graphics.f562.Byte1386(4) And 15

        PS1382.init(App.graphics.f562.Byte1382, clrs)
        PS1366.init(App.graphics.f562.Byte1366, clrs)
    End Sub
    Sub commit()
        App.graphics.f562.Byte1386(1) = CByte(NumericUpDownP1.Value)
        App.graphics.f562.Byte1386(2) = CByte(NumericUpDownP2.Value)
        App.graphics.f562.Byte1386(3) = CByte(NumericUpDownP3.Value)
        App.graphics.f562.Byte1386(4) = CByte(NumericUpDownP4.Value)

        PS1382.commit(App.graphics.f562.Byte1382)
        PS1366.commit(App.graphics.f562.Byte1366)
    End Sub

    Private Sub NumericUpDownP1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownP1.ValueChanged
        LabelP1.BackColor = clrs(CByte(NumericUpDownP1.Value))
    End Sub

    Private Sub NumericUpDownP2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownP2.ValueChanged
        LabelP2.BackColor = clrs(CByte(NumericUpDownP2.Value))
    End Sub

    Private Sub NumericUpDownP3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownP3.ValueChanged
        LabelP3.BackColor = clrs(CByte(NumericUpDownP3.Value))
    End Sub

    Private Sub NumericUpDownP4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownP4.ValueChanged
        LabelP4.BackColor = clrs(CByte(NumericUpDownP4.Value))
    End Sub

    Class Pal2Pic
        Public Shared Function Rasterize1(ByVal p As Byte(), ByVal clrs As Color()) 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(ClrSelUt.ConvertShade10To1Color(clrs, p(1 + i))), New Rectangle(20 * i + 2, 2, 20 - 4, 20 - 4))
                Next
            End Using
            Rasterize1 = pic
        End Function
    End Class

    Class ClrSelUt
        'http://www.dungeon-master.com/forum/viewtopic.php?p=91917#91917

        'Usual DM colour order: 
        'colour 0 (black) 
        'colour 1 (grey3) 
        'colour 2 (grey2) 
        'colour 3 (brown) 
        'colour 4 (pale blue) 
        'colour 5 (pale brown) 
        'colour 6 (green) 
        'colour 7 (pale green) 
        'colour 8 (red) 
        'colour 9 (orange) 
        'colour 10 (skin) 
        'colour 11 (yellow) 
        'colour 12 (grey4) 
        'colour 13 (grey1) 
        'colour 14 (blue) 
        'colour 15 (white)
        Public Shared Function ConvertShade10To1Color(ByVal clrs As Color(), ByVal i As Byte) As Color
            If i < 160 Then
                ConvertShade10To1Color = clrs(Int(i / 10))
            Else
                ConvertShade10To1Color = Color.Empty
            End If
        End Function
    End Class

    Private Sub IngamePaletteEditorSub_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

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        commit()
        Hide()
    End Sub
End Class
