#lang racket
(require gigls/unsafe)
(provide (all-defined-out))

;;; File:
;;;   recursion-basics-lab.rkt
;;; Authors:
;;;   Janet Davis
;;;   Samuel A. Rebelsky
;;;   Jerod Weinman
;;;   YOUR NAME HERE
;;; Summary:
;;;   Code for the lab entitled "Recursion Basics"

; +-----------------+-----------------------------------------------------------
; | Color Utilities |
; +-----------------+

;;; Procedure:
;;;   irgb-dark?
;;; Parameters:
;;;   color, an integer-encoded RGB color
;;; Purpose:
;;;   Determine whether the color appears dark.
;;; Produces:
;;;   dark?, a Boolean
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   If color is relatively dark, then dark? is #t.
;;;   Otherwise, dark? is #f.
(define irgb-dark?
  (lambda (color)
     (> 33 (irgb-brightness color))))

;;; Procedure:
;;;   irgb-brightness
;;; Parameters:
;;;   color, an integer-encoded RGB color
;;; Purpose:
;;;   Computes the brightness of color on a 0 (dark) to 100 (light) scale.
;;; Produces:
;;;   b, an integer
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   If color1 is likely to be perceived as lighter than color2,
;;;     then (irgb-brightness color1) > (irgb-brightness color2).
;;;   0 <= b <= 100
(define irgb-brightness
  (lambda (color)
    (round (* 100 (/ (+ (* 0.30 (irgb-red color))
                        (* 0.59 (irgb-green color))
                        (* 0.11 (irgb-blue color)))
                      255)))))

; +------------+----------------------------------------------------------------
; | Arithmetic |
; +------------+

;;; Procedure:
;;;   sum
;;; Parameters:
;;;   numbers, a list of numbers.
;;; Purpose:
;;;   Find the sum of the elements of a given list of numbers
;;; Produces:
;;;   total, a number.
;;; Preconditions:
;;;   All the elements of numbers must be numbers.
;;; Postcondition:
;;;   total is the result of adding together all of the elements of numbers.
;;;   If all the values in numbers are exact, total is exact.
;;;   If any values in numbers are inexact, total is inexact.
(define sum
  (lambda (numbers)
    (if (null? numbers)
        0
        (+ (car numbers) (sum (cdr numbers))))))

; +------------------+----------------------------------------------------------
; | Filtering Colors |
; +------------------+

;;; Procedure:
;;;   irgb-filter-out-dark
;;; Parameters:
;;;   colors, a list of integer-encoded RGB colors.
;;; Purpose:
;;;   Create a new list of colors, consisting only of non-dark colors.
;;; Produces:
;;;   not-dark, a list of integer-encoded RGB colors.
;;; Preconditions:
;;;   rgb-dark? is defined.
;;; Postconditions:
;;;   Every element of not-dark appears in colors.
;;;   If (not (rgb-dark? (list-ref colors i))) holds for some i,
;;;     then the corresponding color appears in not-dark.
(define irgb-filter-out-dark
  (lambda (colors)
    (cond
     [(null? colors)
      null]
     [(irgb-dark? (car colors))
      (irgb-filter-out-dark (cdr colors))]
     [else
      (cons (car colors) (irgb-filter-out-dark (cdr colors)))])))
