[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Color an Apollonian circle packing fractal in Python

[A colored Apollonian circle packing fractal drawn in Python] My post Draw an Apollonian circle packing fractal in Python explained how you can draw an Apollonian circle packing fractal. It's intricate and cool and everything, but it's not very colorful.

This post explains one way you can color it.

Picking Colors

Coloring the circles is easy; just use the create_oval method's fill and outline parameters when you draw the circles.

I tried this several times with different color schemes using ranges of colors and shades of one color, but the results weren't very interesting. I got a better result when the colors are contrasting.

Eventually I decided to provide a few color schemes. Use the combo box to select Palette (a fixed color palette), Random (each circle gets a random color), or Shade (shades of fuchsia).

Feel free to experiment with other color schemes if you're bored.

Coloring Circles

The following code shows the new example's draw method with the new bits of code highlighted in blue.

def draw(self, *args): '''Draw a big circle with three smaller circles inside it.''' self.canvas.delete(tk.ALL) depth = int(self.depth_var.get()) self.canvas.update() wid = self.canvas.winfo_width() hgt = self.canvas.winfo_height() margin = 10 radius = min(wid, hgt) / 2 - margin cx = wid / 2 cy = hgt / 2 # Define colors. if self.color_mode_var.get() == 'Shades': num_colors = depth + 2 f = 255 / num_colors colors = [f'#{int(d * f):02x}00{int(d * f):02x}' for d in range(num_colors, 0, -1)] else: colors = [ 'red', 'light green', 'blue', 'orange', 'light blue', 'yellow', 'pink', 'cyan', 'fuchsia', 'white' ] num_colors = len(colors) # See if we should outline the circles. if self.outline_var.get(): outline = 'black' else: outline = '' # Get and draw the circles. circles = make_apollonian_circle_packing(depth, cx, cy, radius) for x, y, r, depth in circles: rect = (x-r, y-r, x+r, y+r) if self.color_mode_var.get() == 'Random': fill = random.choice(colors) else: fill = colors[depth % num_colors] self.canvas.create_oval(rect, outline=outline, fill=fill)

The code clears the program's canvas as before. It then checks the combo box to see what color mode you want to use. If you selected Shades, the code creates a list of shades of fuchsia. (Feel free to change the color if you like.)

If you did not pick Shades, the code creates a fixed color palette with contrasting colors.

Next, the code sees if you checked the Outline box and sets the outline property to either black or the empty string to draw no outline.

The code then calls make_apollonian_circle_packing to get the fractal's circles and it loops through the results drawing the circles.

If you selected the Random color mode, the program sets fill to a random color. If you did not pick Random, the code sets fill to a color in the colors list that depends on the circle's depth of recursion. (This is why the code described in the previous example saved each circle's depth. Almost like I knew this was coming, huh?)

Having set the outline and fill colors, the method draws the circle.

Conclusion

Coloring an Apollonian circle packing with random colors or a fixed contrasting color palette produces a nice result. If you want to study the recursive order in which the circles are created, though, shades of a single color are more useful. In the picture below that uses shades of fuchsia, the circles with the lighter colors are created first.

In my next (and probably final) Apollonian circle packing post, I'll show how you can fill the circles with images instead of colors.

[A randomly colored Apollonian circle packing fractal drawn in Python] [An Apollonian circle packing fractal colored in shades of fuchsia in Python]
© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.