; Auto colorize image into random number of colors of random hues
; author: Tin Tran
; date: 2015
(define (script-fu-auto-colorize image layer 
              hatches
         )
	
		(let* 
		   (
		   (color-map 0)
		   (colors 0)
		   (red 0)
		   (green 0)
		   (blue 0)
		   (y 0)
		   (hue)
		   (floating)
		    )
			;(gimp-image-undo-disable image); DN = NO UNDO
		  (gimp-image-undo-group-start image)                   ;undo-group in one step
		  ;convert to indexed
		  (gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE hatches FALSE FALSE "unused palette name")
		  ;grabs color map
		  (set! colors (vector->list (cadr (gimp-image-get-colormap image))))
		  
		  (gimp-image-convert-rgb image) ;converts it to rgb before we call hatch loop
		  
		  (set! y hatches) ;loop hatches number of times
		  (srand (car (gettimeofday)))
		  (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))
			  ;(set! hue (rand 360))
			  ;(gimp-colorize layer hue 100 0)
			  (gimp-edit-copy layer)
			  (set! floating (car(gimp-edit-paste layer TRUE)))
			  (gimp-floating-sel-to-layer floating)
			  (gimp-image-set-active-layer image floating)
			  (set! hue (rand 360))
			  (gimp-colorize floating hue 100 0)
			  ;(script-fu-colorize image floating (list (rand 255) (rand 255) (rand 255)) 100)
			  (if (> y 1) ;if y is still valid we set colors to the next colors
			     (begin
				 (set! colors (cdddr colors))
				 )
				 (begin ;else
				 )
			  )
			  
              ;loop control
			  (set! y (- y 1))
		  );end of while 
          (gimp-selection-none image)	
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
		   (gimp-image-undo-group-end image)                     ;undo group in one step
	       (gimp-displays-flush)
	    )
	
	
    
) ;end of define

(script-fu-register
  "script-fu-auto-colorize"         ;function name
  "<Image>/Script-Fu/Auto Colorize"    ;menu register
  "Randomly colorize image with specified number of colors."       ;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" '(5 2 255 1 10 0 0)
  
)