;; A Color is a (make-color red green blue) ;; where red, green, and blue are Numbers ;; Examples (define RED (make-color 255 0 0)) (define GREEN (make-color 0 255 0)) (define BLUE (make-color 0 0 255)) (define MID (make-color 85 85 85)) #| TEMPLATE for color (define (f c) ... (color-red c) ... ... (color-green c) ... ... (color-blue c) ... ) |# ;; A ListOfColor is either ;; -- empty ;; (cons Color ListOfColor) #| TEMPLATE for ListOfColor (define (c loc) (cons [(empty? loc) ...] [(cons? los) ... (first loc) ... (f (rest loc)) ...])) |# ;; Examples empty (cons RED empty) (cons BLUE (cons RED empty)) ;; Number Number Number -> Number ;; Find the average value of the color portions (define (color-avg r g b) (round (/ (+ r g b) 3))) ;; Tests (= (color-avg 255 0 0) 85) (= (color-avg 0 255 0) 85) (= (color-avg 0 0 255) 85) ;; ColorList -> ColorList ;; Convert all color to BW colors (define (all-bw loc) (cond [(empty? loc) empty] [(cons? loc) (cons (make-color (color-avg (color-red (first loc)) (color-green (first loc)) (color-blue (first loc))) (color-avg (color-red (first loc)) (color-green (first loc)) (color-blue (first loc))) (color-avg (color-red (first loc)) (color-green (first loc)) (color-blue (first loc)))) (all-bw (rest loc)))])) ;; Tests (equal? (all-bw empty) empty) (equal? (all-bw (cons RED empty)) (cons MID empty)) ;; Image -> Image ;; Convert img to black and white (define (image->bw img) (color-list->image (all-bw (image->color-list img)) (image-width img) (image-height img) (/ (image-width img) 2) (/ (image-height img) 2)))