;;; Description:
;;; Creates a colored ice effect on an object or text layer with an alpha channel.
;;; -----------------------------------------------------------------------------
;;; Change Log:
;;; Version-1.0
;;; Version- 2.0 - Fix lightness slider
;;;
; Information can be found at GimpChat.com
;
; License: GPLv3
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; To view a copy of the GNU General Public License
; visit: http://www.gnu.org/licenses/gpl.html

; Thank yous go out to Saulgoode, Graechan, and Samj at GimpChat.com for their help.

        ;Check Gimp version
	(define (gimp-version-meets? check)
		  (let ((c (append (map string->number (strbreakup check ".")) '(0 0 0)))
				(v (append (map string->number (strbreakup (car (gimp-version)) ".")) '(0 0 0))) )
		  (if (> (car v) (car c)) #t
		  (if (< (car v) (car c)) #f
		   (if (> (cadr v) (cadr c)) #t
		   (if (< (cadr v) (cadr c)) #f
			 (if (>= (caddr v) (caddr c)) #t #f)))))))

   (define (script-fu-ice-anything image
								   drawable
								   icelength
								   turbulence
								   color
								   saturation
								   lightness
         )

        ;;begin image undo and reset
          (gimp-context-push)
          (gimp-image-undo-group-start image)   
       
        ;;prepare the text or object
        (gimp-layer-resize-to-image-size drawable) ; Resize the layer to image size
        (gimp-image-select-item image 0 drawable) ; Select the item on the layer
        (plug-in-plasma RUN-NONINTERACTIVE image drawable 0 turbulence) ;create ice grain effect
        (gimp-desaturate-full drawable 1) ;desat image with luminosity
       
        ;;begin curves points array
        (gimp-curves-spline drawable HISTOGRAM-VALUE 14 (list->vector '(0 0 13 62 69 74 100 120 168 144 185 221 255 255)))
        (gimp-selection-none image) ; deselect the item on the layer   
        (gimp-image-rotate image 0) ;rotate to prepare for wind filter

        ;;add Icicles
        (plug-in-wind RUN-NONINTERACTIVE image drawable 2 1 icelength 0 1)
        (gimp-image-rotate image 2) ;rotate back to original position

        ;;color the ice   
        (gimp-colorize drawable color saturation lightness) ;hue  ;saturation ;lightness
       
        ;;set all user settings back to before script ran or undo filter
		  (gimp-displays-flush)
          (gimp-context-pop)
          (gimp-image-undo-group-end image)
   
   )



        ;register menu location and information   
       (script-fu-register "script-fu-ice-anything"
        "<Image>/Addons/AddSCM/Text Effects/Ice Anything"
        "Creates an ice effect on a object or text layer."
        "Rod Detmer"
        "Rod Detmer"
        "June 2013"
        "RGB*"
        SF-IMAGE       "Image"            0
        SF-DRAWABLE    "Drawable"         0
        SF-ADJUSTMENT      "Icicle Length"  '(5 1 100 1 1 0 1)         
        SF-ADJUSTMENT      "Ice Turbulence" '(2.8 0.1 7.0 1 1 1 1)
        SF-ADJUSTMENT      "Ice Color"    '(202 0 360 1 1 0 1)
        SF-ADJUSTMENT      "Saturation"     '(40 0 100 1 1 0 1)
        SF-ADJUSTMENT      "Lightness"      '(0 -77 100 1 1 0 1)
       )