(define (script-fu-Mondrian ch) (begin (gimp-message (string-append "Und meine Mondrian Super läuft::" (number->string ch))) ( mondrian-make-Canvas ch))) ; Wraper Around the Main Procedure for Isolation of the GUI

; The Main Procedure
(define (mondrian-make-Canvas ch)
    (begin 
        ; Load Granularity to Global in Main-Procedure
        (define n ch)
        ; define Seetings for Image
        (define canvas-dx 300) ; Define the Wide of the Picture
        (define canvas-dy 200) ; Define the Hight of the Picture
        ; Construct the Image
        (define image (car (gimp-image-new canvas-dx canvas-dy RGB))) ; Create a New Image with Seetings
        (define layer (car (gimp-layer-new image canvas-dx canvas-dy RGB-IMAGE "Proj1" 100 0)))
        ; Construct the first Layer to hold Content
        
        ;The Working Cernel of the Main-Procedure
        (define (mondrian-paint-image)
        (begin
            (mondrian_paint_backgroud) ; Paint first a Background ??? Eventuelly only for Debuging
            (divider_pre 0 0 300 200 ) ; Paint the Content
            (gimp-display-new image) ; display the Result
        )
        )
        
        ; Just Paint the context
        (define (mondrian_paint_backgroud)
        (begin
            (gimp-context-set-background '(200 100 100)) ; Set Background to a nice Red Color
            (gimp-drawable-fill layer FILL-BACKGROUND) ; Fil Background with the Color
        )
        
        ; Main Divider Function
        ; Should Choose the Way to divide the Space of the Image
        )
        (define (divider x y dx dy )
    (let* (
    (select (random 2)) ; Construct a One to One Rabdom Decision
    )
    (if (> select 1) (divider_x x y dx dy ) (divider_y x y dx dy ))
    )
)
    
; Helper Divider Function
; Choose to Divide or to Divide Future
(define (divider_pre x y dx dy )
        (let* (
        (choice ( random 100)) ; Intialise Random-selection
        )
        ; Complex Decision Function about Chance and not to fine Divide 
        (if (and ( < n choice) (> dx 5) (> dy 5)) (divider x y dx dy ) (mondrian_paint_object x y dx dy))
        )
    )
    
    
; Take the Division
(define (divider_x x y dx dy )
    (let* (
    (n1dx (random dx)) ; Calculate First Halfe
    (n2dx (- dx n1dx)) ; Calculate Second Halfe
    )
    (divider_pre x y n1dx dy) ; Do First Halfe
    (divider_pre (+ x n1dx) y n2dx dy) ; Do Second Halfe
    )
)

(define (divider_y  x y dx dy )
    (let* (
    (n1dy (random dy)) ; Calculate First Halfe
    (n2dy (- dy n1dy)) ; Calculate Second Halfe
    )
    (divider_pre  x y dx n1dy)  ; Do First Halfe
    (divider_pre  x (+ y n1dy) dx n2dy) ; Do Second Halfe
    )
)

(define (mondrian_paint_object x y dx dy) ; Do Draw Content Element
    ; Calculate Paramters for Black Boundary and Content
    (let* (
    ; Random Boundary Paras
    (ddx1 (max (random 9) 1))
    (ddy1 (max (random 9) 1))
    (ddx2 (min (random 9) (- dx 1)))
    (ddy2 (min (random 9) (- dy 1)))
    ; Random Fill Color
    (red (random 200))
    (green (random 200))
    (blue (random 200))
    (color (list red green blue))
    )
    
    ; Paint the Black Boundary
    (gimp-context-set-foreground '(0 0 0)) ; Set foreground Color to Black
    (gimp-rect-select image x y dx dy 0 0 0) ; Define Size of Big Element
    (gimp-edit-fill layer FILL-FOREGROUND) ; Fill it with foreground Color Black
    (gimp-selection-none image) ; Remove selection
    
    ; Paint the Main Content
    (gimp-context-set-foreground color) ; Set Content Color to Random Color - See Above
    ; Calculate and Set Size and PLace of Main Content
    (gimp-rect-select image (+ x ddx1) (+ y ddy1) (- dx ddx2) (- dy ddy2) 0 0 0)
    (gimp-edit-fill layer FILL-FOREGROUND) ; Fill Content with Coor
    (gimp-selection-none image) ; Remove selection
    )
)

        (gimp-image-add-layer image layer 0); Add Layer to Image
        (mondrian-paint-image); And just do the Working
    )
)

    
    
  (script-fu-register
    "script-fu-Mondrian"                        ;func name
    "Create a Mondrian"                                  ;menu label
    "This is my first script-fu Script \
     It draws a Mondrian like Image \
     See for the Famous Artist Piet Mondrian:\
     https://www.piet-mondrian.org \
     Please Attention to the Granularity Volume \
     it Should work betwen 16 and 25"              ;description
    "Thomas Lahme"                             ;author
    "Copyright:: Its under the GPL"        ;copyright notice
    "19 April 2021"                          ;date created
    ""                     ;image type that the script works on
    SF-ADJUSTMENT  "Granularity"     '(5 1 100 1 10 0 1)
   )
  (script-fu-menu-register "script-fu-Mondrian" "<Image>/MondrianVV")
      
