Public Class PalSel16c
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="p">1 based</param>
    ''' <param name="clrs">0 based</param>
    ''' <remarks></remarks>
    Public Sub init(ByVal p As Byte(), ByVal clrs As Color())
        Dim i As Integer
        For i = 1 To 16 : Me.pal(i) = p(i) : Next
        For i = 0 To 15 : Me.clrs(i) = clrs(i) : Next

        UpdateHorz()
        UpdateVert()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="p">1 based array</param>
    ''' <remarks></remarks>
    Public Sub commit(ByVal p As Byte())
        Dim i As Integer
        For i = 1 To 16 : p(i) = Me.pal(i) : Next
    End Sub

    Sub UpdateVert()
        Dim i As Integer
        Dim tsi As ToolStripMenuItem
        Dim pic As Drawing.Bitmap
        For i = 0 To 15
            tsi = ContextMenuStrip1.Items(i)
            'tsi.BackColor = clrs(i)
            pic = New Drawing.Bitmap(25, 25)
            Using cv As Drawing.Graphics = Drawing.Graphics.FromImage(pic)
                cv.Clear(clrs(i))
            End Using
            tsi.Image = pic
        Next
    End Sub
    Sub UpdateHorz()
        Dim i As Integer
        Dim v As Integer
        Dim tsi As ToolStripMenuItem
        For i = 0 To 15
            tsi = MenuStrip1.Items(i)
            v = pal(1 + i)
            If v < 160 Then tsi.BackColor = clrs(Int(v / 10)) Else tsi.BackColor = Color.Empty
        Next
    End Sub

    Dim pal(16) As Byte ' 1 based
    Dim clrs(16) As Drawing.Color ' 0 based

    Private Sub ToolStripMenuItem20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        ToolStripMenuItem20.Click, _
        ToolStripMenuItem21.Click, _
        ToolStripMenuItem22.Click, _
        ToolStripMenuItem23.Click, _
        ToolStripMenuItem24.Click, _
        ToolStripMenuItem25.Click, _
        ToolStripMenuItem26.Click, _
        ToolStripMenuItem27.Click, _
        ToolStripMenuItem28.Click, _
        ToolStripMenuItem29.Click, _
        ToolStripMenuItem30.Click, _
        ToolStripMenuItem31.Click, _
        ToolStripMenuItem32.Click, _
        ToolStripMenuItem33.Click, _
        ToolStripMenuItem34.Click, _
        ToolStripMenuItem35.Click
        pal(1 + cursel) = 10 * Int(CType(sender, ToolStripMenuItem).Text)
        UpdateHorz()
    End Sub

    Dim cursel As Integer

    Private Sub ToolStripMenuItem1_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        ToolStripMenuItem1.DropDownOpening, _
        ToolStripMenuItem2.DropDownOpening, _
        ToolStripMenuItem3.DropDownOpening, _
        ToolStripMenuItem4.DropDownOpening, _
        ToolStripMenuItem5.DropDownOpening, _
        ToolStripMenuItem6.DropDownOpening, _
        ToolStripMenuItem7.DropDownOpening, _
        ToolStripMenuItem8.DropDownOpening, _
        ToolStripMenuItem9.DropDownOpening, _
        ToolStripMenuItem10.DropDownOpening, _
        ToolStripMenuItem11.DropDownOpening, _
        ToolStripMenuItem12.DropDownOpening, _
        ToolStripMenuItem13.DropDownOpening, _
        ToolStripMenuItem14.DropDownOpening, _
        ToolStripMenuItem15.DropDownOpening, _
        ToolStripMenuItem16.DropDownOpening

        cursel = MenuStrip1.Items.IndexOf(sender)

        Dim i As Integer
        For i = 0 To 15
            CType(ContextMenuStrip1.Items(i), ToolStripMenuItem).Checked = (i = Int(pal(1 + cursel) / 10))
        Next
    End Sub
End Class
