; BlendSelection.scm
; for number of steps, keep shrinking image and filling with color that changes
; toward the target color
; author: Tin Tran
; date: 2015

;------------------------- END OF SUB FUNCTION
(define (script-fu-blend-selection image layer 
			start-color
			end-color
            steps
            shrink-pixels
         )
	
		(let* 
			(
		    (red 0)
			(green 0)
			(blue 0)
			(loopy 0)
			(cur-step 0)
		    )
			(gimp-context-push)
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			;fill it with start color
			(gimp-context-set-foreground start-color)
			(gimp-edit-fill layer FOREGROUND-FILL)
			
			(set! loopy steps) ;loop number of steps
		    (while (> loopy 0)
				(set! cur-step (+ cur-step 1))
			    (set! red (+ (car start-color)(*(/(- (car end-color)(car start-color)) steps) cur-step)))
				(set! green (+ (cadr start-color)(*(/(- (cadr end-color)(cadr start-color)) steps) cur-step)))
				(set! blue (+ (caddr start-color)(*(/(- (caddr end-color)(caddr start-color)) steps) cur-step)))
				
				(gimp-context-set-foreground (list red green blue))
				(gimp-selection-shrink image shrink-pixels)
				(gimp-edit-fill layer FOREGROUND-FILL)
				(set! loopy (- loopy 1));loop control
			)
			
			
			
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-displays-flush)
			(gimp-context-pop)
	    )
	
	
    
) ;end of define

(script-fu-register
  "script-fu-blend-selection"         ;function name
  "<Image>/Script-Fu/Blend Selection..."    ;menu register
  "For number of steps, shrinks selection by number of pixels and fills with gradient reaching target color"       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2015"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  SF-COLOR      "Starting Color" '(255 255 255)
  SF-COLOR      "Ending Color" '(0 0 0)
  SF-ADJUSTMENT "Number of steps" '(50 1 255 1 10 0 0)
  SF-ADJUSTMENT "Shrink pixels per step" '(1 1 255 1 10 0 0)
)