Assignment 29 — coloring within the lines
Due Friday, December 2
Module crayon.py
- Complete the implementation of functions
flood()
andsweep()
- For this assignment we are interested in auto-coloring a user-specified coloring book page.
- Everyone of the elements comprising a page will be assigned its own random color. All unpainted spots in a particular element, will be painted using that color. The function managing the painting of an element is
flood()
. If the starting point for a new flooding is already colored, then functionflood()
simply returns.
- Function
sweep()
has responsibility for making sure all of the elements of a page are colored. Going from left-to-right, top-to-bottom, it will examine the current spot. If the spot is unpainted, it will generate a random color and flood that spot's element with the just generated color. If the current spot Is already painted,sweep()
goes on to its next spot.
- The two functions should make use of module do.py to assist with the coloring. It provides the following functions
def random_color() :
- Returns a random RGB three-tuple, where each element of the tuple is a random integer value from the interval 0 ... 255.
def get_width( page ) :
- Returns the width of the Image represented by
page
.
def get_height( page ) :
- Returns the height of the Image represented by
page
.
def left( spot ) :
- Returns the coordinate immediately left of
spot
; that is, its x-coordinate is one less than that ofspot
and its y-coordinate is the same as that ofspot
.
def right( spot ) :
- Returns the coordinate immediately right of
spot
; that is, its x-coordinate is one greater than that ofspot
and its y-coordinate is the same as that ofspot
.
def above( spot ) :
- Returns the coordinate immediately above
spot
; that is, its y-coordinate is one less than that ofspot
and its x-coordinate is the same as that ofspot
.
def below( spot ) :
- Returns the coordinate immediately below
spot
; that is, its y-coordinate is one greater than that ofspot
and its x-coordinate is the same as that ofspot
.
def is_inbounds( page, spot ) :
- Returns
True
orFalse
depending whether coordinate spot is inbounds on the image represented bypage
.
def get_color( page, spot ) :
- If coordinate
spot
is inbounds on the image represented bypage
, the function returns the color atspot
in thepage
; otherwise, the function returnsNone
.
def is_colorable( page, spot, bg ) :
- Returns
True
orFalse
depending whether coordinatespot
is a colorable pixel in imagepage
. To be colorablespot
must be inbounds and equal to the background colorbg
.
def paint( page, spot, c, bg) :
- If
spot
is a colorable pixel on imagepage
, then that pixel is set toc
; otherwise, no action is taken.
Flooding visualization
- Slides exported as PDF
- Slides exported as MP4
Function flood( page, spot, c, bg )
- If
spot
is not a colorable pixel onpage
, then no action is taken.
- If instead
spot
is a colorable pixel onpage
, then all pixel locactions reachable fromspot
traversing only colorable pixels are set to colorc
.
- The function does not return a value
- Visualization of how
flood()
should work is available above.
- (Highly) recommended algorithm
If the spot on the page is not colorable
return
Color the spot using c
Create a to-do list with spot as its element
While the to-do list is not empty
Pop an element from the to-do list and assign it to v
If v’s left is empty, paint it and add to to-do list
If v’s right is empty, paint it and add to to-do list
If v’s above is empty, paint it and add to to-do list
If v’s below is empty, paint it and add to to-do list
Function sweep( spot, bg )
- For every
bg
colorablespot
pixel, aflood()
is performed. For each flooding a new random color is used.
- The function does not return a value.
- My implementation considers the rows of pixels making up a picture from top to bottom. Within a row, the pixels were considered left to right. If you do a different order, your picture could look different. Also, my implementation generates a random color only if the pixel under consideration is colorable.
page_width = do.get_width( page )
page_height = do.get_height( page )
for px in range( 0, page_width ) :
for py in range( 0, page_height ) :
spot = (px, py) # spot is the current drawing coordinate of
# interest.
# if the pixel at spot is colorable, generate a random color
# and start a flood from there
pass
Coloring examples