(define (script-fu-paint-by-numbers simage slayer 
              num-colors
			  create-lines
			  thickness
			  gaussian-blur-radius
		)
	
		(let* 
		   (
		   (color-map 0)
		   (colors 0)
		   (image-width)
		   (image-height)
			(brush-name)
		   (red 0)
		   (green 0)
		   (blue 0)
		   (y 0)
		   (image)
		   (layer)
		   (num-vectors)
		   (vector-ids)
		   (current-vector)
		   (lines-layer)
		    )
		  
		  
		  (gimp-context-push)
		  ;(gimp-image-undo-group-start image)                   ;undo-group in one step
		  ;convert to indexed
		  (set! image-width (car (gimp-image-width simage)))
	      (set! image-height (car (gimp-image-height simage)))
		  
		  
		  ;creates new image instead of working on existing one
		  (gimp-selection-all simage)
		  (gimp-edit-copy slayer)
		  (gimp-selection-none simage)
		  (set! image (car (gimp-edit-paste-as-new)))
		  (set! layer (car(gimp-image-get-active-layer image)))
		  (gimp-display-new image)
		  ;(set! image (car (gimp-image-new width height RGB)))      ;creates new image
		  
		  
		  (plug-in-gauss RUN-NONINTERACTIVE image layer
	                                        gaussian-blur-radius gaussian-blur-radius
											1 ;1 RLE or 0 IIR
	                                        )
		  (gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE num-colors FALSE FALSE "unused palette name")
		  ;grabs color map
		  (set! num-colors (/ (car (gimp-image-get-colormap image)) 3))
		  (set! colors (vector->list (cadr (gimp-image-get-colormap image))))
		  
		  (gimp-image-convert-rgb image) ;converts it to rgb before we call hatch loop
		  
		  
		  (if (= create-lines TRUE)
				(begin
					  (set! y num-colors) ;loop  number of times
					  (gimp-context-set-sample-threshold 0)
					  (while (> y 0)
							;do work here
							(set! red (car colors))
							(set! green (cadr colors))
							(set! blue (caddr colors))
							;select each color
							(gimp-image-set-active-layer image layer)
							(gimp-image-select-color image CHANNEL-OP-REPLACE layer (list red green blue))
							;selection to path
							(plug-in-sel2path 1 image layer)
							(set! colors (cdddr colors))
							
							;loop control
							(set! y (- y 1))
					  );end of while 
		              (gimp-selection-none image)
		  
		  
				
					  (set! num-vectors (car (gimp-image-get-vectors image)))
					  (set! vector-ids (vector->list (cadr (gimp-image-get-vectors image))))
					  (set! y num-vectors)
					  (gimp-context-set-foreground '(0 0 0))
					  (gimp-context-set-brush-size thickness)
					  (set! brush-name (car(gimp-brush-new "paint by numbers")))
						(gimp-brush-set-hardness brush-name 1)
						(gimp-brush-set-radius brush-name thickness)
						(gimp-brush-set-shape brush-name BRUSH-GENERATED-CIRCLE)
						(gimp-context-set-brush brush-name)         ;use created brush
						(gimp-context-set-brush-size thickness)
					  
						(set! lines-layer (car (gimp-layer-new image image-width image-height
									   RGBA-IMAGE "Lines" 100 NORMAL-MODE)))  ;creates layer
						(gimp-image-insert-layer image lines-layer 0 0)	 	  
						(gimp-image-set-active-layer image lines-layer)
					  (while (> y 0)
						(set! current-vector (car vector-ids))
						(gimp-edit-stroke-vectors lines-layer current-vector)
						(set! vector-ids (cdr vector-ids))
						(set! y (- y 1))
					  )
					   (gimp-brush-delete brush-name)
		   	
				)
				(begin)
			)
	       (gimp-displays-flush)
		   (gimp-context-pop)
	    )
	
	
    
) ;end of define

(script-fu-register
  "script-fu-paint-by-numbers"         ;function name
  "<Image>/Script-Fu/Create New/Paint By Numbers..."    ;menu register
  "Create Paint By Numbers look."       ;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-ADJUSTMENT "Number of colors" '(15 2 50 1 10 0 0)
  SF-TOGGLE     "Create Lines" TRUE
  SF-ADJUSTMENT "Line Thickness(pixels)" '(2 1 10 1 10 0 0)
  SF-ADJUSTMENT "Gaussian Blur Radius(pixels)" '(10 5 100 1 10 0 0)
  
)