First and foremost, im not trying to sound mean. But do you understand my original post? If you dont, perhaps read it a few more times. I am not trying to belittle, im trying to see if my next explanation will make sense to you.
If you do understand, here is the short version.
You can take that whole code i posted above and copy it into S3D as you would based on the first layer to make the color change.
You can then re-copy and re paste the same code under the previous one once again and then change the layer number to what you want to make the next color change. You can do this as many times as you wish.
When its all said and done, all this code does, it goes into your Gcode, and enters certain code right before the layer you want.