ColorWall

Project
Program graphical effects for a ColorWall.

Goals

 * Have fun experimenting with and creating graphical effects.
 * Practice using functions and classes.
 * Get experience with graphics programming using the Tkinter GUI toolkit.
 * Practice reading other people's code.

Download and un-archive the ColorWall project skeleton code

 * http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/ColorWall.zip

Un-archiving will produce a  folder containing several Python files, including: run.py, effects.py, and advanced_effects.py.

Test your setup
From a command prompt, navigate to the  directory and run

python run.py

You should see a window pop up and start cycling through colorful effects. If you don't, let a staff member know so you can debug this together.

1. Learn about HSV values
Run the ColorWall effects again with

python run.py

The names of the effects are printed to the terminal as they are run. Pay particular attention to the first 4 effects:
 * SolidColorTest
 * HueTest
 * SaturationTest
 * ValueTest

In all of these effects, a tuple  containing the hue, saturation, and value describing a color are passed to   to change the color of a single pixel on the wall.

What are the differences between these tests? Given these difference and how they are expressed visually, how does varying hue, saturation, or value change a color?

Check your understanding: what saturation and value would you guess firetruck red have?

Step 1 resources:   Python tuples: http://www.tutorialspoint.com/python/python_tuples.htm   Using the  function to produce a sequence of numbers: http://docs.python.org/tutorial/controlflow.html#the-range-function   Using the  module to sleep (do nothing for a bit) inside your program: http://docs.python.org/library/time.html  

2. Examine and the interface its subclasses provide
All of the effects inherit from the  class. Examine this class and its  and   methods.

What is the purpose of the  method?

What is the purpose of the  method?

Open up  and look at this chunk of code at the bottom of the file:

for effect in effects_to_run: new_effect = effect(wall) print new_effect.__class__.__name__ new_effect.run

exports and  list at the bottom of the file. goes through every effect in that list, creates a new instance of the effect, and invokes its  method.

Check your understanding: what would happen if you added an effect to the  list that didn't implement a   method? (Try it!)

Step 2 resources:   Creating and using Python functions: http://www.sthurlow.com/python/lesson05/   Creating and using Python classes: http://www.sthurlow.com/python/lesson08/  A discussion on  and  : http://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do  

3. Examine the nested loop in
for x in range(self.wall.width): for y in range(self.wall.height): self.wall.set_pixel(x, y, hsv)

This code loops over every pixel in the ColorWall, setting the pixel to a particular  value. After that  loop is over,   updates the display.

Check your understanding: what would happen if you moved the  to inside the inner   loop, just under   in  ? (Try it!)

Tip: you can run individual tests by passing their names as command line arguments to. For example, if you only wanted to run, you could:

python run.py SaturationTest

4. Implement a new effect called
It should run for 5 seconds, cycling through the colors in the rainbow, pausing for a moment at each color.

Remember to add your effect to the  list at the bottom of  !

Test your new effect with

python run.py RainbowTest

5. Play with the randomness in
Walk through. Find explanations of the  and   functions in the online documentation at http://docs.python.org/library/random.html.

Experiment with these functions at a Python prompt:

import random random.randint(0, 1) random.randint(0, 5) random.uniform(-1, 1)

Then experiment with the numbers that make up the hue and re-run the effect:

python run.py Twinkle

Challenge: make  twinkle with shades of red.

6. Implement a new effect that involves randomness!
Remember to add your effect to the  list at the bottom of.

1. Checkerboard
Find and change the colors used in the  effect, and re-run the effect:

python run.py Checkerboards

Then change the line

if (x + y + i) % 2 == 0:

to

if (x + y + i) % 3 == 0:

re-run the effect, and see what changed.

What other patterns can you create by tweaking the math for this effect?

2. Matrix
Find and change the color of the columns in the  effect, and re-run the effect:

python run.py Matrix

Each column that we see on the wall corresponds to a  object. Add some randomness to the color used by each column (the variable whose value you changed above) using the  function, re-run the effect, and see what happens.

3. Write more of your own effects!
You have color, time, randomness, letters, and more at your disposal. Go nuts!

Congratulations!
You've read, modified, and added code to a software project that makes art out of pixels. Keep practicing!