210 likes | 304 Views
Computer Science 111. Fundamentals of Programming I Colors, Tuples, Nested Loops, and Grids. Representing Colors. When a color is digitized, it is represented as a triple of three integer values, (r, g, b) The integers represent the intensities of red, green, and blue mixed into the color
E N D
Computer Science 111 Fundamentals of Programming I Colors, Tuples, Nested Loops, and Grids
Representing Colors • When a color is digitized, it is represented as a triple of three integer values, (r, g, b) • The integers represent the intensities of red, green, and blue mixed into the color • Also called the RGB system
Range of RGB Values • Each integer ranges from 0 to 255, for 256 distinct intensity values • Thus, there are 2563 or 224 or 16,777,216 distinct colors • 0 is the absence of intensity, 255 is the total saturation of intensity
Tuples • A tuple is an ordered collection of two or more elements • Represented in Python as • Often used to hold three values, such as RGB color components (<element-1>, . . ., <element-n>)
Examples >>> red = (255, 0, 0) >>> red (255, 0, 0) Build a tuple of three integers and look it up >>> (r, g, b) = red >>> r 255 >>> g 0 >>> b 0 >>> (r, g, b) (255, 0, 0) Use variables to extract the component values
Simple for Loops for i in xrange(5): # Prints 0 1 2 3 4 print i, sum = 0 for element in [20, 30, 40]: sum += element # Sum is 90 at end for element in myfile: sum += int(element) Just iterate through a sequence of values The sequences are linear
Nested Loops for row in xrange(7): for column in xrange(7): <do something with row and column> 0 1 2 3 4 5 6 0 • Often used to process grid-like structures • Game boards • Bitmaps of images • Matrices • Maps and diagrams 1 2 3 4 5 6
Grid Positions as (row,col) Pairs for row in xrange(3): for col in xrange(3): print (row, col), print In this 3 by 3 imaginary grid, each row has 3 positions and each column has 3 positions #col0 col1 col2 (0,0) (0,1) (0,2) # row0 (1,0) (1,1) (1,2) # row1 (2,0) (2,1) (2,2) # row2
Or Use a while Loop row = 0 while row < 3: col = 0 while col < 3: print (row, col), col += 1 print row += 1 #col0 col1 col2 (0,0) (0,1) (0,2) # row0 (1,0) (1,1) (1,2) # row1 (2,0) (2,1) (2,2) # row2
Representing a Grid as Data • Sometimes called a two-dimensional list or matrix • Access each position with the methods get and put • Usually process the grid with a nested loop
A Grid Type • Like the list type or the string type, the grid type is actually a set of operations on a set of data values (grids) • Define a Grid class and its operations in a module • Import the module to create and use grids • The module hides the details of the data structures and operations used to represent and manipulate a grid
Creating a Grid from grid import Grid g = Grid(3, 3, 1) # Create a 3 by 3 grid of ones g = Grid(3, 3) # Ditto g = Grid(3) # Ditto g = Grid() # Ditto
Print a Grid >>> print g 1 1 1 1 1 1 1 1 1 print automatically runs str, which formats the elements in the grid in two dimensions
Print # of Rows and Columns >>> print g.getHeight(), g.getWidth() 3 3
Sum the Elements sum = 0 for row xrange(g.getHeight()): for col in xrange(g.getWidth()): sum += g.get(row, col) print sum # Prints 9
Increment the Elements for row xrange(g.getHeight()): for col in xrange(g.getWidth()): g.put(row, col, g.get(row, col) + 1)
Represent an Image >>> image = Grid(100, 100, (255, 0, 0)) This image is 100 pixels high and 100 pixels wide. Each pixel is red.
Insert a Blue Line Across the Middle of the Image row = image.getHeight() / 2 for col in xrange(image.getWidth()): image.put(row, col, (0, 0, 255)) Much of image processing is just manipulating color values in a grid.