I just figured out how to make spirals that cycle through every color in the rainbow! This blew my mind when I realized you could use math to calculate smooth color transitions.
A mesmerizing spiral that starts from the center and expands outward, but here's the cool part - it smoothly transitions through the entire color spectrum! Red flows into orange, orange into yellow, all the way through the rainbow and back to red. The colors shift so smoothly it looks like a real rainbow was bent into a spiral.
python rainbow_spiral.pyThat's it! Python's turtle module is built-in, so no installing anything. The spiral draws itself in real-time and you can watch every color transition happen. Click anywhere on the window when you're done admiring it.
The secret is in the color mathematics! I discovered you can use trigonometric functions (cos and sin) to create smooth color transitions. Here's what happens:
- The spiral part - Same concept as my galaxy spiral, but now I'm tracking each step number
- The color magic - Each step gets converted to a "hue" value between 0 and 1
- RGB calculation - Three different cosine waves (offset by 120 degrees each) create the red, green, and blue components
- Smooth transitions - Because cosine waves flow smoothly, the colors blend perfectly!
The math behind this creates what's called a "sinusoidal color space" - basically using wave equations to generate rainbow colors. I had no idea you could make rainbows with math equations!
Want to experiment? Here are some fun modifications:
- Change
distance += 0.8to make the spiral expand faster or slower - Modify the
angle = 91- try 89, 93, or even 120 for completely different spiral shapes - Adjust
range(360)to make longer or shorter spirals (try 720 for two full rainbows!) - Change
rainbow_turtle.width(2)to make thicker or thinner rainbow lines - Replace the RGB formula with simpler colors to see the difference
This was my first time combining color theory with programming! I learned that:
- Colors can be calculated mathematically - you don't have to pre-define every color
- Cosine waves create smooth transitions - way smoother than just incrementing numbers
- RGB values work best between 0 and 1 in Python turtle graphics
- Phase shifting (the
+ 2π/3part) is how you get different color channels
Going from my basic galaxy spiral to this rainbow version taught me that adding one new concept (color mathematics) can make something look completely different. The same spiral pattern, but now it's a rainbow!