DECLARE SUB setPalette () DECLARE SUB loadPalette (filename AS STRING) DECLARE SUB savePalette (filename AS STRING) DECLARE SUB setColor (attr AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER) CONST DAC.PELMask = &H3C6 ' DAC (Digital/Analog Converter) PEL Mask Register CONST DAC.READ = &H3C7 ' DAC Read Port/DAC State Register CONST DAC.WRITE = &H3C8 ' DAC Write Port/DAC State Register CONST DAC.DATA = &H3C9 ' DAC Data Port ' Enter mode 13 (320x200, 256 colors) SCREEN 13 ' Display the whole color palette FOR y% = 0 TO 15 FOR x% = 0 TO 15 LINE (x% * 8, y% * 8)-STEP(7, 7), x% + (y% * 16), BF NEXT x% NEXT y% ' Save palette for later savePalette "VGA.PAL" ' Wait for user input COLOR 15: LOCATE 3, 25: PRINT "Press a key - 1": SLEEP ' Tweak attributes 254 and 255 to teal and orange setColor 254, 0, 29, 63 setColor 255, 63, 34, 0 ' Wait for user input COLOR 15: LOCATE 3, 25: PRINT "Press a key - 2": SLEEP ' Modify the whole palette setPalette ' Wait for user input COLOR 255: LOCATE 3, 25: PRINT "Press a key - 3": SLEEP loadPalette "VGA.PAL" ' load {filename} and apply 256-color palette SUB loadPalette (filename AS STRING) DIM clr AS STRING * 768 ' Palette buffer OPEN filename FOR BINARY AS #1 GET #1, , clr CLOSE #1 OUT DAC.PELMask, &HFF ' Mask all registers OUT DAC.WRITE, 0 ' Select first attribute FOR i% = 0 TO 767 ' Set all 768 components OUT DAC.DATA, ASC(MID$(clr, 1 + i%, 1)) NEXT i% END SUB ' save whole 256-color palette to {filename} SUB savePalette (filename AS STRING) DIM clr AS STRING * 768 ' Palette buffer OUT DAC.PELMask, &HFF ' Mask all registers OUT DAC.READ, 0 ' Select first attribute FOR i% = 0 TO 767 ' Read all 768 components MID$(clr, 1 + i%, 1) = CHR$(INP(DAC.DATA)) NEXT i% OPEN filename FOR BINARY AS #1 PUT #1, , clr CLOSE #1 END SUB ' {attr} is the color attribute index, must be 0 - 255 ' {r}, {g} and {b} are Red, Green and Blue intensity, must be 0 - 63 SUB setColor (attr AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER) OUT DAC.PELMask, &HFF ' Mask all registers OUT DAC.WRITE, attr ' Select attribute OUT DAC.DATA, r ' Feed red intensity OUT DAC.DATA, g ' Feed green intensity OUT DAC.DATA, b ' Feed blue intensity END SUB ' fill the whole palette with 64 shades of red, green, blue and grey SUB setPalette OUT DAC.PELMask, &HFF ' Mask all registers OUT DAC.WRITE, 0 ' Select first attribute ' Attributes 0 to 63: red shades FOR i% = 0 TO 63 OUT DAC.DATA, i% OUT DAC.DATA, 0 OUT DAC.DATA, 0 NEXT i% ' Attributes 64 to 127: green shades FOR i% = 0 TO 63 OUT DAC.DATA, 0 OUT DAC.DATA, i% OUT DAC.DATA, 0 NEXT i% ' Attributes 128 to 191: blue shades FOR i% = 0 TO 63 OUT DAC.DATA, 0 OUT DAC.DATA, 0 OUT DAC.DATA, i% NEXT i% ' Attributes 192 to 255: grey shades FOR i% = 0 TO 191 OUT DAC.DATA, i% \ 3 NEXT i% END SUB