; Transfer colors from an existing image
; author: Tin Tran
; date: 2015
(define (script-fu-transfer-color simage slayer 
              src_image
              hatches
        )
	
		(let* 
		   (
		   (color-map 0)
		   (colors 0)
		   (src_colors)
		   (image-width)
		   (image-height)
		   
		   
		   (image)
		   (layer)
		   (s_image)
		   (s_layer)
		    )
		  
		  
			;(gimp-image-undo-disable image); DN = NO UNDO
		  ;(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)
		  (set! image (car (gimp-edit-paste-as-new)))
		  (set! layer (car(gimp-image-get-active-layer image)))
		  
		  (gimp-selection-all src_image)
		  (set! s_layer (car(gimp-image-get-active-layer src_image)))
		  (gimp-edit-copy s_layer)
		  (set! s_image (car (gimp-edit-paste-as-new)))
		  (set! s_layer (car(gimp-image-get-active-layer s_image)))
		  (gimp-display-new image)
		  ;(set! image (car (gimp-image-new width height RGB)))      ;creates new image
		  
		  
		  
		  (gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE hatches FALSE FALSE "unused palette name")
		  (gimp-image-convert-indexed s_image NO-DITHER MAKE-PALETTE hatches FALSE FALSE "unused pallette name 2")
		  ;grabs color map
		  (gimp-image-set-colormap image (car (gimp-image-get-colormap s_image)) (cadr (gimp-image-get-colormap s_image)))
		  
		  (gimp-image-delete s_image)
		  (gimp-displays-flush)
	    )
	
	
    
) ;end of define

(script-fu-register
  "script-fu-transfer-color"         ;function name
  "<Image>/Script-Fu/Create New/Transfer colors..."    ;menu register
  "Transfer colors from an existing image"       ;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-IMAGE      "Source Image" 0
  SF-ADJUSTMENT "Number of colors" '(5 2 256 1 10 0 0)
  
  
)