I think that the TerrainHeight() command returns values between 0 and 1, so in your save terrain function you need to multiple it with 255 to get a useable color out of it, Only a guess though:
Anyway try changing the line:
To:
Hope this helps.
Anyway try changing the line:
hgt# = Floor(TerrainHeight#(terra, x, y))
To:
hgt# = Floor(TerrainHeight#(terra, x, y))*255
Hope this helps.