Sunday, 18 November 2012

Swapping Colors in DrRacket


I think that the fact we can exchange the colours in an entire image is very interesting and useful. At the beginning, it is a hard concept to learn, since first we have to be able to apply the function, let’s say ‘swap-red-blue-green’, to a single pixel in the image. After we are sure that it works, then we proceed to apply the function to the whole image through a tool named ‘map’ followed by the function we want to try.  
The following code is an example of how to apply a function like ‘swap-red-blue-green’ to all the pixels in an image.

       (require picturing-programs)

       (define c (make-color 1 2 3 4))   ; pick any numbers for a colour

       (check-expect (rotate-red-blue-green (make-color 2 3 4 5))  (make-color 3 4 2 5))

        (define (rotate-red-blue-green c) (make-color 
                                                        (color-green c)
                                                        (color-blue c)
                                                        (color-red c)
                                                        (color-alpha c)
                                                         )
          )

         (map-image rotate-red-blue-green  )

Finally, as a result for having swapped the colours, we get the following image:


No comments:

Post a Comment