1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "ColorPool.h"
#include "../../SyntopiaCore/Logging/Logging.h"
#include "../../SyntopiaCore/Exceptions/Exception.h"
#include "Builder.h"
#include "RandomStreams.h"
#include <QFile>
#include <QFileInfo>
using namespace SyntopiaCore::Logging;
using namespace SyntopiaCore::Exceptions;
namespace StructureSynth {
namespace Model {
ColorPool::ColorPool(QString initString) {
initString = initString.toLower();
picture = 0;
if (initString == "randomhue") {
type = RandomHue;
} else if (initString == "greyscale" || initString == "grayscale" ) {
type = GreyScale;
} else if (initString == "randomrgb") {
type = RandomRGB;
} else if (initString.startsWith("image:")) {
initString = initString.remove("image:");
type = Picture;
if (!QFile::exists(initString)) {
throw Exception(QString("Could not open file: %1").arg(QFileInfo(initString).absoluteFilePath()));
}
picture = new QImage(initString);
if (picture->isNull()) {
throw Exception(QString("Could not parse image file: %1").arg(QFileInfo(initString).absoluteFilePath()));
}
} else if (initString.startsWith("list:")) {
initString = initString.remove("list:");
QStringList l = initString.split(",");
for (int i = 0; i < l.count(); i++) {
QColor c(l[i]);
if (!c.isValid()) {
throw Exception(QString("Could not parse color in colorlist: %1").arg(initString));
}
colorList.append(c);
}
type = ColorList;
} else {
throw Exception(QString("Could not understand the color pool: %1. Try: RandomHue, RandomRGB, GrayScale, Image:test.png, List:#234,Red,Blue").arg(initString));
}
}
ColorPool::~ColorPool() {
delete picture;
}
QColor ColorPool::drawColor() {
if (type == RandomHue) {
return QColor::fromHsv(RandomStreams::Color()->getInt(359),255,255);
} else if (type == GreyScale) {
int r = RandomStreams::Color()->getInt(255);
return QColor(r,r,r).toHsv();
} else if (type == RandomRGB) {
// We can only pull one random number, so we must use a few tricks to get three ints
int r = RandomStreams::Color()->getInt(255);
int g = RandomStreams::Color()->getInt(255);
int b = RandomStreams::Color()->getInt(255);
return QColor(r,g,b).toHsv();
} else if (type == Picture) {
int x = RandomStreams::Color()->getInt(picture->width()-1);
int y = RandomStreams::Color()->getInt(picture->height()-1);
QRgb rgb = picture->pixel(x,y);
return QColor(rgb).toHsv();
} else if (type == ColorList) {
int id = RandomStreams::Color()->getInt(colorList.count()-1);
return colorList[id];
}
return QColor();
}
}
}
|