I had the same problem while drawing the map-overview in my roguelike game. What I did was make a large blank image, the size of the map, and a another image with 1-by-1 boxes containing all the possible values for color.
Then I used the copy facility of the photo widget to copy the right color into the right spot on the map image. So in your algorithm just replace the createRectangle call with a call to Photo->copy. It was a lot faster and less memory intensize than the rectangle method.