#lang racket
(require gigls/unsafe)
;;;(image-series n width height)

;;; Procedure:
;;;   rgb-blend
;;; Parameters:
;;;   rgb1, a rgb color
;;;   rgb2, a rgb color
;;; Purpose:
;;;   Create a new color that is a blend of both colors that were given.
;;; Produces:
;;;   rgb-new, a rgb color
;;; Preconditions:
;;;   rgb1 is a rgb-color
;;;   rgb2 is a rgb-color
;;; Postconditions:
;;;   No Additional
(define rgb-blend
  (lambda (rgb1 rgb2)
    (rgb-new
     (/ (+ (rgb-red rgb1) (rgb-red rgb2)) 2)
     (/ (+ (rgb-green rgb1) (rgb-green rgb2)) 2)
     (/ (+ (rgb-blue rgb1) (rgb-blue rgb2)) 2))))

;;; Procedure:
;;;   project
;;; Parameters:
;;;   n, a number 
;;;   width, a number
;;;   height, a number
;;; Purpose:
;;;   Create a interesting image that is variable by n and similar when stretched by different heights and widths.
;;; Produces:
;;;   project, an image 
;;; Preconditions:
;;;   0 < n < 1000 
;;;   0 < width
;;;   0 < height
;;; Postconditions:
;;;   Project will be an image with two intersecting rectangles and two circles relatively in the same area no matter the value of n, height, or width.


(define project
  ;;;outline the parameters
  (lambda (n width height)
    ;;;we need something to create on
    (define canvas (image-new width height))
    ;;;we need to sync the canvas with turtles
    (define world canvas)
    (define T
      (turtle-new canvas))
   
    (image-select-polygon! canvas REPLACE
                           (position-new (/ width 2) 0)
                           (position-new 0 (/ height 2))
                           (position-new 0 0))
    (context-set-fgcolor! "yellow")
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    (image-select-polygon! canvas REPLACE
                           (position-new (/ width 2) 0)
                           (position-new width 0)
                           (position-new width (/ height 2)))
    (context-set-fgcolor! "red")
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    (image-select-polygon! canvas REPLACE
                           (position-new 0 (/ (image-height canvas) 2))
                           (position-new 0 (image-height canvas))
                           (position-new (/ (image-width canvas) 2) (image-height canvas)))
    (context-set-fgcolor! "green")
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    (image-select-polygon! canvas REPLACE
                           (position-new (image-width canvas) (/ (image-height canvas) 2))
                           (position-new (image-width canvas) (image-height canvas))
                           (position-new (/ (image-width canvas) 2) (image-height canvas)))
    (context-set-fgcolor! "blue")
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
          (cond [(<= n 255) (turtle-set-brush! T "inverse Blur Grid")]
         [(and (> n 255) (<= n 510))
              (turtle-set-brush! T "Bristles 01")]
               [(and (> n 510) (<= n 765))
                  (turtle-set-brush! T "Cell 02")]
                  [else (turtle-set-brush! T "2. Hardness 100 (editable)")])
    (turtle-up! T)
    (turtle-forward! T (/ width 10))
    (turtle-down! T)
    (turtle-turn! T 90)
    (turtle-forward! T height)
    (turtle-turn! T 270)
    ;;;
    (turtle-up! T)
    (turtle-forward! T (/ width 10))
    (turtle-turn! T 270)
    (turtle-down! T)
    (turtle-forward! T height)
    (turtle-teleport! T 0 0)
    (turtle-turn! T 180)
    ;;;;
    (turtle-up! T)
    (turtle-forward! T (/ height 10))
    (turtle-down! T)
    (turtle-turn! T 270) 
    (turtle-forward! T width)
    (turtle-turn! T 90)
    ;;;
    (turtle-up! T)
    (turtle-forward! T (/ height 10))
    (turtle-down! T)
    (turtle-turn! T 90)
    (turtle-forward! T width)
    (turtle-up! T)
    ;;;   coloring the vertical rectangle created by turtles
    (image-select-rectangle! canvas ADD (/ width 10) 0 (/ width 10) height)
    (context-set-fgcolor! (rgb-new (modulo n 255) (modulo (* 10 n) 255) (modulo (* 20 n) 255)))
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    ;;;   coloring the horizontal rectangle created by turtles
    (image-select-rectangle! canvas ADD 0 (/ height 10) width (/ height 10))
    (context-set-fgcolor! (rgb-complement (rgb-new (modulo n 255) (modulo (* 10 n) 255) (modulo (* 20 n) 255))))
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    ;;;   coloring the square formed by the two rectangles created by turtles.
    (image-select-rectangle! canvas ADD (/ width 10) (/ height 10) (/ width 10) (/ height 10))
    (context-set-fgcolor! (rgb-blend (rgb-complement (rgb-new (modulo n 255) (modulo (* 10 n) 255) (modulo (* 20 n) 255)))
                                     (rgb-new (modulo n 255) (modulo (* 10 n) 255) (modulo (* 20 n) 255))))
    (image-fill-selection! canvas)
    (image-select-nothing! canvas)
    ;;;   making the lower left circle able to phaseshift the colors of the contained drawings.
    (image-select-ellipse! canvas ADD (* 2 (/ width 3)) (/ height 10) (/ width 5) (/ height 5))
    (image-transform! canvas rgb-complement)
    (image-select-nothing! canvas)
    ;;;   making the lower left circle able to phaseshift the colors of the contained drawings.
    (image-select-ellipse! canvas ADD (/ width 20) (* 7 (/ height 10)) (/ width 2) (/ height 2))
    (image-transform! canvas rgb-complement)
    (image-select-nothing! canvas)
    ;;;   Making the middle diamond capable of darkening any overlapping drawing.
    (image-select-polygon! canvas REPLACE
                           (position-new (/ (image-width canvas) 2) 0)
                           (position-new (image-width canvas) (/ (image-height canvas) 2))
                           (position-new (/ (image-width canvas) 2) (image-height canvas))
                           (position-new 0 (/ (image-height canvas) 2)))
    (image-transform! canvas rgb-darker)
    (image-select-nothing! canvas)
    ;;;   Forming the top-left corner so that all overlapping drawings will be lighter.
    (image-select-polygon! canvas REPLACE
                           (position-new (/ width 2) 0)
                           (position-new 0 (/ height 2))
                           (position-new 0 0))
    (image-transform! canvas rgb-lighter)
    (image-select-nothing! canvas)
    ;;;   Forming the top-right corner so that all overlapping drawings will be redder.
    (image-select-polygon! canvas REPLACE
                           (position-new (/ width 2) 0)
                           (position-new width 0)
                           (position-new width (/ height 2)))
    (image-transform! canvas rgb-redder)
    (image-select-nothing! canvas)
    ;;;   Forming the bottom-left corner so that all overlapping drawings will be greener.
    (image-select-polygon! canvas REPLACE
                           (position-new 0 (/ (image-height canvas) 2))
                           (position-new 0 (image-height canvas))
                           (position-new (/ (image-width canvas) 2) (image-height canvas)))
    (image-transform! canvas rgb-greener)
    (image-select-nothing! canvas)
    ;;;   Forming the bottom-right corner so that all overlapping drawings will be bluer.
    (image-select-polygon! canvas REPLACE
                           (position-new (image-width canvas) (/ (image-height canvas) 2))
                           (position-new (image-width canvas) (image-height canvas))
                           (position-new (/ (image-width canvas) 2) (image-height canvas)))
    (image-transform! canvas rgb-bluer)
    (image-select-nothing! canvas)
   
    ;;;   finally show the image
    (image-show canvas)))