/** * Load a palette from a GIMP palette file. * * The file format is: * * GIMP Palette * *HEADER FIELDS* * # one or more comment * r g b name * ... * * @param filename palette file name * @param writeprotected is the source file read only */ Palette *Palette::fromFile(const QFileInfo& file, bool readonly, QObject *parent) { QFile palfile(file.absoluteFilePath()); if (!palfile.open(QIODevice::ReadOnly | QIODevice::Text)) return nullptr; QTextStream in(&palfile); if(in.readLine() != "GIMP Palette") return nullptr; Palette *pal = new Palette(file.baseName(), file.absoluteFilePath(), !file.isWritable() | readonly, parent); const QRegularExpression colorRe("^(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*(.+)?$"); do { QString line = in.readLine().trimmed(); if(line.isEmpty() || line.at(0) == '#') { // ignore comments and empty lines } else if(line.startsWith("Name:")) { pal->_name = line.mid(5).trimmed(); } else if(line.startsWith("Columns:")) { bool ok; int cols = line.mid(9).trimmed().toInt(&ok); if(ok && cols>0) pal->_columns = cols; } else { QRegularExpressionMatch m = colorRe.match(line); if(m.hasMatch()) { pal->_colors.append(PaletteColor( QColor( m.captured(1).toInt(), m.captured(2).toInt(), m.captured(3).toInt() ), m.captured(4) ) ); } else { qWarning() << "unhandled line" << line << "in" << file.fileName(); } } } while(!in.atEnd()); // Palettes loaded from file are write-protected by default pal->_writeprotect = true; return pal; }
/** * Load a palette from a GIMP palette file. * * The file format is: * * GIMP Palette * *HEADER FIELDS* * # one or more comment * r g b name * ... * * @param filename palette file name */ Palette Palette::fromFile(const QFileInfo& file) { QFile palfile(file.absoluteFilePath()); if (!palfile.open(QIODevice::ReadOnly | QIODevice::Text)) return Palette(); QTextStream in(&palfile); if(in.readLine() != "GIMP Palette") return Palette(); Palette pal(file.baseName(), file.fileName()); const QRegularExpression colorRe("^(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*(.+)?$"); do { QString line = in.readLine().trimmed(); if(line.isEmpty() || line.at(0) == '#') { // ignore comments and empty lines } else if(line.startsWith("Name:")) { pal._name = line.mid(5).trimmed(); } else if(line.startsWith("Columns:")) { bool ok; int cols = line.mid(9).trimmed().toInt(&ok); if(ok && cols>0) pal._columns = cols; } else { QRegularExpressionMatch m = colorRe.match(line); if(m.hasMatch()) { pal.appendColor( QColor( m.captured(1).toInt(), m.captured(2).toInt(), m.captured(3).toInt() ), m.captured(4) ); } else { qWarning() << "unhandled line" << line << "in" << file.fileName(); } } } while(!in.atEnd()); return pal; }