Make a palette image from palette data
-
As you can see in my example, you need to define a step variable base of the number of points.
float step=1.0/(points-1) -
jsonArr.count() ?
As the first param in the json array defines the point position, you need to convert the range 0-255 to 0.0-1.0
@mpergand but would that not count every int in the array? :S
{ "Palettes": [ { "Name": "Rainbow", "imageURL": "", "palette": [ 0, 255, 0, 0, 32, 171, 85, 0, 64, 171, 171, 0, 96, 0, 255, 0, 128, 0, 171, 85, 160, 0, 0, 255, 192, 85, 0, 171, 224, 171, 0, 85, 255, 255, 0, 0 ] } ] }
-
int amountPoint; amountPoint = jsonArr.count()/4;
gives
9
so now i can dofloat p=0,s=1.0/amountPoint;
?
-
QImage MainWindow::createPaletteImage(QJsonArray jsonArr) { int width = 500; int height = 26; QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); QPainter painter(&image); int amountPoint; amountPoint = jsonArr.count()/4; for (int i = 0; i < jsonArr.size(); i++) { float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0); } return image; }
so far correct?
now what?
-
QImage MainWindow::createPaletteImage(QJsonArray jsonArr) { int width = 500; int height = 26; QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); QPainter painter(&image); int amountPoint; amountPoint = jsonArr.count()/4; for (int i = 0; i < jsonArr.size(); i++) { float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0); } return image; }
so far correct?
now what?
-
@mpergand sooo
int amountPoint; amountPoint = jsonArr.count()/4; float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0); for (int i = 0; i < amountPoint; i++) { // but how to get the pos, r, g, b since its just a long array of ints // gradient.setColorAt(p, QColor(r,g,b)); }
-
-
am i doing something wrong here?
QImage MainWindow::createPaletteImage(QJsonArray jsonArr) { int width = 500; int height = 26; QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); QPainter painter(&image); int amountPoint; amountPoint = jsonArr.count()/4; float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0); for (int i = 0; i < amountPoint; i++) { int pos = jsonArr.at(i).toInt(); int r = jsonArr.at(i+1).toInt(); int g = jsonArr.at(i+2).toInt(); int b = jsonArr.at(i+3).toInt(); gradient.setColorAt(p, QColor(r,g,b)); } return image; }
-
@Kris-Revi said in Make a palette image from palette data:
int pos = jsonArr.at(i).toInt();
gradient.setColorAt(p, QColor(r,g,b));Apart from the really strange idea to store a image in such an extensive way you want 'pos' in the second call.
-
@Kris-Revi said in Make a palette image from palette data:
int pos = jsonArr.at(i).toInt();
gradient.setColorAt(p, QColor(r,g,b));Apart from the really strange idea to store a image in such an extensive way you want 'pos' in the second call.
@Christian-Ehrlicher said in Make a palette image from palette data:
you want 'pos' in the second call.
int pos = jsonArr.at(i).toInt(); int r = jsonArr.at(i+1).toInt(); int g = jsonArr.at(i+2).toInt(); int b = jsonArr.at(i+3).toInt(); gradient.setColorAt(pos, QColor(r,g,b));
huh like so? :S
-
for(int p=0; p<amountPoint; p++) { int pos=jsonArr[p*4]; int r=jsonArr[p*4+1]; int g=jsonArr[p*4+2]; int b=jsonArr[p*4+3]; gradient.setColorAt(pos/255.0, QColor(r,g,b)); }
QImage MainWindow::createPaletteImage(QJsonArray jsonArr) { int width = 500; int height = 26; QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); QPainter painter(&image); int amountPoint; amountPoint = jsonArr.count()/4; float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0); for (int p = 0; p < amountPoint; p++) { int pos=jsonArr[p*4].toInt(); int r=jsonArr[p*4+1].toInt(); int g=jsonArr[p*4+2].toInt(); int b=jsonArr[p*4+3].toInt(); gradient.setColorAt(pos/255.0, QColor(r,g,b)); } painter.fillRect(rect(),QBrush(gradient)); return image; }
produces
-
@mpergand all i got is
POS : 0 RGB : 255 0 0 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.12549 RGB : 171 85 0 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.25098 RGB : 171 171 0 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.376471 RGB : 0 255 0 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.501961 RGB : 0 171 85 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.627451 RGB : 0 0 255 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.752941 RGB : 85 0 171 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 0.878431 RGB : 171 0 85 GRAD : QBrush(QColor(Invalid),LinearGradientPattern) POS : 1 RGB : 255 0 0 GRAD : QBrush(QColor(Invalid),LinearGradientPattern)
-
Don't paste my code as this !
QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0);should be the image width no ?
@mpergand oh yea! now it worked :) thank you all
here is the working code and the solution
QImage MainWindow::createPaletteImage(QJsonArray jsonArr) { int width = 500; int height = 26; QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); QPainter painter(&image); int amountPoint; amountPoint = jsonArr.count()/4; float p=0,s=1.0/(amountPoint-1); QLinearGradient gradient = QLinearGradient(0,0, width, 0); for (int p = 0; p < amountPoint; p++) { int pos=jsonArr[p*4].toInt(); int r=jsonArr[p*4+1].toInt(); int g=jsonArr[p*4+2].toInt(); int b=jsonArr[p*4+3].toInt(); gradient.setColorAt(pos/255.0, QColor(r,g,b)); } painter.fillRect(rect(),QBrush(gradient)); return image; }