File indexing completed on 2025-01-26 04:11:38

0001 # A script that converts the palette with the given name to a gimp
0002 # palette at the location asked for.
0003 
0004 # By Wolthera(originally)
0005 
0006 # SPDX-License-Identifier: CC0-1.0
0007 
0008 # @package palette_docker
0009 
0010 
0011 # Importing the relevant dependencies:
0012 from PyQt5.QtWidgets import QFileDialog, QMessageBox
0013 from krita import Palette
0014 
0015 
0016 class gimpPaletteExporter:
0017 
0018     def __init__(self, name):
0019         # We want people to select a palette and a location to save to...
0020         self.fileName = QFileDialog.getExistingDirectory()
0021         allPalettes = Application.resources("palette")
0022         self.paletteName = name
0023         self.currentPalette = Palette(allPalettes[self.paletteName])
0024         self.export()
0025         done = QMessageBox()
0026         done.setWindowTitle(i18n("Export Successful"))
0027         done.setText(
0028             str(i18n("{input} has been exported to {output}.")).format(
0029                 input=self.paletteName, output=self.fileName))
0030         done.exec_()
0031         pass
0032 
0033     def export(self):
0034         # open the appropriate file...
0035         gplFile = open(self.fileName + "/" + self.paletteName + ".gpl", "w")
0036         gplFile.write("GIMP Palette\n")
0037         gplFile.write("Name: %s\n" % self.paletteName)
0038         gplFile.write("Columns: %s/n", self.currentPalette.columnCount())
0039         gplFile.write("#%s\n" % self.currentPalette.comment())
0040         colorCount = self.currentPalette.colorsCountGroup("")
0041 
0042         for i in range(colorCount):
0043             entry = self.currentPalette.colorSetEntryFromGroup(i, "")
0044             color = self.currentPalette.colorForEntry(entry)
0045             # convert to sRGB
0046             color.setColorSpace("RGBA", "U8", "sRGB built-in")
0047 
0048             red = max(min(int(color.componentsOrdered()[0] * 255), 255), 0)
0049             green = max(min(int(color.componentsOrdered()[1] * 255), 255), 0)
0050             blue = max(min(int(color.componentsOrdered()[2] * 255), 255), 0)
0051             gplFile.write(
0052                 "{red} {green} {blue}    {id}-{name}\n".format(
0053                     red=red, green=green, blue=blue, id=entry.id(),
0054                     name=entry.name))
0055             groupNames = self.currentPalette.groupNames()
0056             for groupName in groupNames:
0057                 colorCount = self.currentPalette.colorsCountGroup(groupName)
0058                 for i in range(colorCount):
0059                     entry = self.currentPalette.colorSetEntryFromGroup(
0060                         i, groupName)
0061                     color = self.currentPalette.colorForEntry(entry)
0062                     # convert to sRGB
0063                     color.setColorSpace("RGBA", "U8", "sRGB built-in")
0064                     red = max(
0065                         min(int(color.componentsOrdered()[0] * 255), 255), 0)
0066                     green = max(
0067                         min(int(color.componentsOrdered()[1] * 255), 255), 0)
0068                     blue = max(
0069                         min(int(color.componentsOrdered()[2] * 255), 255), 0)
0070                     gplFile.write(
0071                         "{red} {green} {blue}    {id}-{name}\n".format(
0072                             red=red, green=green, blue=blue, id=entry.id(),
0073                             name=entry.name))
0074         gplFile.close()