25 #include <QTextStream> 43 return index >= 0 && index < colors.size();
93 : QObject(),
p ( other.
p )
146 p->
colors.reserve(color_table.size());
147 for ( QRgb
c : color_table )
151 p->
colors.push_back(qMakePair(color,QString()));
159 if ( image.isNull() )
164 p->
colors.reserve(image.width()*image.height());
165 for (
int y = 0; y < image.height(); y++ )
167 for (
int x = 0; x < image.width(); x++ )
169 QColor color ( image.pixel(x, y) );
171 p->
colors.push_back(qMakePair(color,QString()));
192 p->
name = QFileInfo(name).baseName();
196 if ( !file.open(QFile::ReadOnly|QFile::Text) )
202 QTextStream stream( &file );
204 if ( stream.readLine() !=
"GIMP Palette" )
213 QHash<QString,QString> properties;
214 while( !stream.atEnd() )
216 line = stream.readLine();
217 if ( line.isEmpty() )
219 if ( line[0] ==
'#' )
221 int colon = line.indexOf(
':');
224 properties[line.left(colon).toLower()] =
225 line.right(line.size() - colon - 1).trimmed();
232 if ( !stream.atEnd() && line[0] ==
'#' )
233 while( !stream.atEnd() )
235 qint64 pos = stream.pos();
236 line = stream.readLine();
237 if ( !line.isEmpty() && line[0] !=
'#' )
244 while( !stream.atEnd() )
246 int r = 0, g = 0, b = 0;
247 stream >> r >> g >> b;
248 line = stream.readLine().trimmed();
249 p->
colors.push_back(qMakePair(QColor(r, g, b), line));
274 if ( filename.isEmpty() )
279 QFile file(filename);
280 if ( !file.open(QFile::Text|QFile::WriteOnly) )
283 QTextStream stream(&file);
285 stream <<
"GIMP Palette\n";
288 stream <<
"Columns: " <<
p->
columns <<
'\n';
292 for (
int i = 0; i <
p->
colors.size(); i++ )
294 stream << qSetFieldWidth(3) <<
p->
colors[i].first.red() << qSetFieldWidth(0) <<
' ' 295 << qSetFieldWidth(3) <<
p->
colors[i].first.green() << qSetFieldWidth(0) <<
' ' 296 << qSetFieldWidth(3) <<
p->
colors[i].first.blue() << qSetFieldWidth(0) <<
'\t' 331 foreach(
const QColor& col, colors)
332 p->
colors.push_back(qMakePair(col,QString()));
350 p->
colors[index].first = color;
362 p->
colors[index].first = color;
384 p->
colors.push_back(qMakePair(color,name));
392 if ( index < 0 || index >
p->
colors.size() )
395 p->
colors.insert(index, qMakePair(color, name));
428 return name.isEmpty() ? tr(
"Unnamed") :
name;
434 if ( !size.isValid() ||
p->
colors.empty() )
438 out.fill(background);
439 QPainter painter(&out);
444 columns = std::ceil( std::sqrt( count *
float(size.width()) / size.height() ) );
445 int rows = std::ceil(
float(count) / columns );
446 QSizeF color_size(
float(size.width()) / columns,
float(size.height()) / rows);
448 for (
int y = 0, i = 0; y < rows && i <
count; y++ )
450 for (
int x = 0; x < columns && i <
count; x++, i++ )
452 painter.fillRect(QRectF(x*color_size.width(), y*color_size.height(),
453 color_size.width(), color_size.height()),
477 for (
int i = 0; i <
p->
colors.size(); i++ )
478 out.push_back(
p->
colors[i].first);
486 for (
const auto& color_pair :
p->
colors )
487 out.push_back(color_pair.first.rgba());