Saturday, 3 November 2012

Assignment #1

Fortunately for me, I found almost everything to be pretty straightforward this week. The biggest challenge I encountered was from our first project that is due next week. 
In DrRacket we are given instructions to complete the definition of a function, clarify-image that will take an image and make its light colours transparent. We are given the definition of clarify-color which takes the sum of the red, green and blue colour intensities of a pixel and sets their alpha intensity to 0 depending if the sum is greater than 650. Pixels with colour intensities over 650 will become absolutely transparent, otherwise, they remain unchanged. The definition of clarify-color is given as follows:

; clarify-color : color -> color
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is


(define (clarify-color c)
  (make-color
   (color-red c)
   (color-green c)
   (color-blue c)
   (cond
     [(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
     [else (color-alpha c)])))


The next portion of this code asks to write a definition for clarify-image, having been given 2 check-expects:

; clarify-image : image -> image
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is, in im
(check-expect (clarify-image (circle 10 "solid" (make-color 250 250 250 255)))
              (circle 10 "solid" (make-color 250 250 250 0)))
(check-expect (clarify-image (circle 10 "solid" (make-color 100 100 100 255)))
                             (circle 10 "solid" (make-color 100 100 100 255)))

; !!! fix the definition of clarify-image
(define (clarify-image im) im)


This was the hard part. I initially approached this question thinking "I want clarify-image to do exactly what clarify-color does, except to entire images". Copying the exact code of clarify-color and changing "c" to "im" will not work. I know this because I tried it and it failed. And while most check-expects provide clues on writing a definition, these were not very helpful. However, I recalled that during one of our lectures, Professor Heap took an image and made it less green by first defining a function to reduce the green colour and then using map-image to use that function towards an image. Map-image is important as it applies a function to all pixels of an inputted image. Following the examples Heap gave in class and combining them with my new-found knowledge of condition statements, I was able to devise a definition that carried out the role of clarify-image. At first I had created an additional function (which I later realized was redundant), do-clarify-image that would do what clarify-image is supposed to do on a pixel level. I would then use map-image to apply do-clarify-image to all the pixels in an image:


(define (do-clarify-image c)
(make-color
   (color-red c)
   (color-green c)
   (color-blue c)
   (cond
     [(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
     [else (color-alpha c)])))


(define (clarify-image im) 
              map-image do-clarify-image im)

The check-expect tests were ran and they passed using this definition of clarify-image. It then dawned on me that do-clarify-image was uncannily similar to another function I've seen before...
Yes, that's right.
The definitions for clarify-color and do-clarify-image are exactly the same.
Therefore, you can can completely eliminate do-clarify-image and replace it with clarify-color which has already been defined! You will end up with:

(define (clarify-image im) 
              map-image clarify-color im)
Although it may seem confusing, this is the actual thought process I used to arrive at the solution!

Below are some helpful links that helped me with this part of the assignment:

 You can find the code to Professor Heap's mapping lecture on the CSC course website for Week 5 http://www.cdf.toronto.edu/~heap/104/F12/. 

Also, the Picturing Programs Teachpack is useful in explaining the purpose of map-image and make-color:
http://docs.racket-lang.org/picturing-programs/index.html

No comments:

Post a Comment