[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: Make a list of color shades in Python

[10 shades of green created in Python]

This example shows how you can build a list of color shades in Python. Figuring out how to do this in an RGB (red, green, blue) color model is somewhat tricky, but it's trivial in the HSL (hue, saturation, lightness) color model: You simply make the lightness vary while keeping hue and saturation fixed.

Making Shades

The following hsl_make_shades function creates a list holding shades in the HSL color model.

def hsl_make_shades(h, s, l0, l1, num_shades): '''Make the indicated number of shades of this color.''' dl = (l1 - l0) / (num_shades - 1) shades = [ (h, s, l0 + (i * dl), 255) for i in range(num_shades) ] return shades

The function first calculates the difference in lightness needed to go from l0 to l1 in num_shades steps. It then uses a list comprehension to make HSLA color values keeping H and S fixed and interpolating the L values. (It sets the alpha or opacity component to 255 so the colors are fully opaque. You can change that if you must for a particular application.)

Converting to RGB

This program uses tkinter to display color samples but tkinter doesn't understand the HSL color model so the program needs to convert the colors. The following hsl_list_to_tkinter_list function converts a list of HSL colors to a list of RGB hex strings that tkinter can understand.

def hsl_list_to_tkinter_list(hsl_list): '''Convert a list of HSL colors to a list of tkinter hex color values.''' return [hsl_to_tkinter(hsl) for hsl in hsl_list]

This code simply uses a list comprehension to call hsl_to_tkinter to convert each HSL color into an RGB hex string.

The hsl_to_tkinter function and the other HSL functions that it uses are described in my earlier HSL posts. For example, see Use a rainbow gradient to select numbers in Python.

Drawing Shades

The following code executes when you click the example's Draw button.

def draw(self, *args): '''Draw the shades.''' # Get parameters. h = float(self.h_var.get()) s = float(self.s_var.get()) l0 = float(self.l0_var.get()) l1 = float(self.l1_var.get()) num_shades = int(self.num_shades_var.get()) # Make the shades. hsl_shades = hsl_make_shades(h, s, l0, l1, num_shades) rgb_shades = hsl_list_to_tkinter_list(hsl_shades) # Draw the shades. self.canvas.delete(tk.ALL) self.canvas.update() wid = self.canvas.winfo_width() hgt = self.canvas.winfo_height() dw = wid / num_shades for i in range(num_shades): rect = (i * dw, 0, (i + 1) * dw, hgt) color = rgb_shades[i] self.canvas.create_rectangle(rect, fill=color, outline='')

This code gets the values that you entered. It calls hsl_make_shades to make the list of HSL shades and then calls hsl_list_to_tkinter_list to convert the list into tkinter colors.

Next, the program clears its canvas and uses a loop to draw samples of each of the shades.

Conclusion

[10 shades of blue created in Python] Creating a list of color shades in the HSL color model is relatively easy. Note that the starting and ending lightness values don't need to be white and black. The start value also doesn't need to be greater than the end value. For example, the picture on the right shows a list of shades starting at 25% lightness and ending at 50% lightness. I also changed the hue to 240 (blue) for variety.

Download the example to experiment with it and to see additional details.

© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.