Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a simple Palette Builder
#6
Cleaned up the mouser sub to fit bplus's recommendations.   (I had my while:wend in the wrong spot in an earlier version none of you were unfortunate enough to see so I went with that routine from the example).

Code: (Select All)
'Palette_Builder03b
'by James D. Jarvis , Feb 2/2/2023
'
'a simple 256 color palette builder for QB64PE
' saves and loads simple palette data files or basic source code to build a palette
'defaultpal loads an embedded default palette
Dim Shared klr(0 To 255) As _Unsigned Long
Dim tklr As _Unsigned Long
Screen _NewImage(1100, 400, 256)
_Title "Palette_Builder"
Dim Shared showpalnos
showpalnos = 0
defaultpal
Color 255, 0
drawgrid

Do
    _Limit 60
    kk$ = InKey$
    Mouser mx, my, mb
    If mb = -1 And lb = 0 Then 'open color dialog on left button mouse click over grid position for color
        If mx > -1 And mx < 1024 And my > -1 And my < 256 Then
            px = mx \ 32
            py = my \ 32
            pk = py * 32 + px
            Line (10, 310)-(800, 340), klr(0), BF
            pm$ = "Color #: " + Str$(pk) + " R,G,B: " + Str$(_Red32(klr(pk))) + "," + Str$(_Green32(klr(pk))) + "," + Str$(_Blue32(klr(pk)))
            _PrintString (10, 312), pm$
        End If
    End If
    If mb = 0 And lb = -2 Then 'open color dialog on right button mouse release over grid position for color
        If mx > -1 And mx < 1024 And my > -1 And my < 256 Then
            px = mx \ 32
            py = my \ 32
            pk = py * 32 + px
            If pk > -1 And pk < 256 Then
                klr(pk) = _ColorChooserDialog("Choose Color", _RGB32(_Red32(klr(pk)), _Green32(klr(pk)), _Blue32(klr(pk))))
                'klr(pk) = _ColorChooserDialog("Choose Color", pk)
                _PaletteColor pk, klr(pk)
            End If
        End If
    End If
    lb = mb 'record mouse button just clicked as last button clicked
    Select Case kk$
        Case "s", "S" 'save palette
            savefile$ = _SaveFileDialog$("Save File", "", "*.*", "")
            If savefile$ <> "" Then
                _MessageBox "Information", "File will be saved to " + savefile$
                Open savefile$ For Output As #1
                For k = 0 To 255
                    Print #1, klr(k)
                Next k
                Close #1
            End If
        Case "l", "L" 'load palette
            loadfile$ = _OpenFileDialog$("Open File", "", "*.*", "*.*", -1)
            If loadfile$ <> "" Then
                _MessageBox "Information", "You selected " + loadfile$
                k = 0
                Open loadfile$ For Input As #1
                Do Until EOF(1)
                    Input #1, klr(k)
                    _PaletteColor k, klr(k)
                    k = k + 1
                Loop
                Close #1
                drawgrid
            End If
        Case "b", "B" 'save basic code for palette to a file
            savefile$ = _SaveFileDialog$("Save Basic Code to File", "", "*.*", "")
            If savefile$ <> "" Then
                _MessageBox "Information", "File will be saved to " + savefile$
                Open savefile$ For Output As #1
                Print #1, "'256 color palette uncomment lines as needed for use"
                Print #1, "'Screen _NewImage(600, 400,256) "
                Print #1, "'dim shared klr(0 to 255) as _unsigned long"
                For k = 0 To 255
                    bc$ = ""
                    bc$ = "klr(" + _Trim$(Str$(k)) + ") = _rgb32(" + _Trim$(Str$(_Red32(klr(k)))) + "," + _Trim$(Str$(_Green32(klr(k)))) + "," + _Trim$(Str$(_Blue32(klr(k)))) + ")"
                    Print #1, bc$
                Next k
                Print #1, "'For k = 0 To 255 "
                Print #1, "' _PaletteColor k, klr(k) "
                Print #1, "' Next k"
                Close #1
            End If
        Case "n", "N" 'toggle display of color numbers on palette grid
            If showpalnos = 0 Then showpalnos = 1 Else showpalnos = 0
            drawgrid
        Case "c", "C"
            Mouser mx, my, mb
            If mx > -1 And mx < 1024 And my > -1 And my < 256 Then
                px = mx \ 32
                py = my \ 32
                pk = py * 32 + px
                Line (10, 310)-(800, 340), klr(0), BF
                pm$ = "Color #: " + Str$(pk) + " R,G,B: " + Str$(_Red32(klr(pk))) + "," + Str$(_Green32(klr(pk))) + "," + Str$(_Blue32(klr(pk)))
                _PrintString (10, 312), pm$
                If pk > 0 And pk < 256 Then tklr = klr(pk)
            End If
        Case "p", "P"
            Mouser mx, my, mb
            If mx > -1 And mx < 1024 And my > -1 And my < 256 Then
                px = mx \ 32
                py = my \ 32
                pk = py * 32 + px
                If pk > 0 And pk < 256 Then klr(pk) = tklr
                _PaletteColor pk, klr(pk)
            End If


    End Select
Loop Until kk$ = Chr$(27)
System

'draw the palette grid
Sub drawgrid
    _PrintMode _KeepBackground
    For y = 0 To 7
        For x = 0 To 31
            yy = y * 32
            xx = x * 16
            dk = yy + x
            xx = xx * 2
            Line (xx, yy)-(xx + 30, yy + 30), dk, BF
            If showpalnos = 1 Then
                _PrintString (xx + 2, yy + 6), _Trim$(Str$(dk))
            End If
    Next x, y
    _PrintString (10, 257), "S - Save File   L - Load File   B - Save Basic Code  N - show color #'s <ESC> -QUIT "
    _PrintString (10, 275), "Left Click - show RGB values    Right Click - change RGB values "
    _PrintString (10, 293), "C - Copy color    P - Paste color"
End Sub
'mouse sub from wiki with added check for mouse(2)
Sub Mouser (x, y, b)
    While _MouseInput: Wend
    b = _MouseButton(1)
    If _MouseButton(2) = -1 Then b = -2
    x = _MouseX
    y = _MouseY
End Sub

Sub defaultpal
    'a default 256 color palette
    klr(0) = _RGB32(0, 0, 0)
    klr(1) = _RGB32(0, 0, 200)
    klr(2) = _RGB32(0, 80, 0)
    klr(3) = _RGB32(0, 217, 217)
    klr(4) = _RGB32(255, 0, 0)
    klr(5) = _RGB32(193, 0, 193)
    klr(6) = _RGB32(149, 5, 5)
    klr(7) = _RGB32(192, 192, 192)
    klr(8) = _RGB32(100, 100, 100)
    klr(9) = _RGB32(0, 128, 255)
    klr(10) = _RGB32(128, 255, 128)
    klr(11) = _RGB32(120, 240, 255)
    klr(12) = _RGB32(255, 128, 0)
    klr(13) = _RGB32(235, 150, 235)
    klr(14) = _RGB32(255, 255, 0)
    klr(15) = _RGB32(235, 235, 235)
    klr(16) = _RGB32(16, 12, 6)
    klr(17) = _RGB32(30, 24, 12)
    klr(18) = _RGB32(12, 16, 6)
    klr(19) = _RGB32(24, 30, 12)
    klr(20) = _RGB32(12, 6, 16)
    klr(21) = _RGB32(16, 6, 12)
    klr(22) = _RGB32(36, 12, 12)
    klr(23) = _RGB32(95, 60, 30)
    klr(24) = _RGB32(97, 70, 52)
    klr(25) = _RGB32(113, 56, 0)
    klr(26) = _RGB32(160, 160, 150)
    klr(27) = _RGB32(240, 240, 142)
    klr(28) = _RGB32(50, 0, 0)
    klr(29) = _RGB32(0, 50, 0)
    klr(30) = _RGB32(0, 0, 50)
    klr(31) = _RGB32(130, 125, 120)
    klr(32) = _RGB32(10, 10, 10)
    klr(33) = _RGB32(0, 0, 210)
    klr(34) = _RGB32(0, 100, 0)
    klr(35) = _RGB32(15, 217, 217)
    klr(36) = _RGB32(255, 15, 15)
    klr(37) = _RGB32(193, 15, 193)
    klr(38) = _RGB32(149, 20, 20)
    klr(39) = _RGB32(199, 199, 199)
    klr(40) = _RGB32(110, 110, 110)
    klr(41) = _RGB32(15, 128, 255)
    klr(42) = _RGB32(132, 255, 132)
    klr(43) = _RGB32(140, 240, 255)
    klr(44) = _RGB32(255, 128, 32)
    klr(45) = _RGB32(240, 155, 240)
    klr(46) = _RGB32(255, 255, 40)
    klr(47) = _RGB32(240, 240, 240)
    klr(48) = _RGB32(48, 38, 19)
    klr(49) = _RGB32(49, 39, 19)
    klr(50) = _RGB32(38, 48, 19)
    klr(51) = _RGB32(39, 49, 19)
    klr(52) = _RGB32(38, 19, 48)
    klr(53) = _RGB32(48, 19, 38)
    klr(54) = _RGB32(48, 18, 18)
    klr(55) = _RGB32(115, 73, 32)
    klr(56) = _RGB32(132, 96, 72)
    klr(57) = _RGB32(183, 91, 0)
    klr(58) = _RGB32(170, 170, 150)
    klr(59) = _RGB32(245, 245, 154)
    klr(60) = _RGB32(60, 0, 0)
    klr(61) = _RGB32(0, 60, 0)
    klr(62) = _RGB32(0, 0, 60)
    klr(63) = _RGB32(110, 125, 135)
    klr(64) = _RGB32(20, 20, 20)
    klr(65) = _RGB32(0, 0, 220)
    klr(66) = _RGB32(0, 120, 0)
    klr(67) = _RGB32(30, 217, 217)
    klr(68) = _RGB32(255, 30, 30)
    klr(69) = _RGB32(193, 30, 193)
    klr(70) = _RGB32(149, 40, 40)
    klr(71) = _RGB32(204, 204, 204)
    klr(72) = _RGB32(120, 120, 120)
    klr(73) = _RGB32(30, 128, 255)
    klr(74) = _RGB32(136, 255, 136)
    klr(75) = _RGB32(160, 240, 255)
    klr(76) = _RGB32(255, 128, 64)
    klr(77) = _RGB32(245, 160, 245)
    klr(78) = _RGB32(255, 255, 80)
    klr(79) = _RGB32(245, 245, 245)
    klr(80) = _RGB32(80, 64, 32)
    klr(81) = _RGB32(94, 75, 37)
    klr(82) = _RGB32(64, 80, 32)
    klr(83) = _RGB32(75, 94, 37)
    klr(84) = _RGB32(64, 32, 80)
    klr(85) = _RGB32(80, 32, 64)
    klr(86) = _RGB32(60, 24, 24)
    klr(87) = _RGB32(125, 83, 33)
    klr(88) = _RGB32(165, 120, 90)
    klr(89) = _RGB32(236, 118, 0)
    klr(90) = _RGB32(180, 180, 150)
    klr(91) = _RGB32(250, 250, 166)
    klr(92) = _RGB32(70, 0, 0)
    klr(93) = _RGB32(0, 70, 0)
    klr(94) = _RGB32(0, 0, 70)
    klr(95) = _RGB32(200, 150, 200)
    klr(96) = _RGB32(30, 30, 30)
    klr(97) = _RGB32(0, 0, 230)
    klr(98) = _RGB32(0, 140, 0)
    klr(99) = _RGB32(60, 217, 217)
    klr(100) = _RGB32(255, 60, 60)
    klr(101) = _RGB32(193, 60, 193)
    klr(102) = _RGB32(149, 60, 60)
    klr(103) = _RGB32(208, 208, 208)
    klr(104) = _RGB32(140, 140, 140)
    klr(105) = _RGB32(45, 128, 255)
    klr(106) = _RGB32(140, 255, 140)
    klr(107) = _RGB32(180, 240, 255)
    klr(108) = _RGB32(255, 128, 96)
    klr(109) = _RGB32(250, 160, 250)
    klr(110) = _RGB32(255, 255, 100)
    klr(111) = _RGB32(250, 245, 245)
    klr(112) = _RGB32(112, 89, 44)
    klr(113) = _RGB32(126, 100, 50)
    klr(114) = _RGB32(89, 112, 44)
    klr(115) = _RGB32(100, 126, 50)
    klr(116) = _RGB32(89, 44, 112)
    klr(117) = _RGB32(112, 44, 89)
    klr(118) = _RGB32(72, 30, 30)
    klr(119) = _RGB32(135, 93, 35)
    klr(120) = _RGB32(192, 156, 135)
    klr(121) = _RGB32(255, 154, 53)
    klr(122) = _RGB32(195, 195, 150)
    klr(123) = _RGB32(255, 255, 178)
    klr(124) = _RGB32(80, 0, 0)
    klr(125) = _RGB32(0, 80, 0)
    klr(126) = _RGB32(0, 0, 80)
    klr(127) = _RGB32(200, 200, 150)
    klr(128) = _RGB32(40, 40, 40)
    klr(129) = _RGB32(0, 0, 240)
    klr(130) = _RGB32(0, 160, 0)
    klr(131) = _RGB32(90, 217, 217)
    klr(132) = _RGB32(255, 90, 90)
    klr(133) = _RGB32(193, 90, 193)
    klr(134) = _RGB32(149, 80, 80)
    klr(135) = _RGB32(212, 212, 212)
    klr(136) = _RGB32(150, 150, 150)
    klr(137) = _RGB32(60, 128, 255)
    klr(138) = _RGB32(150, 255, 150)
    klr(139) = _RGB32(190, 240, 255)
    klr(140) = _RGB32(255, 128, 128)
    klr(141) = _RGB32(250, 170, 250)
    klr(142) = _RGB32(255, 255, 110)
    klr(143) = _RGB32(250, 250, 245)
    klr(144) = _RGB32(144, 115, 57)
    klr(145) = _RGB32(158, 126, 63)
    klr(146) = _RGB32(115, 144, 57)
    klr(147) = _RGB32(126, 158, 63)
    klr(148) = _RGB32(115, 57, 144)
    klr(149) = _RGB32(144, 57, 115)
    klr(150) = _RGB32(84, 36, 36)
    klr(151) = _RGB32(140, 98, 32)
    klr(152) = _RGB32(204, 175, 157)
    klr(153) = _RGB32(255, 177, 100)
    klr(154) = _RGB32(210, 210, 150)
    klr(155) = _RGB32(255, 255, 190)
    klr(156) = _RGB32(90, 0, 0)
    klr(157) = _RGB32(0, 90, 0)
    klr(158) = _RGB32(0, 0, 90)
    klr(159) = _RGB32(200, 200, 200)
    klr(160) = _RGB32(50, 50, 50)
    klr(161) = _RGB32(0, 0, 250)
    klr(162) = _RGB32(0, 180, 0)
    klr(163) = _RGB32(120, 217, 217)
    klr(164) = _RGB32(255, 120, 120)
    klr(165) = _RGB32(193, 120, 193)
    klr(166) = _RGB32(149, 100, 100)
    klr(167) = _RGB32(216, 216, 216)
    klr(168) = _RGB32(160, 160, 160)
    klr(169) = _RGB32(75, 128, 255)
    klr(170) = _RGB32(160, 255, 160)
    klr(171) = _RGB32(200, 250, 255)
    klr(172) = _RGB32(255, 136, 136)
    klr(173) = _RGB32(250, 180, 250)
    klr(174) = _RGB32(255, 255, 120)
    klr(175) = _RGB32(250, 250, 250)
    klr(176) = _RGB32(176, 140, 70)
    klr(177) = _RGB32(190, 152, 76)
    klr(178) = _RGB32(140, 176, 70)
    klr(179) = _RGB32(152, 190, 76)
    klr(180) = _RGB32(140, 70, 176)
    klr(181) = _RGB32(176, 70, 140)
    klr(182) = _RGB32(96, 40, 40)
    klr(183) = _RGB32(150, 108, 40)
    klr(184) = _RGB32(217, 196, 183)
    klr(185) = _RGB32(255, 192, 130)
    klr(186) = _RGB32(225, 225, 150)
    klr(187) = _RGB32(255, 255, 202)
    klr(188) = _RGB32(100, 0, 0)
    klr(189) = _RGB32(0, 100, 0)
    klr(190) = _RGB32(0, 0, 100)
    klr(191) = _RGB32(250, 200, 200)
    klr(192) = _RGB32(60, 60, 60)
    klr(193) = _RGB32(20, 20, 250)
    klr(194) = _RGB32(0, 200, 0)
    klr(195) = _RGB32(150, 217, 217)
    klr(196) = _RGB32(255, 150, 150)
    klr(197) = _RGB32(193, 150, 193)
    klr(198) = _RGB32(149, 120, 120)
    klr(199) = _RGB32(228, 228, 228)
    klr(200) = _RGB32(170, 170, 170)
    klr(201) = _RGB32(90, 128, 255)
    klr(202) = _RGB32(170, 255, 170)
    klr(203) = _RGB32(200, 255, 255)
    klr(204) = _RGB32(255, 148, 148)
    klr(205) = _RGB32(255, 185, 255)
    klr(206) = _RGB32(255, 255, 140)
    klr(207) = _RGB32(255, 250, 250)
    klr(208) = _RGB32(208, 166, 83)
    klr(209) = _RGB32(222, 177, 88)
    klr(210) = _RGB32(166, 208, 83)
    klr(211) = _RGB32(177, 222, 88)
    klr(212) = _RGB32(166, 83, 208)
    klr(213) = _RGB32(208, 83, 166)
    klr(214) = _RGB32(108, 44, 44)
    klr(215) = _RGB32(155, 113, 44)
    klr(216) = _RGB32(223, 206, 196)
    klr(217) = _RGB32(255, 220, 185)
    klr(218) = _RGB32(240, 240, 150)
    klr(219) = _RGB32(255, 255, 234)
    klr(220) = _RGB32(120, 0, 0)
    klr(221) = _RGB32(0, 120, 0)
    klr(222) = _RGB32(0, 0, 120)
    klr(223) = _RGB32(250, 250, 200)
    klr(224) = _RGB32(70, 70, 70)
    klr(225) = _RGB32(40, 40, 250)
    klr(226) = _RGB32(0, 220, 0)
    klr(227) = _RGB32(150, 217, 217)
    klr(228) = _RGB32(255, 180, 180)
    klr(229) = _RGB32(193, 180, 193)
    klr(230) = _RGB32(149, 140, 140)
    klr(231) = _RGB32(240, 240, 236)
    klr(232) = _RGB32(180, 180, 180)
    klr(233) = _RGB32(115, 128, 255)
    klr(234) = _RGB32(190, 255, 190)
    klr(235) = _RGB32(220, 255, 255)
    klr(236) = _RGB32(255, 160, 160)
    klr(237) = _RGB32(255, 200, 255)
    klr(238) = _RGB32(255, 255, 160)
    klr(239) = _RGB32(255, 255, 250)
    klr(240) = _RGB32(240, 192, 96)
    klr(241) = _RGB32(254, 203, 101)
    klr(242) = _RGB32(192, 240, 96)
    klr(243) = _RGB32(203, 254, 101)
    klr(244) = _RGB32(192, 96, 240)
    klr(245) = _RGB32(240, 96, 192)
    klr(246) = _RGB32(120, 48, 48)
    klr(247) = _RGB32(165, 120, 45)
    klr(248) = _RGB32(227, 214, 204)
    klr(249) = _RGB32(255, 235, 195)
    klr(250) = _RGB32(255, 255, 150)
    klr(251) = _RGB32(255, 255, 249)
    klr(252) = _RGB32(140, 0, 0)
    klr(253) = _RGB32(0, 140, 0)
    klr(254) = _RGB32(0, 0, 140)
    klr(255) = _RGB32(250, 250, 250)
    For k = 0 To 255
        _PaletteColor k, klr(k)
    Next k

End Sub
Reply


Messages In This Thread
a simple Palette Builder - by James D Jarvis - 02-01-2023, 04:42 PM
RE: a simple Palette Builder - by James D Jarvis - 02-02-2023, 01:50 AM
RE: a simple Palette Builder - by bplus - 02-02-2023, 11:11 AM
RE: a simple Palette Builder - by James D Jarvis - 02-02-2023, 02:09 PM
RE: a simple Palette Builder - by SMcNeill - 02-02-2023, 12:21 PM
RE: a simple Palette Builder - by James D Jarvis - 02-02-2023, 02:26 PM
RE: a simple Palette Builder - by James D Jarvis - 02-02-2023, 02:17 PM
RE: a simple Palette Builder - by mnrvovrfc - 02-02-2023, 06:21 PM



Users browsing this thread: 6 Guest(s)