I don't know anything about the method you are trying to implement, but for the rest: Assuming Canvas
is of type Picture
, you can't get directly the color that way. The color of a pixel can be obtained from a variable of type Pixel
:
Example: Here is the procedure to get the color of each pixels from an image and assign them at the exact same position in a new picture:
def copy(old_picture):
# Create a picture to be returned, of the exact same size than the source one
new_picture = makeEmptyPicture(old_picture.getWidth(), old_picture.getHeight())
# Process copy pixel by pixel
for x in xrange(old_picture.getWidth()):
for y in xrange(old_picture.getHeight()):
# Get the source pixel at (x,y)
old_pixel = getPixel(old_picture, x, y)
# Get the pixel at (x,y) from the resulting new picture
# which remains blank until you assign it a color
new_pixel = getPixel(new_picture, x, y)
# Grab the color of the previously selected source pixel
# and assign it to the resulting new picture
setColor(new_pixel, getColor(old_pixel))
return new_picture
file = pickAFile()
old_pic = makePicture(file)
new_pic = copy(old_pic)
Note: The example above applies only if you want to work on a new picture without modifying the old one. If your algorithm requires to modify the old picture on the fly while performing the algorithm, the final setColor
would have been applied directly to the original pixel (no need for a new picture, neither the return
statement).
Starting from here, you can compute anything you want by manipulating the RGB values of a pixel (using setRed()
, setGreen()
and setBlue()
functions applied to a Pixel
, or col = makeColor(red_val, green_val, blue_val)
and apply the returned color to a pixel using setColor(a_pixel, col)
).
Example of RGB manipulations here.
Some others here and especially here.