Sunday, October 14, 2012

Another tutorial exercise down...



I started the Dr. Racket exercises for tutorial #4 by practicing the code we had done in class with map and map-image. I created various lists to get familiar with using map and I defined my own function add5, mimicking add1, to add 5 to every number in my list. I also tried the functions previously defined in class (less-green and swap-red-blue) on a new image.  Then I moved on to defining my own functions to change the colours in my selected image. I initially tried to define my function so that red would be removed from every pixel but I was unsuccessful. I kept getting errors like:

                color-red expects a color, given 0

and my check-expect kept failing so finally I gave up trying to eliminate red entirely. Instead I defined my function to mimic less-green, but set red to be the quotient when divided by 10 rather than 2. After this worked I opened the Help Desk from Dr. Racket. I searched until I found an example of how to define a function that produces an image with one colour completely leached out. The definition in the example was written: 

                ; lose red: color -> color
                (define (lose-red old color)
                                (make-color 0 (color-green old-color) (color-blue old-color)))

I followed this example but rather than use 'old-color' I kept what we had used in class, so my function read:

                (define (remove-red c)
                                (make-color 0
                                (color-green c)
                                (color-blue c)
                                (color-alpha c)))

I ran this and applied it to an image. It worked! My test passed and it produced the image I wanted.
                I moved on to the next part of the tutorial, defining rotate-red-blue-green but had an extra set of brackets when I tried to apply it to my image and I kept getting the error:  

                color-green: expects a color, given #<image>

I looked at swap-red-blue and realized I didn't need  the brackets. I deleted them, ran and got the expected image.  I found the tutorial exercises good practice for getting acquainted with using map and map-image on my own.

No comments:

Post a Comment