23#ifdef HAVE_SYS_TYPES_H
27#include <netinet/in.h>
34#include <tqvaluevector.h>
36#include <tqtextstream.h>
41#include "kis_debug_areas.h"
42#include "kis_palette.h"
43#include "kis_iterators_pixel.h"
47 enum enumPaletteType {
58 : super(TQString(
"")),
61 Q_ASSERT(nColors > 0);
62 Q_ASSERT(!
img->isNull());
70 : super(TQString(
"")),
73 Q_ASSERT(nColors > 0);
74 Q_ASSERT(device != 0);
83 : super(TQString(
"")),
86 Q_ASSERT(nColors > 0);
87 Q_ASSERT(gradient != 0);
93 dx = 1.0 / (nColors - 1);
96 for (i = 0, cur_x = 0; i < nColors; i++, cur_x += dx) {
97 gradient->colorAt(cur_x, &e.color, &opacity);
122 setFilename(rhs.filename());
126 m_comment = rhs.m_comment;
127 m_columns = rhs.m_columns;
128 m_colors = rhs.m_colors;
132KisPalette::~KisPalette()
138 TQFile file(filename());
139 file.open(IO_ReadOnly);
140 m_data = file.readAll();
148 TQFile file(filename());
149 if (!file.open(IO_WriteOnly | IO_Truncate)) {
153 TQTextStream stream(&file);
156 stream <<
"GIMP Palette\nName: " << name() <<
"\nColumns: " << m_columns <<
"\n#\n";
158 for (uint i = 0; i < m_colors.size(); i++) {
159 const KisPaletteEntry& entry = m_colors.at(i);
160 TQColor c = entry.color;
161 stream << c.red() <<
" " << c.green() <<
" " << c.blue() <<
"\t";
162 if (entry.name.isEmpty())
163 stream <<
"Untitled\n";
165 stream << entry.name <<
"\n";
177TQ_INT32 KisPalette::nColors()
179 return m_colors.count();
182bool KisPalette::init()
184 enumPaletteType format = FORMAT_UNKNOWN;
186 TQString s = TQString::fromUtf8(m_data.data(), m_data.count());
188 if (s.isEmpty() || s.isNull() || s.length() < 50) {
189 kdWarning(DBG_AREA_FILE) <<
"Illegal Gimp palette file: " << filename() <<
"\n";
194 if (s.startsWith(
"RIFF") || s.startsWith(
"PAL data"))
198 else if (s.startsWith(
"GIMP Palette"))
203 TQStringList lines = TQStringList::split(
"\n", s);
205 if (lines.size() < 3) {
209 TQString entry, channel, columns;
218 if (!lines[1].startsWith(
"Name: ") || !lines[0].startsWith(
"GIMP") )
220 kdWarning(DBG_AREA_FILE) <<
"Illegal Gimp palette file: " << filename() <<
"\n";
224 setName(i18n(lines[1].mid(strlen(
"Name: ")).stripWhiteSpace().ascii()));
229 if (lines[index].startsWith(
"Columns: ")) {
230 columns = lines[index].mid(strlen(
"Columns: ")).stripWhiteSpace();;
231 m_columns = columns.toInt();
235 for (TQ_UINT32 i = index; i < lines.size(); i++) {
236 if (lines[i].startsWith(
"#")) {
237 m_comment += lines[i].mid(1).stripWhiteSpace() +
" ";
239 else if (!lines[i].isEmpty())
241 TQStringList a = TQStringList::split(
" ", lines[i].replace(TQChar(
'\t'),
" "));
255 if (r < 0 || r > 255 ||
262 color = TQColor(r, g, b);
265 TQString name = a.join(
" ");
266 e.name = name.isEmpty() ? i18n(
"Untitled") : name;
274 else if (s.length() == 768) {
275 kdWarning(DBG_AREA_FILE) <<
"Photoshop format palette file. Not implemented yet\n";
282void KisPalette::add(
const KisPaletteEntry & c)
284 m_colors.push_back(c);
287void KisPalette::remove(
const KisPaletteEntry & c)
289 TQValueVector<KisPaletteEntry>::iterator it = m_colors.begin();
290 TQValueVector<KisPaletteEntry>::iterator end = m_colors.end();
301KisPaletteEntry KisPalette::getColor(TQ_UINT32 index)
303 return m_colors[index];
306#include "kis_palette.moc"
virtual bool load()
Load this resource.
KisPalette(const TQImage *img, TQ_INT32 nColors, const TQString &name)
Create a palette from the colours in an image.
KisPalette()
Create an empty palette.
virtual TQImage img()
Returns a TQImage representing this resource.
virtual bool save()
Save this resource asynchronously.