; 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-using-path image layer 
			start-color
			end-color
            steps
            shrink-pixels
         )
	
		(let* 
			(
		    (red 0)
			(green 0)
			(blue 0)
			(loopy 0)
			(cur-step 0)
			(path-original 0)
			(path 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)
			(plug-in-sel2path 1 image layer)
		    (set! path-original (car (gimp-image-get-vectors-by-name image "Selection")))
			
			(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))
				
				;shrink from original
				(gimp-image-select-item image CHANNEL-OP-REPLACE path-original)
				
				(gimp-selection-shrink image (* shrink-pixels cur-step))
				
				;converts to path then back to selection to get rid of aliased pixels
				(plug-in-sel2path 1 image layer)
				(set! path (car (gimp-image-get-vectors-by-name image "Selection #1")))
				(gimp-image-select-item image CHANNEL-OP-REPLACE path)
				(gimp-image-remove-vectors image path)
				
				(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-using-path"         ;function name
  "<Image>/Script-Fu/Blend Selection Using Paths..."    ;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)
)