1 / 20

Computer Science 111

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

myrna
Download Presentation

Computer Science 111

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 111 Fundamentals of Programming I Colors, Tuples, Nested Loops, and Grids

  2. 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

  3. 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

  4. Some Example Colors

  5. 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>)

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. The grid Module’s Functions

  14. 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

  15. 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

  16. Print # of Rows and Columns >>> print g.getHeight(), g.getWidth() 3 3

  17. 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

  18. Increment the Elements for row xrange(g.getHeight()): for col in xrange(g.getWidth()): g.put(row, col, g.get(row, col) + 1)

  19. Represent an Image >>> image = Grid(100, 100, (255, 0, 0)) This image is 100 pixels high and 100 pixels wide. Each pixel is red.

  20. 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.

More Related