[gimp] plug-ins: Create save Gradient as CSS3 python-script



commit 4245b6bbe18a3ee770e80153ed9aa95a292cb641
Author: João S. O. Bueno <gwidion gmail com>
Date:   Thu May 12 13:47:50 2011 -0400

    plug-ins: Create save Gradient as CSS3 python-script
    
    A script to enable seamlesly save GIMP gradients as
    CSS3 gradients, conformant to w3c and current existing
    implementations: mozilla and webkit.

 plug-ins/pygimp/plug-ins/Makefile.am      |    1 +
 plug-ins/pygimp/plug-ins/css-gradients.py |  104 +++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 0 deletions(-)
---
diff --git a/plug-ins/pygimp/plug-ins/Makefile.am b/plug-ins/pygimp/plug-ins/Makefile.am
index 35549f4..503b008 100644
--- a/plug-ins/pygimp/plug-ins/Makefile.am
+++ b/plug-ins/pygimp/plug-ins/Makefile.am
@@ -4,6 +4,7 @@ pluginexecdir = $(gimpplugindir)/plug-ins
 
 scripts = \
 	colorxhtml.py		\
+	css-gradients.py		\
 	file-openraster.py	\
 	foggify.py		\
 	palette-offset.py	\
diff --git a/plug-ins/pygimp/plug-ins/css-gradients.py b/plug-ins/pygimp/plug-ins/css-gradients.py
new file mode 100644
index 0000000..fd3c65d
--- /dev/null
+++ b/plug-ins/pygimp/plug-ins/css-gradients.py
@@ -0,0 +1,104 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+#   Allows saving (TODO: and loading) CSS gradient files
+#   Copyright (C) 2011 João S. O. Bueno <gwidion gmail com>
+#
+#   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.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# Currently this exports all color segments as RGB linear centered segments.
+# TODO: Respect gradient alpha, off-center segments, different blending
+# functions and HSV colors 
+    
+from gimpfu import *
+
+gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
+
+w3c_template = """background-image: linear-gradient(top, %s);\n"""
+moz_template = """background-image: -moz-linear-gradient(center top, %s);\n"""
+webkit_template = """background-image: -webkit-gradient(linear, """ \
+                  """left top, left bottom, %s);\n"""
+
+color_to_html = lambda c: "rgb(%d,%d,%d)" % tuple(c)[:3]
+
+def format_text(text):
+    counter = 0
+    new_text = []
+    for token in text.split(","):
+        if counter + len(token) > 77:
+            token = "\n    " + token
+            counter = 4
+        new_text.append(token)
+        if "\n" in token:
+            counter = len(token.rsplit("\n")[-1]) + 1
+        else:
+            counter += len(token) + 1
+        
+    return ",".join(new_text)
+    
+def gradient_css_save(gradient, file_name):
+    stops = []
+    wk_stops = []
+    n_segments = pdb.gimp_gradient_get_number_of_segments(gradient)
+    last_stop = None
+    for index in xrange(n_segments):
+        lcolor, lopacity = pdb.gimp_gradient_segment_get_left_color(
+                                gradient,
+                                index)
+        rcolor, ropacity = pdb.gimp_gradient_segment_get_right_color(
+                                gradient,
+                                index)
+        lpos = pdb.gimp_gradient_segment_get_left_pos(gradient, index)
+        rpos = pdb.gimp_gradient_segment_get_right_pos(gradient, index)
+        
+        lstop = color_to_html(lcolor) + " %d%%" % int(100 * lpos)
+        wk_lstop = "color-stop(%.03f, %s)" %(lpos, color_to_html(lcolor))
+        if lstop != last_stop:
+            stops.append(lstop)
+            wk_stops.append(wk_lstop)
+        
+        rstop = color_to_html(rcolor) + " %d%%" % int(100 * rpos)
+        wk_rstop = "color-stop(%.03f, %s)" %(rpos, color_to_html(rcolor))
+        
+        stops.append(rstop)
+        wk_stops.append(wk_rstop)
+        last_stop = rstop
+    
+    final_text = w3c_template % ", ".join(stops)
+    final_text += moz_template % ",".join(stops)
+    final_text += webkit_template % ",".join(wk_stops)
+    
+    with open(file_name, "wt") as file_:
+        file_.write(format_text(final_text))
+
+register(
+        "palette_from_gradient",
+        "Creates a new palette from a given gradient",
+        "palette_from_gradient (gradient, number, segment_colors) -> None",
+        "Joao S. O. Bueno",
+        "(c) GPL V3.0 or later",
+        "2011",
+        "Save as CSS...",
+        "",
+        [
+          (PF_GRADIENT, "gradient", N_("Gradient to use"),""),
+          (PF_FILE, "file_name", N_("File Name"), ""),
+        ],
+        [],
+        gradient_css_save,
+        menu="<Gradients>",
+        domain=("gimp20-python", gimp.locale_directory)
+        )
+main()
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]