Title: Make more colorful rings of nested circles in Python
This example combines my previous two:
Drawing Spirals
The following draw method orchestrates the drawing with the most interesting section highlighted in blue.
def draw(self, *args):
'''Draw a big circle with three smaller circles inside it.'''
# Create a PIL image to hold the drawing.
wid = hgt = 400
transparent = (0, 0, 0, 0)
image = Image.new('RGBA', (wid, hgt), transparent)
dr = ImageDraw.Draw(image)
n = int(self.num_circles_var.get())
margin = 10
R = min(wid, hgt) / 2 - margin
cx = wid / 2
cy = hgt / 2
# Make the colors.
num_color_sets = int(self.num_color_sets_var.get())
num_colors = math.ceil(n / num_color_sets + 1)
from_hsl = (0, 1.0, 0.5)
to_hsl = (360, 1.0, 0.5)
hsl_colors = hsl_make_color_range(from_hsl, to_hsl, num_colors)
rgb_colors = hsl_list_to_tkinter_list(hsl_colors)
# Make the colors reverse.
rgb_colors = rgb_colors + rgb_colors[-2:0:-1]
# Set the outline color.
if self.outline_var.get():
outline = 'black'
else:
outline = None
# Draw the circles.
max_depth = int(self.num_rings_var.get())
draw_nested_circles(dr, max_depth, outline, rgb_colors, False,
n, cx, cy, R)
# Display the image.
self.canvas.config(width=wid, height=hgt)
self.photo_image = ImageTk.PhotoImage(image)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo_image)
# Save the image.
image.save('circles.png')
The code creates a new image as in previous examples. The blue code then divides the number of circles in each ring by the number of color sets. (The number of color sets if the number of times the circles should repeat all of the colors. For example, is the number of sets is 4, then the picture will repeat the colors red through blue 4 times.)
The call to hsl_make_color_range make a list of colors ranging from red to blue and the hsl_list_to_tkinter_list call converts the list from HSL to RGB.
As in the earlier post, the program adds on the reverse of the list so the colors rage from red to blue and back to red.
The rest of the method is as before. It calls draw_nested_circles to draw the circles and then displays the result.
Conclusion
I think that's enough digression into colored rings of circles. In my next post, I'll finally get to the somewhat related topic of Apollonian circle packing.
Meanwhile, download the example to experiment with it and to see additional details.
|