sorry I didn't answered you earlier, about your last question as far I can remember the each polygon data have some value(bit shift) that tell which palette to use in which polygon, that's all I can remember...
Here's the source BTW:
@kian that looks nice!