Krita Source Code Documentation
Loading...
Searching...
No Matches
palette_exporter_gimppalette.py
Go to the documentation of this file.
1# A script that converts the palette with the given name to a gimp
2# palette at the location asked for.
3
4# By Wolthera(originally)
5
6# SPDX-License-Identifier: CC0-1.0
7
8# @package palette_docker
9
10
11# Importing the relevant dependencies:
12try:
13 from PyQt6.QtWidgets import QMessageBox
14except:
15 from PyQt5.QtWidgets import QMessageBox
16from krita import Palette, FileDialog
17from builtins import Application, i18n
18
19
21
22 def __init__(self, name):
23 # We want people to select a palette and a location to save to...
25 if not self.fileName: return
26 allPalettes = Application.resources("palette")
27 self.paletteName = name
28 self.currentPalette = Palette(allPalettes[self.paletteName])
29 self.export()
30 done = QMessageBox()
31 done.setWindowTitle(i18n("Export Successful"))
32 done.setText(
33 str(i18n("{input} has been exported to {output}.")).format(
34 input=self.paletteName, output=self.fileName))
35 done.exec()
36 pass
37
38 def export(self):
39 # open the appropriate file...
40 gplFile = open(self.fileName + "/" + self.paletteName + ".gpl", "w")
41 gplFile.write("GIMP Palette\n")
42 gplFile.write("Name: %s\n" % self.paletteName)
43 gplFile.write("Columns: %s\n" % self.currentPalette.columnCount())
44 gplFile.write("#%s\n" % self.currentPalette.comment())
45
46 groupNames = self.currentPalette.groupNames()
47 for groupName in groupNames:
48 slotCount = self.currentPalette.slotCountGroup(groupName)
49 for i in range(slotCount):
50 entry = self.currentPalette.entryByIndexFromGroup(i, groupName)
51 color = entry.color()
52 # convert to sRGB
53 color.setColorSpace("RGBA", "U8", "sRGB built-in")
54 red = max(min(int(color.componentsOrdered()[0] * 255), 255), 0)
55 green = max(min(int(color.componentsOrdered()[1] * 255), 255), 0)
56 blue = max(min(int(color.componentsOrdered()[2] * 255), 255), 0)
57 name = f"{entry.id()}-{entry.name()}" if entry.id() else entry.name()
58 gplFile.write(
59 "{red:>3} {green:>3} {blue:>3} {name}\n".format(
60 red=red, green=green, blue=blue, name=name))
61 gplFile.close()
static QString getExistingDirectory(QWidget *parent=nullptr, const QString &caption=QString(), const QString &directory=QString(), const QString &dialogName=QString())
Create and show a file dialog and return the name of an existing directory selected by the user.