(define (script-fu-decompose-indexed image layer numcolors posterize keeplayer workcopy RGBmode)
  (let* (
    (work-image 0)
    (new-layer 0)
    (index 0)
    (map 0)
    (red 0)
    (green 0)
    (blue 0)
    )

    (if (= workcopy TRUE)
      (begin
        (set! work-image (car (gimp-image-duplicate image)))
        (gimp-display-new work-image)
        )
      (begin
        (set! work-image image)
        (gimp-image-undo-group-start image)
        )
      )
    (set! layer (car (gimp-image-get-active-layer work-image)))
    (if (> (car (gimp-image-get-layers work-image)) 1)
      (set! layer (car (gimp-image-merge-visible-layers work-image EXPAND-AS-NECESSARY)))
      )
    (set! layer (car (gimp-image-get-active-layer work-image)))
    (gimp-selection-all work-image)
    (if (= (car (gimp-drawable-is-rgb layer)) 0)
      (gimp-image-convert-rgb work-image)
      )
    (if (= posterize TRUE)
      (gimp-posterize layer numcolors)
      )
    (gimp-image-convert-indexed work-image NO-DITHER MAKE-PALETTE numcolors 0 TRUE "")
    (set! map (gimp-image-get-colormap work-image))
    (set! index 0)
    (set! numcolors (/ (car map) 3))
    (set! map (cadr map))
    (if (> numcolors 0) ;; 0 means not indexed
      (begin
        (while (> numcolors 0)
          (set! red   (fmod (aref map index) 256))
          (set! green (fmod (aref map (+ 1 index)) 256))
          (set! blue  (fmod (aref map (+ 2 index)) 256))
          (gimp-by-color-select layer
              (list red green blue) ;; color
              0 CHANNEL-OP-REPLACE FALSE 0 0 0 ;; Threshold=0, NO Feather, Layer Only
              )
          (set! new-layer (car (gimp-layer-copy layer TRUE)))
          (gimp-drawable-set-name new-layer
              (string-append "RGB=[" (number->string red) ", " (number->string green) ", " (number->string blue) "]")
              )
          (gimp-image-add-layer work-image new-layer -1)
          (gimp-selection-invert work-image)
          (gimp-edit-clear new-layer)
          (set! numcolors (- numcolors 1))
          (set! index (+ index 3))
          )
        )
      )
    (if (= RGBmode TRUE)
      (gimp-image-convert-rgb work-image)
      )
    (if (= keeplayer FALSE)
      (gimp-image-remove-layer work-image layer)
      )
    (gimp-selection-none work-image)
    (gimp-displays-flush)
    )
  (if (= workcopy FALSE)
    (gimp-image-undo-group-end image)
    )
  )

(script-fu-register "script-fu-decompose-indexed"
 "<Image>/Filters/Colors/_Indexed Decompose"  ;; I prefer it appear in the Filters menu but others
; "<Image>/Script-Fu/Colors/_Indexed Decompose" ;; might like to keep it separate from the standard plugins
 "Separates image into separate layers based on color"
 "Saul Goode"
 "Saul Goode"
 "3/11/2006"
 ""
 SF-IMAGE    "Image"    0
 SF-DRAWABLE "Drawable" 0
 SF-ADJUSTMENT "Number of colors (2-255)" '( 8 2 255 1 5 0 0 )
 SF-TOGGLE "Posterize first (slightly different shading)" TRUE
 SF-TOGGLE "Keep original layer" FALSE
 SF-TOGGLE "Work on copy" TRUE
 SF-TOGGLE "Convert result to RGB" FALSE
 )
