This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/color_harmony/color_harmony/export.py

140 lines
5.3 KiB
Python

#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2009-2018 Ilya Portnov <portnov84@rambler.ru>
# (original 'palette-editor' tool, version 0.0.7)
# 2020 Maren Hachmann (extension-ification)
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from os.path import join, basename
from lxml import etree as ET
from zipfile import ZipFile, ZIP_DEFLATED
class PaletteFile(object)
def __init__(self, name, colors, filename, folder):
self.colors = colors
if name != "":
self.name = name
else:
self.name = "Palette"
self.folder = folder
def save(self, format):
FORMATS = {'gimp': [build_gpl, 'gpl'],
'scribus': [build_scribus_xml, 'xml'],
'krita': [build_kpl, 'kpl'],
'css': [build_css, 'css'],
'android_xml': [build_android_xml, 'xml'],
}
if os.path.exists(self.folder):
# save with given file name
pass
def build_gpl(self):
# TODO: fix
palette_string = (u"Name: {}\n".format(self.palette.name).encode('utf-8'))
if hasattr(self.palette, 'ncols') and self.palette.ncols:
palette_string += 'Columns: %s\n' % self.palette.ncols
for key,value in self.palette.meta.items():
if key != "Name":
palette_string += u"# {}: {}\n".format(key, value).encode('utf-8')
palette_string += '#\n'
for row in self.palette.slots:
for slot in row:
n = slot.name
r, g, b = slot.color.getRGB()
s = '%d %d %d %s\n' % (r, g, b, n)
palette_string += s
for key,value in slot.color.meta.items():
if key != "Name":
palette_string += u"# {}: {}\n".format(key, value).encode('utf-8')
return palette_string
def build_kpl(self):
# TODO: fix (and don't save it here, only build)
with ZipFile(file_w, 'w', ZIP_DEFLATED) as zf:
zf.writestr("mimetype", MIMETYPE)
xml = ET.Element("Colorset")
xml.attrib['version'] = '1.0'
xml.attrib['columns'] = str(self.palette.ncols)
xml.attrib['name'] = self.palette.name
xml.attrib['comment'] = self.palette.meta.get("Comment", "Generated by Palette Editor")
for i,row in enumerate(self.palette.slots):
for j,slot in enumerate(row):
color = slot.color
name = color.name
default_name = "Swatch-{}-{}".format(i,j)
if not name:
name = default_name
elem = ET.SubElement(xml, "ColorSetEntry")
elem.attrib['spot'] = color.meta.get("Spot", "false")
elem.attrib['id'] = default_name
elem.attrib['name'] = name
elem.attrib['bitdepth'] = 'U8'
r,g,b = color.getRGB1()
srgb = ET.SubElement(elem, "sRGB")
srgb.attrib['r'] = str(r)
srgb.attrib['g'] = str(g)
srgb.attrib['b'] = str(b)
tree = ET.ElementTree(xml)
tree_str = ET.tostring(tree, encoding='utf-8', pretty_print=True, xml_declaration=False)
zf.writestr("colorset.xml", tree_str)
def build_scribus_xml(self):
# TODO: fix, and don't save here
xml = ET.Element("SCRIBUSCOLORS", NAME=name)
for i,row in enumerate(self.palette.getColors()):
for j,color in enumerate(row):
name = color.name
if not name:
name = "Swatch-{}-{}".format(i,j)
elem = ET.SubElement(xml, "COLOR", NAME=name, RGB=color.hex())
if "Spot" in color.meta:
elem.attrib["Spot"] = color.meta["Spot"]
if "Register" in color.meta:
elem.attrib["Register"] = color.meta["Register"]
ET.ElementTree(xml).write(file_w, encoding="utf-8", pretty_print=True, xml_declaration=True)
def build_android_xml(self):
# https://stackoverflow.com/questions/3769762/web-colors-in-an-android-color-xml-resource-file
palette_string = ''
# TODO: implement
return palette_string
def build_css(self):
palette_string = ''
# TODO: fix
for i, row in enumerate(self.palette.slots):
for j, slot in enumerate(row):
hex = slot.color.hex()
s = ".color-{}-{}{} {{ color: {} }};\n".format(i,j, user, hex)
palette_string += s
return palette_string