12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

168 Chapter 17. Classes and methodscolor is an RGB tuple; that is, the elements are Red-Green-Blue levels between 0.0 and 1.0 (seewikipedia.org/wiki/RGB_color_model).If you run this code, you should see a window with a black background and a blue sphere. If youdrag the middle button up and down, you can zoom in and out. You can also rotate the scene bydragging theright button, but withonly one sphere intheworld, itis hardtotell thedifference.The following loop creates acube of spheres:t = range(0, 256, 51)for x in t:for y in t:for z in t:pos = x, y, zsphere(pos=pos, radius=10, color=color)1. Put thiscode inascriptand make sureitworks foryou.2. Modify the program so that each sphere in the cube has the color that corresponds to itspositioninRGBspace. Noticethatthecoordinatesareintherange0–255,buttheRGBtuplesare intherange 0.0–1.0.3. Download thinkpython.com/code/color_list.py and use the function read_colors togenerate a list of the available colors on your system, their names and RGB values. For eachnamed color draw asphere intheposition that corresponds toitsRGB values.You can see mysolutionatthinkpython.com/code/color_space.py.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!