So, as some of you other modders are aware, permanent transformation into other creatures during worldgen is finicky and difficult, often leading to crashes. I have collated several methods of doing it in such a way that these crashes don't happen, and have decided to organize them here for use by other modders.
NOTE: This is all the result of me fumbling through experimentation and my own experiences. I would say this is mostly correct and I am mostly certain these are the rules, and these are the rules I go by when making my own mods. However, I might be wrong. Testing this stuff is hard and what I think is 100% safe might only be 99% safe. Any sort of confirmation of one or the other would be greatly appreciated.
Terminology:
Color- a color as defined in the dwarf fortress descriptor_color_standard.txt file and associated files
Color set- the total amount of potential colors a given body part can have. All the skin colors a human has constitutes a color set
Color slot- a single instance of a color in a color set. BLUE:1:BLUE:1:BLUE:1 is three color slots, even if they are all filled with the same color.
A creature can be safely transformed into a new creature if one of these conditions are met
1. The transformed end result has no color sets in the creature file, and coloration is instead described in the description itself. Like werewolves and night trolls for example. A human, for example, has a color set for eyes, skin, and hair. If those are not present on the transformed creature, there will be no issue. All vanilla transformed creatures work like this.
2. The transformed end result has the same color sets and color slots within those sets as the creature to be transformed. For example, you could safely transform a human into a giant during worldgen because a human and a giant have the same amount of potential eye, hair, and skin colors and number of color slots/sets. You would have to add graying and white hair to the giant though, or remove graying from humans, since that counts towards the total amount of sets.
3. The transformed end result has different colors but the same amount of color slots in each set. For example, if you took a giant, changed their potential skin colors from this
[TL_COLOR_MODIFIER:BROWN:1:BURNT_UMBER:1:CINNAMON:1:COPPER:1:DARK_BROWN:1:DARK_PEACH:1:DARK_TAN:1:ECRU:1:PALE_BROWN:1:PALE_CHESTNUT:1:PALE_PINK:1:PEACH:1:PINK:1:RAW_UMBER:1:SEPIA:1:TAN:1:TAUPE_PALE:1:TAUPE_SANDY:1]
to this
[TL_COLOR_MODIFIER:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1]
a human could safely become one in worldgen, their skin changing to one of the three new colors, because the number of color slots for the skin of both creatures is the same. If you did this:
[TL_COLOR_MODIFIER:RED:1:YELLOW:1:GREEN:1]
and tried to transform a human into a giant with the above color set, the game would crash.
4. The transformed creature has a greater amount of color slots in a set than the creature it transformed into. For example, if we took the above red/yellow/green-skinned giant and added this:
[TL_COLOR_MODIFIER:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:RED:1:YELLOW:1:GREEN:1:PURPLE:1000000]
All humans transformed into giants would have red, yellow, or green skin, because those color slots are the ones equivalent to the color slot of the human creature, while purple is not. However, all natural-born giants would have purple skin because of how I tweaked the ratio to increase the odds of being born purple, and if you added another color past purple, no human would transform into a giant of that color either, but natural born giants could also be born with it.
5. The transformed creature has less color sets than the original, but the remaining sets still have a matching amount of slots. For example, lets take our colorful giants, and make them eyeless, and then delete the color set pertaining to eye coloration. A human could safely transform into one of these giants so long as the skin and hair still followed the rules described above. If the eye color set was kept but the amount of color slots were reduced, the game would crash, but eliminating all references to eye coloration does not cause a crash, and the same applies to any transformation of a creature into one with less color sets than the original. You could make a hairless, eyeless giant, with both color sets pertaining to those body parts removed, and so long as the remaining skin color set of the giant had an equivalent or greater amount of color slots as the human skin color set, it would be safe.
6. The transformed creature has different body parts, but the shares the amount of color slots between both sets. Let's say we then decided to take our colorful giants and replace their hair with feathers, and then replaced the hair color set with feathers so it looked like this:
[SET_TL_GROUP:BY_CATEGORY:ALL:FEATHER]
[TL_COLOR_MODIFIER:AMBER:1:AUBURN:1:BLACK:1:BROWN:1:BUFF:1:BURNT_SIENNA:1:BURNT_UMBER:1:CHARCOAL:1:CHESTNUT:1:CHOCOLATE:1:CINNAMON:1:COPPER:1:DARK_BROWN:1:DARK_CHESTNUT:1:DARK_TAN:1:ECRU:1:FLAX:1:GOLD:1:GOLDEN_YELLOW:1:GOLDENROD:1:LIGHT_BROWN:1:MAHOGANY:1:OCHRE:1:PALE_BROWN:1:PALE_CHESTNUT:1:PUMPKIN:1:RAW_UMBER:1:RUSSET:1:SAFFRON:1:SEPIA:1:TAN:1:TAUPE_DARK:1:TAUPE_GRAY:1:TAUPE_MEDIUM:1:TAUPE_PALE:1:TAUPE_SANDY:1]
[TLCM_NOUN:feathers:PLURAL]
[TL_COLOR_MODIFIER:GRAY:1]
[TLCM_NOUN:feathers:PLURAL]
[TLCM_TIMING:ROOT:40000:0:70000:0]
[TL_COLOR_MODIFIER:WHITE:1]
[TLCM_NOUN:feathers:PLURAL]
[TLCM_TIMING:ROOT:70000:0:90000:0]
A human turning into a feather-haired, colorful, eyeless giant would be fine, because as you can see, the amount of color slots for the feathers matches up with the amount of hair color slots humans have. Note how you also have to include graying hair if the original creature has it, but you can make it not appear by increasing the amount of years it takes to show up. Realistically no feathered giant should have gray or white feathers in-game because it would take too long.
In addition, you cannot transform a creature into another creature that has more color sets. If we gave our colorful, eye-having, feather-haired giants a layer of scales over their skin, and defined the color of the scales as we do with skin, then a human transforming into one would crash the game. However, a scaled, feather-haired, skin-having, eye-having giant being transformed into a human would not crash the game, because a creature with more color sets can safely transform into a creature with less color sets, provided the color slots of the remaining sets are equal to or greater than the color slots of the original sets.
Furthermore, a creature can transform more than once in worldgen, but ONLY ONCE in gameplay. If you make interactions so a human turns into an ogre who can turn into a giant, then it is possible for a human to become a giant in worldgen. However, a human could only become an ogre during gameplay, and would not transform again after becoming an ogre.
Lastly, though I am not entirely certain of this, I believe the tissues must match up when transformation happens. If you were to transform a human into another human, then for both creatures the hair set must be the first set, the skin color set the second, and eyes the third. if you switched the hair and eye color sets of the end result human around, I don't know what would happen, but it would probably result in a crash. So if you make a new creature, like a three-eyed, red-skinned feather-haired demigod that humans can transform into, then if you make his skin the first color set, it must have as many slots as the HAIR of humans, since that is the first color set humans have, and so on. The first color set of a creature must match the first color set as the transformation end result creature, the second to the second, and so on. Not matching tissue to tissue. This may benefit from testing however.