From the "Encyclopedia of American Food and Drink" by John F. Mariani [Lebhar-Friedman:New York] 1999 (p. 175):
King cake. A brioche-style cake made during the Louisiana carnival season, beginning in January and ending at Mardi Gras...By tradition the cake contains a red bean (sometimes covered in gold or silver leaf) or a figurine of the baby Jesus. It is sold widely throughout Louisiana...the person who finds the bean or figurine is promised good luck. There are various stories of the origins of the cake, though most in some way derive from the legend of the Three Kings visiting the infant Jesus in Bethlehem, as described in the New Testament. In the first half of the 16th century France commemorated Kings' Day-the 12th day after Christmas-with a "Twelfth Night cake." A century later King Louis XIV took part in such a feast at which gateau des Rois ("Kings' cake") contained a hidden bean or ceramic figure, as it does to this day. Before the Civil War, American King cakes often contained gold, diamonds, or a valuable instead of beans; after the war, with the end of gala Creole balls in Louisiana, peas, beans, pecans, and coins were used, and in 1871 the tradition of choosing the queen of the Mardi Gras was determined by who drew the prize in the cake...The colors of purple (for justice), green (for faith), and gold (for power) that traditionally tint the cake's icing first appeared in 1872 after the Rex Krewe, a Mardi Gras parade
organization, chose those colors to celebrate that year's festival."