Skip to content

Instantly share code, notes, and snippets.

@lucatronica
Created April 6, 2020 01:07
Show Gist options
  • Save lucatronica/6dc7fae058440ab208d99584fe314e5c to your computer and use it in GitHub Desktop.
Save lucatronica/6dc7fae058440ab208d99584fe314e5c to your computer and use it in GitHub Desktop.
-- In RGB displays, each pixel contains 3 subpixels for red, green and blue.
-- The intensity of each subpixel can be changed independently.
-- Mixing different intensities of each allows for a large range of colors.
-- In this program, we'll use a similar approach.
-- Except our RGB subpixels can only be on or off, which limits us to 8 colors.
-- We can treat a PICO-8 color index like an RGB pixel by treating the first 3
-- bits as flags for each RGB subpixel.
-- Here's where we'll implement that model.
-- We're mapping each index to a color, based on how the RGB subpixels should mix.
for i=1,8 do
pal(i,({
-- 0 [000] black
8, -- 1 [001] red
11, -- 2 [010] green
10, -- 3 [011] green + red = yellow
140, -- 4 [100] blue
14, -- 5 [101] blue + red = magenta
12, -- 6 [110] blue + green = cyan
7 -- 7 [111] blue + green + red = white
})[i],1)
end
-- Draw loop!
::_::
cls()
-- Print some text in the top left.
-- 8 appears black since we used i=1,8 in the pallete code above.
?"#pico8",0,0,8
-- Iterate the three layers: red, green, blue.
for i=0,2 do
-- Z depth factor
k=.25+i/60+cos(t()/8)/5
-- Iterate over the pixels of the text.
for y=0,5,.25 do
for x=0,23,.25 do
if pget(x,y)>0 then
-- Destination pixel
u=64+(x-11)/k
v=64+(y-3)/k
-- So how do we mix two color indexes? It should look like:
-- red [001] + red [001] = red [001]
-- red [001] + green [010] = yellow [011]
-- This kind of operation is called bitwise OR.
-- In PICO-8 it's provided by the bor() function.
-- 2^i is the color of the current layer.
-- We want to mix this color with the color currently at the
-- destination pixel.
pset(u,v,bor(2^i,pget(u,v)))
end
end
end
end
flip()
goto _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment