Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API function pew.palette([pal], [offset=0]) that takes a sequence of R, G, B, R, G, B... byte values for up to 256 colors (blue component is ignored here) and an optional offset to cyclically shift the palette. Passing no palette preserves the previous one and only adjusts the offset, which may be useful for some animations. Passing an empty palette or None sets a default palette (described at https://hackaday.io/project/167369-picopew/log/171107-256-colors). Changes take effect at the next pew.show(). The palette is preserved by pew.init(), much like pew.brightness(), so that a choice made by the user before starting a program stays in effect, unless the program explicitly sets its own palette. Program tint.py is provided to allow selection of some offsets in the default palette that match the color schemes of other PewPew devices. Generating the default palette in code is a lot more compact that including it as a bytes literal. Zlib compression and Base64 encoding would be a little better still but I did not examine their memory and time requirements.
- Loading branch information
Showing
2 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
import pew | |||
|
|||
pew.init() | |||
choice = {64: 1, 80: 2, 96: 3, 112: 4}.get(pew._paletteoffset, 0) | |||
pew.palette(None) | |||
screen = pew.Pix() | |||
|
|||
keyhistory = 0 | |||
def keyevents(): | |||
global keyhistory | |||
keys = pew.keys() | |||
events = keys & (~keyhistory | (keyhistory & (keyhistory >> 8) & (keyhistory >> 16) & (keyhistory >> 24))) | |||
keyhistory = ((keyhistory & 0x3FFFFF) << 8) | keys | |||
return events | |||
|
|||
def paint(): | |||
if choice == 0: | |||
for i in range(3): | |||
screen.box(i+1, 2*i+1, 1, 2, 2) | |||
else: | |||
for i in range(3): | |||
screen.box(32+i+1, 2*i+1, 1, 2, 1) | |||
for c in range(1, 5): | |||
if choice == c: | |||
for i in range(3): | |||
screen.box(48+c*16+i+1, 2*i+1, 1+c, 2, 2) | |||
else: | |||
screen.box(48+c*16+1, 1, 1 + c + (0 if choice > c else 1), 6, 1) | |||
pew.show(screen) | |||
|
|||
paint() | |||
while pew.keys(): | |||
pew.tick(0.1) | |||
while True: | |||
k = keyevents() | |||
if k & pew.K_UP: | |||
choice = (choice - 1) % 5 | |||
if k & pew.K_DOWN: | |||
choice = (choice + 1) % 5 | |||
if k & pew.K_O: | |||
pew.palette(offset = (0, 64, 80, 96, 112)[choice]) | |||
break | |||
paint() | |||
pew.tick(0.06) |