1#!/usr/local/bin/python 2# -*- coding: utf-8 -*- 3 4# Allows saving (TODO: and loading) CSS gradient files 5# Copyright (C) 2011 João S. O. Bueno <gwidion@gmail.com> 6# 7# This program is free software: you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 3 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <https://www.gnu.org/licenses/>. 19 20 21# Currently this exports all color segments as RGB linear centered segments. 22# TODO: Respect gradient alpha, off-center segments, different blending 23# functions and HSV colors 24 25from gimpfu import * 26 27gettext.install("gimp20-python", gimp.locale_directory, unicode=True) 28 29w3c_template = """background-image: linear-gradient(top, %s);\n""" 30moz_template = """background-image: -moz-linear-gradient(center top, %s);\n""" 31webkit_template = """background-image: -webkit-gradient(linear, """ \ 32 """left top, left bottom, %s);\n""" 33 34color_to_html = lambda c: "rgb(%d,%d,%d)" % tuple(c)[:3] 35 36def format_text(text): 37 counter = 0 38 new_text = [] 39 for token in text.split(","): 40 if counter + len(token) > 77: 41 token = "\n " + token 42 counter = 4 43 new_text.append(token) 44 if "\n" in token: 45 counter = len(token.rsplit("\n")[-1]) + 1 46 else: 47 counter += len(token) + 1 48 49 return ",".join(new_text) 50 51def gradient_css_save(gradient, file_name): 52 stops = [] 53 wk_stops = [] 54 n_segments = pdb.gimp_gradient_get_number_of_segments(gradient) 55 last_stop = None 56 for index in xrange(n_segments): 57 lcolor, lopacity = pdb.gimp_gradient_segment_get_left_color( 58 gradient, 59 index) 60 rcolor, ropacity = pdb.gimp_gradient_segment_get_right_color( 61 gradient, 62 index) 63 lpos = pdb.gimp_gradient_segment_get_left_pos(gradient, index) 64 rpos = pdb.gimp_gradient_segment_get_right_pos(gradient, index) 65 66 lstop = color_to_html(lcolor) + " %d%%" % int(100 * lpos) 67 wk_lstop = "color-stop(%.03f, %s)" %(lpos, color_to_html(lcolor)) 68 if lstop != last_stop: 69 stops.append(lstop) 70 wk_stops.append(wk_lstop) 71 72 rstop = color_to_html(rcolor) + " %d%%" % int(100 * rpos) 73 wk_rstop = "color-stop(%.03f, %s)" %(rpos, color_to_html(rcolor)) 74 75 stops.append(rstop) 76 wk_stops.append(wk_rstop) 77 last_stop = rstop 78 79 final_text = w3c_template % ", ".join(stops) 80 final_text += moz_template % ",".join(stops) 81 final_text += webkit_template % ",".join(wk_stops) 82 83 with open(file_name, "wt") as file_: 84 file_.write(format_text(final_text)) 85 86register( 87 "gradient-save-as-css", 88 "Creates a new palette from a given gradient", 89 "palette_from_gradient (gradient, number, segment_colors) -> None", 90 "Joao S. O. Bueno", 91 "(c) GPL V3.0 or later", 92 "2011", 93 "Save as CSS...", 94 "", 95 [ 96 (PF_GRADIENT, "gradient", N_("Gradient to use"),""), 97 (PF_FILENAME, "file_name", N_("File Name"), ""), 98 ], 99 [], 100 gradient_css_save, 101 menu="<Gradients>", 102 domain=("gimp20-python", gimp.locale_directory) 103 ) 104main()