(define (make-icon image
                   drawable
                   bg-color
                   blur-width
                   tint-flag
                   tint-color
                   tint-amount
                   mod-colors-flag
                   flatten-flag)
  
  (if (> tint-flag 100)
      (= tint-flag 100)
      (if (< tint-flag 0)
          (= tint-flag 0)))
  
  (set! item-opacity (- 100 tint-amount))

  (let ((bg-layer (car (gimp-layer-new image 64 64
                        RGBA-IMAGE "Background" 100 NORMAL-MODE)))
        (blur-layer (aref (cadr (gimp-image-get-layers image)) 0))
        (selection (gimp-selection-bounds image)))
        
       (if (= (car selection) 1)
           (gimp-image-crop image
                            (- (nth 3 selection) (nth 1 selection))
                            (- (nth 4 selection) (nth 2 selection))
                            (cadr selection)
                            (caddr selection)))
                            
       (gimp-image-scale image 64 64)
       (gimp-drawable-set-name blur-layer "Blur")
       (gimp-layer-add-alpha blur-layer)
       
       ;background layer
       (gimp-image-add-layer image bg-layer 0)
       (gimp-image-lower-layer-to-bottom image bg-layer)
       (gimp-drawable-fill bg-layer WHITE-FILL)
       (plug-in-colortoalpha 1 image bg-layer '(255 255 255))

       ;color layer
       (set! color-layer (car (gimp-layer-copy blur-layer 1)))
       (gimp-drawable-set-name color-layer "Color")
       (gimp-image-add-layer image color-layer  0)
       (gimp-by-color-select color-layer bg-color 0 CHANNEL-OP-REPLACE FALSE 0 0 0)
       (gimp-selection-invert image)
       (gimp-context-set-foreground tint-color)
       (gimp-edit-fill color-layer FOREGROUND-FILL)
       (gimp-selection-invert image)
       (plug-in-colortoalpha 1 image color-layer bg-color)
       (gimp-selection-none image)
       (if (eqv? tint-flag FALSE)
           (gimp-image-remove-layer image color-layer))
       
       ;item layer
       (set! item-layer (car (gimp-layer-copy blur-layer 1)))
       (gimp-drawable-set-name item-layer "Item")
       (gimp-image-add-layer image item-layer 0)
       (gimp-by-color-select item-layer bg-color 0 CHANNEL-OP-REPLACE FALSE 0 0 0)
       (plug-in-colortoalpha 1 image item-layer bg-color)
       (if (eqv? tint-flag TRUE)
           (gimp-layer-set-opacity item-layer item-opacity))
       (gimp-selection-none image)
       
       ;blur layer
       (gimp-by-color-select blur-layer bg-color 0 CHANNEL-OP-REPLACE FALSE 0 0 0)
       (gimp-selection-invert image)
       (gimp-selection-grow image 1)
       (gimp-context-set-foreground '(239 231 173))
       (gimp-edit-fill blur-layer FOREGROUND-FILL)
       (gimp-selection-invert image)
       (plug-in-colortoalpha 1 image blur-layer bg-color)
       (gimp-selection-none image)
       
       (define (blur n)
         (define (call_blur n acc)
           (plug-in-blur 1 image blur-layer)
           (if (not (= acc n))
               (blur (+ acc 1))))
         (call_blur (- n 1) 0))
       
       (blur 4)
       
       (if (eqv? mod-colors-flag TRUE)
           (prog1 (gimp-brightness-contrast item-layer 0 75)
                  (gimp-color-balance item-layer SHADOWS TRUE 100 0 0)
                  (plug-in-cartoon 1 image item-layer 7 0)))
                  
       (if (eqv? flatten-flag TRUE)
           (gimp-image-merge-visible-layers image CLIP-TO-IMAGE))
           
       (gimp-displays-flush)))

(script-fu-register "make-icon"
		    _"<Image>/Script-Fu/Oblivion/Make Icon"
		    "Make an image into an icon for oblivion"
		    "Alasdair"
		    "Alasdair"
		    "Jan 2006"
		    ""
        SF-IMAGE    "image" 0
        SF-DRAWABLE "Drawable" 0
		    SF-COLOR    "Background colour" '(127 127 127)
        SF-VALUE    "Blur width (pixels)" "3"
        SF-TOGGLE   "Tint?" 1
        SF-COLOR    "Tint - Colour" '(106 61 43)
        SF-VALUE    "Tint - Intensity (0-100)" "60"
        SF-TOGGLE   "Tweak Item Colours?" 1
        SF-TOGGLE   "Flatten Layers?" 1
        )