Log In  


Cart #colortablesexmaple-0 | 2025-05-28 | Embed ▽ | License: CC4-BY-NC-SA
24


Hi!

TLDR:

Color tables are basically free to use because the gfx pipeline uses one by default. By modifying it we can achieve effects-in this case we add 2 colors that allow us to draw "shadows" and add lit areas. Copy the function below and give it a 64x64 sprite as a color table. Now draw shapes with color 32 and 33 to apply the effects.

My attempt at explaining color tables

I've struggled with understanding color tables, but thanks to @washburnello I finally got it. He is using a sprite to visually represent the color table, which makes it much easier to plan.
This small function allows you to do the same and use a 64x64 sprite to visually represent and plan the color table. It then memmaps it into the appropriate spot in memory.
The color table sprite looks like this:
On the vertical axis are the colors that you are drawing with. The horizontal axis are the colors that are already on screen at whatever position you're drawing. Their intersection is the color that will get drawn instead.
The first 32 colors will always get drawn as themselves(in this case) with the exception of color 0, which is always mapped to the color that is already on screen. This is how Picotron makes transparency work.
Two colors got added: Color 32 and 33 have alterations to achieve the lightning effects. Color 34 is another set of our original colors as a starting point for your own custom effect color.

This is the function:

function apply_color_table(color_table_sprite)
	local sprite=get_spr(color_table_sprite)
	--copy the sprite into the address 0x8000 in memory
	memmap(sprite,0x8000)
	--poke the bit that makes it work for shapes(circ,rect etc.), the bit for sprites
	--is already set by default.
	poke(0x550b,0x3f)
	--the color table got copied and will be used. This is the same color table 
	--that pal() modifies.
end

The function takes the sprite, which is userdata, and puts it in the appropriate spot in memory. It also does a poke poke(0x550b, 0x3f) to make the gfx pipeline apply the color table to shapes like rectfill etc.

The cart provided above will result in this effect:

Picotron has the ability to handle 64 colors, but only 32 are being used by default. The color table allows us to define color 32 and 33 as our "shadow" or "light" color respectively. Play around with the color table, switch colors around, add more shapes to the code, make them overlap, have fun!

I enjoyed playing around with it a lot and came up with a little sample scene. All the lightning effects were achieved using nothing but shapes drawn with color 33 and 32.

EDIT 31/5/25

This function allows you to change just one specific row of your colortable.

function apply_colortable_row(color_table_sprite,color_row)
		local sprite=get_spr(color_table_sprite)
		local address=0x8000+color_row*64
		for x=0,63 do
				c=sprite:get(x,color_row)
				poke(address+x,c)
		end
		poke(0x550b,0x3f)
end
24


It's worth highlighting that in order for colour tables to work with shapes, you must
poke(0x550b, 0x3f).


The function does exactly that, but I will also add the poke to the description in this post, for now I had just mentioned it.


1

i love how, even though picotron is here, we still use the old pico-8 colors. pico-8 shall never die!


Well, this first half of the inbuilt palette is the Pico-8 palette.


I added a version that allows you to change just a specific row of the colortable - useful for when you play around with the pal() function in other parts of your code. Keep in mind that calling pal() without any arguments will reset your colortable!

function apply_colortable_row(color_table_sprite,color_row)
		local sprite=get_spr(color_table_sprite)
		local address=0x8000+color_row*64
		for x=0,63 do
				c=sprite:get(x,color_row)
				poke(address+x,c)
		end
		poke(0x550b,0x3f)
end

1

Really good write up!
Thanks for posting this here @taxicomics <3


Edit: I certainly misunderstood how memmap works. This... kinda makes sense.


Original post:

I was playing around with this code, and noticed an interesting behavior/bug:

If you call apply_color_table() in _init() AND have at least 1 pal() call in _draw(), the color table is broken, all shapes will be drawn in black.

Reproducible code below. If you comment the lines 12 or 19, it works as expected.

-- Color Table minimal setup
-- by taxicomics & washburnello

function apply_color_table(color_table_sprite)
	local sprite=get_spr(color_table_sprite)
	memmap(sprite,0x8000)
	poke(0x550b,0x3f)
end

function _init()
	-- COMMENT THIS LINE
	apply_color_table(1)
end

function _draw()
	cls()

	-- OR THIS LINE
	pal()

	-- Make sure that the color table is always correct
	apply_color_table(1)

	for x=0,32 do
		for y=0,16 do
			rectfill(x*16,y*16,x*16+16,y*16+16,(x+y)%16)
		end
	end	

	local pos_x=120+cos(t()/5)*24
	local pos_y=130+sin(t()/5)*24

	circfill(pos_x,pos_y-24,64,32)
	circfill(pos_x+240,pos_y-24,64,33)
	circfill(pos_x,pos_y+38,64,32)
	circfill(pos_x+240,pos_y+38,64,33)
end

1

You should clean up your memmaps by unmapping the color tables when you're done with them. The code above was failing because the color table wasn't getting unmapped and pal was overwriting the data in the userdata.


1

Thanks for this tutorial, it helped me with my understanding of the gfx pipeline.

Like abledbody said, unmap should be called after using the color table and I think it's better to also reset the 0x550b address to 0x00.

I finally changed the way to manage this in order to copy the sprite only once in memory, by using the color table 2.

In the _init :

tools = {
	col_tab_2_addr = 0xa000,
	col_tab_2_bit = 0x80,
	col_tab_2_dark = 32
}
function init_color_table(color_table_sprite)
	local table_sprite = get_spr(color_table_sprite)
	table_sprite:poke(tools.col_tab_2_addr) -- copy sprite into color table 2
end

Then when I need to use the color 32 or 33.

function apply_color_table()
	poke(0x5508, 0xbf) -- set read mask to observe the bit 7 (which allows pointing to table 2) + color bits
	poke(0x550b, 0x3f) -- as previously
end

The cleaning function:

function clear_color_table()
	poke(0x5508, 0x3f)
	poke(0x550b, 0x00)
end

And using it like this :

circfill(inputs.mouse_x,
	inputs.mouse_y + self.lens_calibration,
	self.lens_radius,
	0x800000000 | tools.col_tab_2_bit | tools.col_tab_2_dark)

Warning : pal(0) seems to reset the color table.



[Please log in to post a comment]