How to include .txt on Qt
-
My code is : (cpp)
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPixmap> #include <QImage> #include <QFileDialog> #include <QColor> #include <QPoint> #include <QSize> #include <iostream> using namespace std; QColor Couleurdominante(const QImage& image, const QPoint& topLeft, const QSize& rectSize); void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectSize, const QColor& colour); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_push_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)")); QPixmap pix(fileName); ui->label->setPixmap(pix); const QSize s = pix.size(); pixi = QImage(pix.toImage()); ui->label_2->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) ); } void MainWindow::on_push2_clicked() { for (int i=0;i<pixi.width();i+=a){ for (int j=0;j<pixi.height();j+=b){ Remplissage(pixi,QPoint(i,j),QSize(a,b),Couleurdominante(pixi,QPoint(i,j),QSize(a,b))); }} pixa=QPixmap::fromImage(pixi); ui->label_3->setPixmap(pixa); } void Remplissage(QImage& image, const QPoint& topLeft, const QSize& rectangle, const QColor& colour) { int maxX = topLeft.x() + rectangle.width(); int maxY = topLeft.y() + rectangle.height(); for(int x = topLeft.x(); x < maxX; ++x) { for(int y = topLeft.y(); y < maxY; ++y) { image.setPixelColor(x, y, colour); QMapIterator<QRgb, CostInfo >i; while (i.hasNext()) { i.next(); QColor BaseColor( i.key() ); if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) ) { CostInfo& ci = Costs[colour.rgb()]; int Cost = ci.Cost; QPixmap pix( ci.ImageName ); } } } } } QColor Couleurdominante(const QImage & image,const QPoint & topLeft, const QSize & rectangle) { int rouge = 0, vert = 0, bleue = 0; int X = topLeft.x() + rectangle.width(); int Y = topLeft.y() + rectangle.height(); for (int y = topLeft.y(); y < Y; y++) { for (int x = topLeft.x(); x < X; x++) { QRgb pixel = image.pixel(x, y); rouge += qRed(pixel); vert += qGreen(pixel); bleue += qBlue(pixel); } } int n = rectangle.width() * rectangle.height(); Q_ASSERT(n); if (n <= 0) return Qt::black; return QColor(rouge / n, vert / n, bleue / n); } QMap<QRgb, CostInfo > Costs = { { QColor(255 , 0 , 0 ).rgb(), { "://fraise.png", 10 }}, { QColor(0 , 255 , 0 ).rgb(), { "://balleverte.png", 20 }}, { QColor(0 , 0 , 255 ).rgb(), { "://ballebleue.png", 20 }}, { QColor(255 , 255 , 255 ).rgb(), { "://balleblanche.png", 20 }}, { QColor(255 , 128 , 0 ).rgb(), { "://ballepeche.png", 20 }}, { QColor(0 , 0 , 0 ).rgb(), { "://noir.png", 20 }}, { QColor(102 , 51 , 0 ).rgb(), { "://marron.png", 20 }}, { QColor(255 , 102 , 78 ).rgb(), { "://rose.png", 20 }}, { QColor(0 , 204 , 204 ).rgb(), { "://turquoise.png", 20 }}, { QColor(255 , 178 , 102 ).rgb(), { "://beige.png", 20 }}, { QColor(76 , 0 , 153 ).rgb(), { "://violet.png", 20 }}, { QColor(100 , 100 , 100 ).rgb(), { "://gris.png", 20 }}, }; bool IsCloseColor( QColor c1, QColor c2 ) { int diffRed = Math.abs(c1.red() - c2.red()); int diffGreen = Math.abs(c1.green() - c2.green()); int diffBlue = Math.abs(c1.blue() - c2.blue()); if (diffBlue+diffRed+diffGreen< 100){ / return c1; } }
i took z = 100. For the moment
.h :
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPixmap> #include <QPoint> #include <QSize> #include <iostream> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_push_clicked(); struct CostInfo { QString ImageName; int Cost; }; void on_push2_clicked(); void on_verticalSlider_sliderMoved(int position); private: Ui::MainWindow *ui; QImage pixi; QPixmap pixa; int a; int b; }; #endif // MAINWINDOW_H
-
Hi
In your
bool IsCloseColor( QColor c1, QColor c2 ) {
int diffRed = Math.abs(c1.red() - c2.red());
int diffGreen = Math.abs(c1.green() - c2.green());
int diffBlue = Math.abs(c1.blue() - c2.blue());if (diffBlue+diffRed+diffGreen< 100){ / return c1; <<<<<< you should return true else //////////////////you need this part also return false; /////////
-
Thank you i corrected.
And for my other error ?
in : if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) )
for the function IsCloseColor, its not better to return a color ? like QColor IsCloseColor();, i don't understand what we can do if we return "false" or "true"
-
It just checks if the color is "close " to base color
No reason to return color ?
we say
if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) )So if we have direct match OR
the colour is close to BaseColor then we think its ok. -
@Payx
No it cant..
IsCloseColor tells if 2 colors are close. It return true/false.
it dont return any color. it tells you if they color u have "in the hand" matches the
base color given.
You already have the color (colour var) so no reason to return it. -
Okay, so it's useless to write :
bool IsCloseColor( QColor c1, QColor c2 ) { int diffRed = Math.abs(c1.red() - c2.red()); int diffGreen = Math.abs(c1.green() - c2.green()); int diffBlue = Math.abs(c1.blue() - c2.blue()); if (diffBlue+diffRed+diffGreen< 100){ / return true;. else return false; } }
So if i understood well the "<100" is now useless. Can't we just return the color who have the minimum diffBlue+diffRed+diffGreen ?
-
@Payx If you want to return the closest color from a list then you need to write a function similar to IsCloseColor. You iterate over the list, calculate the diff and store the color with the smallest diff and its diff in local variables, then return the color from that local variable.
-
@Payx
No sure what "browse a list is"QList<QColor> ColorList; ColorList << QColor(100,100,100) << QColor(200,200,200) << QColor(0,0,100) ; for( int c=0; c < ColorList.size();c++ ) { QColor CurCol=ColorList[c]; // take from list // ... do what u want }
-
like compare all the color with a for
you said it to me ^^
@Payx
No. use a list (of colors) and loop over it, checking each.i already have this (you give it to me)
QMap<QRgb, CostInfo > Costs = { { QColor(255 , 0 , 0 ).rgb(), { "://fraise.png", 10 }}, { QColor(0 , 255 , 0 ).rgb(), { "://balleverte.png", 20 }}, { QColor(0 , 0 , 255 ).rgb(), { "://ballebleue.png", 20 }}, { QColor(255 , 255 , 255 ).rgb(), { "://balleblanche.png", 20 }}, { QColor(255 , 128 , 0 ).rgb(), { "://ballepeche.png", 20 }}, { QColor(0 , 0 , 0 ).rgb(), { "://noir.png", 20 }}, { QColor(102 , 51 , 0 ).rgb(), { "://marron.png", 20 }}, { QColor(255 , 102 , 78 ).rgb(), { "://rose.png", 20 }}, { QColor(0 , 204 , 204 ).rgb(), { "://turquoise.png", 20 }}, { QColor(255 , 178 , 102 ).rgb(), { "://beige.png", 20 }}, { QColor(76 , 0 , 153 ).rgb(), { "://violet.png", 20 }}, { QColor(100 , 100 , 100 ).rgb(), { "://gris.png", 20 }}, }; bool IsCloseColor( QColor c1, QColor c2 ) { int diffRed = Math.abs(c1.red() - c2.red()); int diffGreen = Math.abs(c1.green() - c2.green()); int diffBlue = Math.abs(c1.blue() - c2.blue()); if (diffBlue+diffRed+diffGreen< 100){ / return true;. else return false; } }
the function i want to create is to browse the Qmap and pick the color who got the minimum ""diffBlue+diffRed+diffGreen< 100""
-
Hi
ok. i see. we had the code higher up.Qcolor colour; // the one you compare too. set to something...
QMapIterator<QRgb, CostInfo >i(Costs);
while (i.hasNext()) {
i.next();
QColor BaseColor( i.key() ); // the color in table
if (Costs.contains( colour.rgb() ) || IsCloseColor(BaseColor, colour) ) { // directly have colour or it is < 100
//// we got a match
}
}// while