#lang racket
(require gigls/unsafe)

;;; Procedure:
;;;   black-to-rainbow!
;;; Parameters:
;;;   image, an image
;;; Purpose:
;;;   Convert all the black (or very dark) colors
;;;   to a rainbow blend.
;;; Produces:
;;;   [Nothing; called for the side effects]
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   The black (or close to black) colors in the
;;;   image now show some strange rainbow blend.
(define black-to-rainbow!
  (let ([blackish? (lambda (color)
                     (and (< (irgb-red color) 16)
                          (< (irgb-green color) 16)
                          (< (irgb-blue color) 16)))])
    (lambda (image)
      (let* ([hscale (/ 1 (image-width image))]
             [vscale (/ 1 (image-height image))]
             [hue-multiplier (* hscale 360)]
             [sat-multiplier vscale])
        (image-redo!
         image
         (lambda (col row color)
           (if (blackish? color)
               (hsv->irgb (list (round (* col hue-multiplier))
                                (* row sat-multiplier)
                                1))
               color)))))))
