2011-10-18 11:52:59 +02:00

126 lines
3.3 KiB
Plaintext

;;Highway-Image
(define highway ...)
;;Purpose: Horizontal Sobel-Operator
(define sobel-we (list 2 0 -2))
;; Contract: brightness: color -> number
;; Purpose: Calculates the brightness of a color-struct.
;; Example: (brightness (make-color 0 0 0))
;; (brightness (make-color 255 0 255))
(define (brightness color)
(/ (+ (color-red color)
(color-green color)
(color-blue color)
)
3
)
)
;; Test
(check-expect (brightness (make-color 0 0 0)) 0)
(check-expect (brightness (make-color 255 0 255)) 170)
;; Contract: append2loc: (listof color) -> (listof color)
;; Purpose: Ensures that there are at least 3 color-structs
;; in the list. If not (make-color 0 0 0) are appended.
;; If list is empty, no list is given triggers an error.
;; Example:
(define (append2loc loc)
(if (cons? loc)
(if (color? (second loc))
(if (color? (thrid loc))
loc
(append loc (make-color 0 0 0))
)
(append loc (make-color 0 0 0) (make-color 0 0 0))
)
(error 'append2loc "not a listof color")
)
)
;; Test
;; Contract: apply-for-env3x1: (listof color) ((listof color) -> color) -> (listof color)
;; Purpose:
;; Example:
(define (apply-for-env3x1 loc loc2c)
(list (loc2c (list (first (append2loc loc))
(second (append2loc loc))
(third (append2loc loc))
)
)
(apply-for-env3x1 (rest (append2loc loc)))
)
)
;; Test
;;(check-expect (apply-for-env3x1))
;;(check-expect (apply-for-env3x1))
;; Contract: numberfitcolor: number -> number
;; Purpose: Checks if number is a valid value
;; for a color struct. If not produces
;; a valid value.
;; Uses min and floor.
;; Example: (numberfitcolor -5)
;; (numberfitcolor 267)
(define (numberfircolor n)
(if (< n 0)
0
(floor (min n 255))
)
)
;; Test
(check-expect (numberfitcolor -5) 0)
(check-expect (numberfitcolor 267) 255)
;; Contract: vec-multi: (listof number) (listof number) -> number
;; Purpose: Calculates Scalar-product of two vectors
;; Example: (vec-multi '(1 2 3) '(1 2 3))
;; (vec-multi '(1 2 3 4) '(1 2 3 4))
(define (vec-multi v1 v2)
(if (and (cons? v1)
(cons? v2)
)
(foldl + 0 (map * v1 v2))
0
)
)
;; Test
(check-expect (vec-multi '(1 2 3) '(1 2 3)) 14)
(check-expect (vec-multi '(1 2 3 4) '(1 2 3 4)) 30)
;; Contract: scalarofcolorsobel-we: color -> number
;; Purpose: Calculates scalar-product of a color-struct
;; and sobel-we
;; Makes sure result is a valid color-value.
;; Uses vec-multi, sobel-we
;; Example:
(define (scalarofcolorsobel-we color)
(numberfitcolor (vec-multi color sobel-we))
)
;; Test
;; Contract: sobel: (list color color color) -> color
;; Purpose:
;; Example:
(define (sobel lo3c)
(if (and (cons? lo3c)
(color? (first lo3c))
(color? (second lo3c))
(color? (third lo3c))
)
(make-color (scalarofcolorsobel-we (first lo3c))
(scalarofcolorsobel-we (second lo3c))
(scalarofcolorsobel-we (third lo3c))
)
)
;; Test
(check-expect (sobel (list (make-color 0 0 60)
(make-color 0 0 0)
(make-color 0 60 0)))
(make-color 0 0 0))
(check-expect (sobel (list (make-color 0 0 20)
(make-color 127 0 255)
(make-color 0 30 40)))
(make-color 34 34 34))