;script as described by PegLeg44 here: http://gimpchat.com/viewtopic.php?f=9&t=14046&start=10#p191875
;-----------------------------------------------------------------------------------
;1. Set the layer mode to Grain Extract
;2. Select the alpha of the layer [Layer] [Tranceparency] [Alpha to Selection].
;3. Copy Visible [Edit] [Copy Visible]
;4. Paste (Standard Paste should center automatically)
;5. [Layer] [Anchor Layer]
;6. [Colors] [Invert]
;7. Threshold with the left field at 128 [Colors] [Threshold] (No need to change the layer to normal)
;8. [Layer] [Transparency] [Color to Alpha]
;9. Set layer mode to [Color]


(define (script-fu-colorize-heightmap-for-pegleg44 image layer 
         )
	
		(let* 
		   (
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   (floating-sel 0)
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			(gimp-context-push)
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			
			;1. Set the layer mode to Grain Extract
			(gimp-layer-set-mode layer GRAIN-EXTRACT-MODE)
			
			;2. Select the alpha of the layer [Layer] [Tranceparency] [Alpha to Selection].
			(gimp-image-select-item image CHANNEL-OP-REPLACE layer)
			
			;3. Copy Visible [Edit] [Copy Visible]
			(gimp-edit-copy-visible image)
			
			;4. Paste (Standard Paste should center automatically)
			(set! floating-sel (car (gimp-edit-paste layer TRUE)))
			
			;5. [Layer] [Anchor Layer]
			(gimp-floating-sel-anchor floating-sel)
			
			;6. [Colors] [Invert]
			(gimp-invert layer)
			
			;7. Threshold with the left field at 128 [Colors] [Threshold] (No need to change the layer to normal)
			(gimp-threshold layer 128 255)
			
			;8. [Layer] [Transparency] [Color to Alpha]
			(plug-in-colortoalpha 1 image layer '(255 255 255))
			
			;9. Set layer mode to [Color]
			(gimp-layer-set-mode layer COLOR-MODE)
			
			;10. Added by Tin to rename layer to make it user-friendly
			(gimp-layer-set-name layer (string-append (car (gimp-layer-get-name layer)) " - colorize"))
			
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-context-pop)
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(script-fu-register
  "script-fu-colorize-heightmap-for-pegleg44"         ;function name
  "<Image>/Script-Fu/colorize-heightmap-for-PegLeg44 ..."    ;menu register
  "Steps described by PegLeg44 "       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2016"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  ;SF-STRING     "Text" "Gimp Chat"
)
