home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 December / maximum-cd-2009-12.iso / DiscContents / gimp-2.7.0-i686-setup.exe / {app} / share / gimp / 2.0 / scripts / palette-export.scm < prev    next >
Encoding:
Text File  |  2009-08-19  |  20.4 KB  |  433 lines

  1. ; ----------------------------------------------------------------------------------------------------
  2. ; GIMP palette export toolkit - Written by Barak Itkin <lightningismyname@gmail.com>
  3. ;
  4. ; This script includes various exporters for gimp palettes, and other utility function to help in
  5. ; exporting to other (text-based) formats.
  6. ; See instruction on adding new exporters at the end
  7. ; ----------------------------------------------------------------------------------------------------
  8. ; Changelog:
  9. ; July 23rd, 2009 - Version 1.1
  10. ;                  Removed the palette choose box - the script will now export the active palette.
  11. ;                  This was done because, this script should usually be invoked by a right click on a
  12. ;                  palette, and it's confusing to have to choose a palette again.
  13. ;                  Renamed the default palette name to "palette.ext"
  14. ;                  Shortened long lines (320 charcters =D)
  15. ;
  16. ;                  See http://bugzilla.gnome.org/show_bug.cgi?id=304399#c19
  17. ;
  18. ; May 15th, 2009 - Version 1.0
  19. ;                  Basic framework for exporting palettes.
  20. ;                  The default exporters are java, css, text, python and php
  21. ;
  22. ;                  See http://bugzilla.gnome.org/show_bug.cgi?id=304399#c16
  23. ; ----------------------------------------------------------------------------------------------------
  24. ; Numbers and Math
  25. ; ----------------------------------------------------------------------------------------------------
  26.  
  27. ; For all the opertations below, this is the order of respectable digits:
  28. (define conversion-digits (list "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  29.                                 "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
  30.                                 "r" "s" "t" "u" "v" "w" "x" "y" "z"))
  31.  
  32. ; Converts a decimal number to another base. The returned number is a string
  33. (define (convert-decimal-to-base num base)
  34.   (define (highest-order num base)
  35.     (if (and (<= 0 num) (< num base))
  36.         0
  37.         (+ 1 (highest-order (quotient num base) base))
  38.         )
  39.     )
  40.   (define (calc base num str)
  41.     (let ((max-order (highest-order num base)))
  42.       (cond ((not (= 0 max-order))
  43.              (let ((num-of-times (quotient num (inexact->exact (expt base max-order)))))
  44.                (calc base
  45.                      (- num (* num-of-times (expt base max-order)))
  46.                      (string-append str (list-ref conversion-digits num-of-times)))
  47.                )
  48.              )
  49.             (else (string-append str (list-ref conversion-digits num)))
  50.             ))
  51.     )
  52.   (calc base num "")
  53.   )
  54.  
  55. ; Convert a string representation of a number in some base, to a decimal number
  56. (define (convert-base-to-decimal base num-str)
  57.   (define (convert-char num-char)
  58.     (if (char-numeric? num-char)
  59.         (string->number (string num-char))
  60.         (+ 10 (- (char->integer num-char) (char->integer #\a)))
  61.         )
  62.     )
  63.   (define (calc base num-str num)
  64.     (if (equal? num-str "")
  65.         num
  66.         (calc base
  67.               (substring num-str 1)
  68.               (+ (* num base) (convert-char (string-ref num-str 0)))
  69.               )
  70.         )
  71.     )
  72.   (calc base num-str 0)
  73.   )
  74.  
  75. ; If a string num-str is shorter then size, pad it with pad-str in the begining untill it's at least
  76. ; size long
  77. (define (pre-pad-number num-str size pad-str)
  78.   (if (< (string-length num-str) size)
  79.       (pre-pad-number (string-append pad-str num-str) size pad-str)
  80.       num-str
  81.       )
  82.   )
  83.  
  84. ; ----------------------------------------------------------------------------------------------------
  85. ; Color convertors
  86. ; ----------------------------------------------------------------------------------------------------
  87.  
  88. ; The standard way for representing a color would be a list of red green and blue (gimp's default)
  89. (define color-get-red car)
  90. (define color-get-green cadr)
  91. (define color-get-blue caddr)
  92.  
  93. ; Convert a color to a hexadecimal string
  94. ; '(255 255 255) => "#ffffff"
  95.  
  96. (define (color-rgb-to-hexa-decimal color)
  97.   (string-append "#"
  98.                  (pre-pad-number (convert-decimal-to-base (color-get-red color) 16) 2 "0")
  99.                  (pre-pad-number (convert-decimal-to-base (color-get-green color) 16) 2 "0")
  100.                  (pre-pad-number (convert-decimal-to-base (color-get-blue color) 16) 2 "0")
  101.                  )
  102.   )
  103.  
  104. ; Convert a color to a css color
  105. ; '(255 255 255) => "rgb(255, 255, 255)"
  106. (define (color-rgb-to-css color)
  107.   (string-append "rgb(" (number->string (color-get-red color))
  108.                  ", " (number->string (color-get-green color))
  109.                  ", " (number->string (color-get-blue color)) ")")
  110.   )
  111.  
  112. ; Convert a color to a simple pair of braces with comma seperated values
  113. ; '(255 255 255) => "(255, 255, 255)"
  114. (define (color-rgb-to-comma-seperated-list color)
  115.   (string-append "(" (number->string (color-get-red color))
  116.                  ", " (number->string (color-get-green color))
  117.                  ", " (number->string (color-get-blue color)) ")")
  118.   )
  119.  
  120.  
  121. ; ----------------------------------------------------------------------------------------------------
  122. ; Export utils
  123. ; ----------------------------------------------------------------------------------------------------
  124.  
  125. ; List of charcters that should not appear in file names
  126. (define illegal-file-name-chars (list #\\ #\/ #\: #\* #\? #\" #\< #\> #\|))
  127.  
  128. ; A function to filter a list lst by a given predicate pred
  129. (define (filter pred lst)
  130.   (if (null? lst)
  131.       '()
  132.       (if (pred (car lst))
  133.           (cons (car lst) (filter pred (cdr lst)))
  134.           (filter pred (cdr lst))
  135.           )
  136.       )
  137.   )
  138.  
  139. ; A function to check if a certain value obj is inside a list lst
  140. (define (contained? obj lst) (member obj lst))
  141.  
  142. ; This functions filters a string to have only characters which are either alpha-numeric or contained
  143. ; in more-legal (which is a variable holding a list of characters)
  144. (define (clean str more-legal)
  145.   (list->string (filter (lambda (ch) (or (char-alphabetic? ch)
  146.                                          (char-numeric? ch)
  147.                                          (contained? ch more-legal)))
  148.                         (string->list str)))
  149.   )
  150.  
  151. ; A function that recieves the a file-name, and filters out all the character that shouldn't appear
  152. ; in file names. Then, it makes sure the remaining name isn't only white-spaces. If it's only
  153. ; white-spaces, the function returns false. Otherwise, it returns the fixed file-name
  154. (define (valid-file-name name)
  155.   (let* ((clean (list->string (filter (lambda (ch) (not (contained? ch illegal-file-name-chars)))
  156.                                       (string->list name))))
  157.          (clean-without-spaces (list->string (filter (lambda (ch) (not (char-whitespace? ch)))
  158.                                                      (string->list clean))))
  159.          )
  160.     (if (equal? clean-without-spaces "")
  161.         #f
  162.         clean
  163.         )
  164.     )
  165.   )
  166.  
  167. ; Filters a string from all the characters which are not alpha-numeric (this also removes whitespaces)
  168. (define (name-alpha-numeric str)
  169.   (clean str '())
  170.   )
  171.  
  172. ; This function does the same as name-alpha-numeric, with an added operation - it removes
  173. ; any numbers from the begining
  174. (define (name-standard str)
  175.   (let ((cleaned (clean str '())))
  176.     (while (char-numeric? (string-ref cleaned 0))
  177.            (set! cleaned (substring cleaned 1))
  178.            )
  179.     cleaned
  180.     )
  181.   )
  182.  
  183. (define name-no-conversion (lambda (obj) obj))
  184. (define color-none (lambda (x) ""))
  185. (define name-none (lambda (x) ""))
  186.  
  187. (define displayln (lambda (obj) (display obj) (display "\n")))
  188.  
  189. ; The loop for exporting all the colors
  190. (define (export-palette palette-name color-convertor name-convertor
  191.                         start name-pre name-after name-color-seperator
  192.                         color-pre color-after entry-seperator end)
  193.  
  194.   (define (write-color-line index)
  195.     (display name-pre)
  196.     (display (name-convertor (car (gimp-palette-entry-get-name palette-name index))))
  197.     (display name-after)
  198.     (display name-color-seperator)
  199.     (display color-pre)
  200.     (display (color-convertor (car (gimp-palette-entry-get-color palette-name index))))
  201.     (display color-after)
  202.     )
  203.  
  204.   (let ((color-count (car (gimp-palette-get-colors palette-name)))
  205.         (i 0)
  206.         )
  207.  
  208.     (display start)
  209.  
  210.     (while (< i (- color-count 1))
  211.            (begin
  212.              (write-color-line i)
  213.              (display entry-seperator)
  214.              (set! i (+ 1 i))
  215.              )
  216.            )
  217.  
  218.     (write-color-line i)
  219.     (display end)
  220.     )
  221.   )
  222.  
  223. (define (register-palette-exporter export-type file-type description author copyright date)
  224.   (script-fu-register (string-append "gimp-palette-export-" export-type)
  225.                       (string-append export-type "...")
  226.                       description
  227.                       author
  228.                       copyright
  229.                       date
  230.                       ""
  231.                       SF-DIRNAME "Folder for the output file" ""
  232.                       SF-STRING "The name of the file to create
  233. (If a file with this name already exist, it will be replaced)" (string-append "palette." file-type)
  234.                       )
  235.   (script-fu-menu-register (string-append "gimp-palette-export-" export-type) "<Palettes>/Export as")
  236.   )
  237.  
  238. (define (bad-file-name)
  239.   (gimp-message (string-append "The file name you entered is not a suitable name for a file name! \""
  240.                                file-name "\" All the characters in the file name are either "
  241.                                "white-spaces or characters which can not appear in file names (The"
  242.                                "following characters can not appear in file names: "
  243.                                (list->string illegal-file-name-chars) ")")))
  244.  
  245. ; ----------------------------------------------------------------------------------------------------
  246. ; Exporters
  247. ; ----------------------------------------------------------------------------------------------------
  248. ; The typical look of an exporter would be like this (look at the bottom to see some examples):
  249. ; (define (gimp-palette-export-my-export directory-name file-name palette-name)
  250. ;   (let ((valid-name (valid-file-name file-name)))
  251. ;     (if valid-name
  252. ;         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  253. ;           (lambda ()
  254. ;             (export-palette (car (gimp-context-get-palette)) - The active palette (since this script
  255. ;                                                                should be invoked by a right click on
  256. ;                                                                a palette)
  257. ;                             color-to-string-convertor - A function that recieves a color and
  258. ;                                                         converts it to a string
  259. ;                             name-convertor - A function that recieves the name of the color
  260. ;                                                       from the palette, and alters it if necessary
  261. ;                             start - A string to put before the loop of the colors
  262. ;                             name-pre - A string to put before the name of each color
  263. ;                             name-after - A string to put after the name of each color
  264. ;                             name-color-seperator - A string to put between the name of the color
  265. ;                                                    and the color itself
  266. ;                             color-pre - A string to put before each color
  267. ;                             color-after - A string to put after each color
  268. ;                             entry-seperator - A string that should be put only between entries
  269. ;                             end - A string to put when the loop has ended
  270. ;                             )))
  271. ;         (bad-file-name)
  272. ;         )
  273. ;     )
  274. ;   )
  275. ; (register-palette-exporter "my-export" ".filetype" description author copyright date)
  276. ;
  277. ; avaialable name conversions: name-alpha-numeric, name-standard, name-no-conversion, name-none, color-none
  278. ; avaialable color conversions: color-rgb-to-hexa-decimal, color-rgb-to-css, color-rgb-to-comma-seperated-list
  279. ;
  280. ; You can also add more custom features with this toolkit, however then you will need to do some coding :)
  281. ; For non-programmers who want to add exporters: \n means newline, \t means tab, (and for a normal \, you
  282. ; must write \\)
  283. ;
  284. ; WARNING! If a procedure that writes a file from script-fu crashes, it will currupt the behaviour of the entire
  285. ;          script-fu system (you will have to restart gimp) so you must double-check that it can't get stuck!
  286. ;          If you are not sure what you are doing, use the template above or contact me by email (and I'll try to
  287. ;          help) <lightningismyname@gmail.com>
  288.  
  289. (define (gimp-palette-export-css directory-name file-name)
  290.   (let ((valid-name (valid-file-name file-name)))
  291.     (if valid-name
  292.         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  293.           (lambda () (export-palette (car (gimp-context-get-palette))
  294.                                      color-rgb-to-css
  295.                                      name-alpha-numeric        ; name-convertor
  296.                                      "/* Generated with GIMP palette Export */\n" ; start
  297.                                      "."                       ; name-pre
  298.                                      ""                        ; name-after
  299.                                      " { "                     ; name-color-seperator
  300.                                      "color: "                 ; color-pre
  301.                                      " }"                      ; color-after
  302.                                      "\n"                      ; entry-seperator
  303.                                      ""                        ; end
  304.                                      )))
  305.         (bad-file-name)
  306.         )
  307.     )
  308.   )
  309. (register-palette-exporter "css" "css"
  310.                            (string-append "Exports the active palette as a list css stylesheet with the colors "
  311.                                           "entry name as their class name, and the color itself as the color attribute")
  312.                            "Barak Itkin <lightningismyname@gmail.com>" "Barak Itkin" "May 15th, 2009")
  313.  
  314. (define (gimp-palette-export-php directory-name file-name)
  315.   (let ((valid-name (valid-file-name file-name)))
  316.     (if valid-name
  317.         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  318.           (lambda () (export-palette (car (gimp-context-get-palette))
  319.                                      color-rgb-to-hexa-decimal
  320.                                      name-standard             ; name-convertor
  321.                                      "<?php\n/* Generated with GIMP palette Export */\n$colors={\n" ; start
  322.                                      "'"                       ; name-pre
  323.                                      "'"                       ; name-after
  324.                                      " => "                    ; name-color-seperator
  325.                                      "'"                       ; color-pre
  326.                                      "'"                       ; color-after
  327.                                      ",\n"                     ; entry-seperator
  328.                                      "}\n?>"                 ; end
  329.                                      )))
  330.         (bad-file-name)
  331.         )
  332.     )
  333.   )
  334. (register-palette-exporter "php" "php"
  335.                            "Exports the active palette as a php dictionary (name => color)"
  336.                            "Barak Itkin <lightningismyname@gmail.com>" "Barak Itkin" "May 15th, 2009")
  337.  
  338. (define (gimp-palette-export-python directory-name file-name)
  339.   (let ((valid-name (valid-file-name file-name)))
  340.     (if valid-name
  341.         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  342.           (lambda ()
  343.             (let ((palette-name (car (gimp-context-get-palette))))
  344.               (begin (displayln "# Generated with GIMP palette Export")
  345.                      (displayln (string-append "# Based on the palette " palette-name))
  346.                      (export-palette palette-name
  347.                                      color-rgb-to-hexa-decimal
  348.                                      name-standard             ; name-convertor
  349.                                      "colors={\n"              ; start
  350.                                      "'"                       ; name-pre
  351.                                      "'"                       ; name-after
  352.                                      ": "                      ; name-color-seperator
  353.                                      "'"                       ; color-pre
  354.                                      "'"                       ; color-after
  355.                                      ",\n"                     ; entry-seperator
  356.                                      "}"                       ; end
  357.                                      ))))
  358.           )
  359.         (bad-file-name)
  360.         )
  361.     )
  362.   )
  363. (register-palette-exporter "python" "py"
  364.                            "Exports the active palette as a python dictionary (name: color)"
  365.                            "Barak Itkin <lightningismyname@gmail.com>" "Barak Itkin" "May 15th, 2009")
  366.  
  367. (define (gimp-palette-export-text directory-name file-name)
  368.   (let ((valid-name (valid-file-name file-name)))
  369.     (if valid-name
  370.         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  371.           (lambda ()
  372.             (export-palette (car (gimp-context-get-palette))
  373.                             color-rgb-to-hexa-decimal
  374.                             name-none                 ; name-convertor
  375.                             ""                        ; start
  376.                             ""                        ; name-pre
  377.                             ""                        ; name-after
  378.                             ""                        ; name-color-seperator
  379.                             ""                        ; color-pre
  380.                             ""                        ; color-after
  381.                             "\n"                      ; entry-seperator
  382.                             ""                        ; end
  383.                             )
  384.             )
  385.           )
  386.         (bad-file-name)
  387.         )
  388.     )
  389.   )
  390. (register-palette-exporter "text" "txt"
  391.                            "Write all the colors in a palette to a text file, one hexa-decimal value per line (no names)"
  392.                            "Barak Itkin <lightningismyname@gmail.com>" "Barak Itkin" "May 15th, 2009")
  393.  
  394. (define (gimp-palette-export-java directory-name file-name)
  395.   (let ((valid-name (valid-file-name file-name)))
  396.     (if valid-name
  397.         (with-output-to-file (string-append directory-name DIR-SEPARATOR file-name)
  398.           (lambda ()
  399.             (let ((palette-name (car (gimp-context-get-palette))))
  400.               (begin (displayln "")
  401.                      (displayln "import java.awt.Color;")
  402.                      (displayln "import java.util.Hashtable;")
  403.                      (displayln "")
  404.                      (displayln "// Generated with GIMP palette Export ")
  405.                      (displayln (string-append "// Based on the palette " palette-name))
  406.                      (displayln (string-append "public class " (name-standard palette-name) " {"))
  407.                      (displayln "")
  408.                      (displayln "    Hashtable<String, Color> colors;")
  409.                      (displayln "")
  410.                      (displayln (string-append "    public " (name-standard palette-name) "() {"))
  411.                      (export-palette (car (gimp-context-get-palette))
  412.                                      color-rgb-to-comma-seperated-list
  413.                                      name-no-conversion
  414.                                      "        colors = new Hashtable<String,Color>();\n" ; start
  415.                                      "        colors.put(\""                             ; name-pre
  416.                                      "\""                                                ; name-after
  417.                                      ", "                                                ; name-color-seperator
  418.                                      "new Color"                                         ; color-pre
  419.                                      ");"                                                ; color-after
  420.                                      "\n"                                                ; entry-seperator
  421.                                      "\n    }"                                           ; end
  422.                                      )
  423.                      (display "\n}"))))
  424.           )
  425.         (bad-file-name)
  426.         )
  427.     )
  428.   )
  429.  
  430. (register-palette-exporter "java" "java"
  431.                            "Exports the active palette as a java.util.Hashtable<String, Color>"
  432.                            "Barak Itkin <lightningismyname@gmail.com>" "Barak Itkin" "May 15th, 2009")
  433.