What is the reason (in canon) for the different colors of lightsabers? I know about purple being specially chosen by the actor Samuel L. Jackson since it's his favorite color, but is there a reason or purpose for the colors, other than "red=sith, blue/green=jedi"?
First, there is nothing that was told in the movies about the lightsaber colors, so all information on the subject came from extended universe.
The color of the lightsaber come from the crystal used in it's construction. It's a personal choice based on multiple factor like crystal availability, traditional color for a Specialization (undermentioned as class) or just a personal color preference.
Out of universe, the Blue / Red distinction was made during post-production when the lightsaber blades were being rotoscoped, as the original blades were simply white. The color choice might have been influenced by the political climax, the red color being traditionally associated with Soviets and blue with Americans, but also note than the colors reversed for blasters and spaceships. Luke's green lightsaber was added late in post-production of the Return of the Jedi in order to better stand out against the blue sky of Tatooine in outdoor scenes. So all those signification / explanation are all retcon.
I made a digest of the meaningful passages of the Lightsaber crystal entry on Wookieepedia (emphasis mine).
While the Jedi used natural crystals, the Sith often used red-hued synthetic crystals. Sith-made synthetic crystals create a slightly more powerful blade when energized by the dark side of the Force, occasionally demonstrating the ability to "break" Jedi lightsaber blades, disrupting the energy circuit and shorting out the weapon. However, this rarely happened, and as synthetic crystals demonstrated a slightly lower amount of maneuverability, this advantage was very slim.
Around 4,000 BBY, lightsaber crystals were used to indicate a Jedi's chosen class. Blue indicated a Jedi Guardian, a Jedi who used the Force on a more physical level. Green indicated a Jedi Consular, a Jedi who preferred to reflect on the mysteries of the Force and fight the dark side at its heart. Yellow indicated a Jedi Sentinel, a Jedi who honed their skills in a balance of combat and scholarly pursuits.
This distinction fell out of practice in later years when most Jedi used blue or green crystals, based on the supply on the various source worlds. Purple, yellow, and orange crystals were still used by some Jedi until the the fall of the Jedi Order, but they were exceedingly rare, and often passed down through generations.
For millennia, the Jedi Order lit lightsabers in every color and hue imaginable. However, following the Battle of Ruusan, the hues of most available crystals had been limited to either blue or green.
For the out of universe section, this quote is taken form Lightsaber Entry, Behind the scenes - Color section on Wookieepedia (emphasis mine)
Lightsabers depicted in the first two released films, Star Wars Episode IV: A New Hope and Star Wars Episode V: The Empire Strikes Back, had blades that were colored either blue (for the Jedi) or red (for the Sith). This color difference was a decision during post-production when the lightsaber blades were being rotoscoped, as the original blades were simply white. In Star Wars Episode VI: Return of the Jedi, Luke Skywalker's newly-constructed lightsaber was colored blue during the initial editing of the film, and appears so in both an early movie trailer and the official theatrical posters, but in the final film, it was ultimately colored green in order to better stand out against the blue sky of Tatooine in outdoor scenes. It also appeared as green in re-release posters.
Green would become another standard blade color for Jedi lightsabers in the Star Wars prequel trilogy. Mace Windu's amethyst-bladed lightsaber, as first seen in Attack of the Clones, was a personal request from actor Samuel L. Jackson as a way to make his character stand out among other Jedi. Jackson's favorite color is purple and he frequently requests the characters he plays to use an item of the color.
Various blade colors appear in the Expanded Universe and in other Star Wars products. The original Kenner figure of Luke Skywalker in his Tatooine costume from Star Wars was released with a yellow-bladed lightsaber. A multitude of other colors have since been used, including orange, silver, cyan, viridian, gold, bronze, and even black.
Canon? Don't know how canon this is, but at least in the video game Knights of the Old Republic, the color was influenced by crystals added to a lightsaber which changed the saber's properties. (Giving additional damage to specific enemies, giving general damage boosts and so on.)
Lightsaber color comes from the sort of crystal focus the lightsaber has, as DavRob mentions, red are synthetic and blue and green are common, with a number of other rarer colors, like purple, showing up in various places. The mention of rare crystals being passed down through the generations comes from kights of the old republic era jedi actually having kids occasionally, with an example of this being the Sunrider family.
The color of a lightsaber appears to come from the Force. As relayed by the Databank:
A lightsaber crystal is colorless until first attuned and connected to a Jedi – at which times it glows either blue or green, or in some rare instances, another shade. From that point on, it retains that hue.
Other works confirm this:
The crystals grew. Clear as ice and cold until they found the hands that waited for them, they added structure in an ordered way, one prism at a time. And while they grew, they waited.
So basically, there’s an interaction between Jedi, crystal, and Force that influences the color of the crystal, and thus the blade. This explains the standard blue and green blades, as well as unusual variations such as Mace Windu’s purple lightsaber. This may also explain the Darksaber, a rare black-bladed lightsaber that was stolen from the Jedi temple by Mandalorians.
The Sith, on the other hand, create uniformly red crystals by imposing their will on stolen (and perhaps occasionally colorless) kyber crystals:
Dark crystals were made, too, but not in that holy place. They were plundered from their rightful bearers and corrupted by the hands that stole them. Even rock could be changed by the power of the Force, bleeding alterations until their color was the deepest red. The balance was finely staged between the two, light and dark, and it took very little to upset it.
The language used seems to suggest the typical Sith versus Jedi dichotomy, of attuning oneself to the crystal versus forcing one’s will upon it.
The new canon also incorporates synthetic kyber crystals. These are green, and would presumably produce a green lightsaber. This would, however, be a pretty bad idea, since they are highly explosive.
There’s also the odd case of yellow lightsabers, which are wielded by the Jedi temple guards:
An ancient, honored heritage within the hallowed halls of the Jedi Temple was the role of the Jedi Temple Guard. Reporting to Cin Drallig, head of security, the Temple Guards were anonymous sentinels plucked from the ranks of the Jedi as part of a Knight’s ongoing commitment to the Order. A Jedi Temple Guard would disappear inside the formal robes and identity-concealing masks, the ultimate expression of emotional detachment required in service to a higher calling. Temple Guards carried imposing lightsaber pikes – thick, double-bladed weapons that produced a rare and distinctive yellow blade.
It’s a little harder to explain why all the guards have the same saber color, since the process of attunement is quite individual, and, moreover, the guards are chosen from the ranks of the Jedi, presumably having already attuned to a lightsaber. They likely cannot be using synthetic crystals, since those appear to be unstable in the new canon. They are likely not imposing their will, as a Sith would. The only remaining likely possibility (though not confirmed in any canon) is that they somehow reach a state of mind where attuning to a kyber crystal will produce a yellow color. It is also not impossible that there are naturally occurring kyber crystals with yellow impurities.
Finally, a white lightsaber can also be obtained. Ahsoka Tano managed this by eliminating the Dark-side induced red color from an Inquisitor’s saber:
“I’ve never seen white ones before,” Bail mused.
“They used to be red,” Ahsoka said. “When the creature had them, they were red. But I heard them before I ever saw him on Raada, and knew that they were meant for me.”
“You changed their nature?” he asked.
“I restored them,” Ahsoka replied. “I freed them. The red crystals were corrupted by the dark side when those who wielded them bent them to their will. They call it making the crystal bleed. That’s why the blade is red.”
It’s uncertain whether this could be done with a colorless crystal by not attuning at all, or if it was a consequence of the specific, unusual way Ahsoka created her lightsabers.
Relation to the old (Legends) canon
The differences are enormous. Few things have changed so much between Legends and Disney canon, with the possible exception of Palpatine actually being dead. In the old EU, now Legends, the red (and occasionally orange) color of Sith lightsabers was a consequence of their being made of synthetic kyber crystals. That’s now out the window, despite what any other books may say:
Luke’s lightsaber was also synthetic, but not red.
Further, instead of being colorless, lightsaber crystals often seemed to have a natural color.
no clue how "canon" my answer will be but I will try.
Movies to identify things for the audience....red was sith/bad guys, blue was jedi/good guys green was only used to signify jedi masters and mace windu got purple because Samuel L Jackson did the movie for free or little pay and asked for the color...producers made it happen and someone somewhere had to come up with an answer...so purple is a mixture of red and blue or a balance of dark and light. Go Windu you confused Jedi!
Games from way back when had a book out as game reference for the the table top gamers of the 80s and gave no specification of color and why it was colored the way it was with the exception that it revolved around the crystal in the handle and each crystal gave bonuses for certain things. There was even a last choice on the table to roll that was a rainbow gem and allowed a rainbow of colors and bonuses....yes difficult to find and so on and so forth...I doubt anyone considers that old book and info as canon but it is interesting when you run across it again later.
And if you were a kid like me when the films came out the 4" 'action figures' were the thing to have, but being the young age I was I bit Leia's head off and chewed Luke's lightsaber to a flattened thing....the action figures light saber was yellow, darth vader's was red and if you were lucky enough to be given a Obi Wan one...it was blue. I was not that lucky.