Skip to content

Commit 8a01ae2

Browse files
committed
Change label text colors for more readable ones
1 parent e267e7e commit 8a01ae2

5 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/label_widget.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void LabelWidget::setNewLabel(const LabelInfo &label) {
1919

2020
setText(text);
2121
setAlignment(Qt::AlignHCenter);
22-
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + invColor(_label.color).name() + "; font: bold 14px; }");
22+
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + readableColor(_label.color).name() + "; font: 14px; }");
2323
}
2424

2525
void LabelWidget::setSelected(bool s) {
@@ -40,4 +40,8 @@ void LabelWidget::paintEvent(QPaintEvent *event) {
4040
p.drawRect(rect());
4141
p.end();
4242
}
43+
}
44+
45+
QString LabelWidget::getName(){
46+
return this->_label.name;
4347
}

src/label_widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class LabelWidget : public QLabel {
1818

1919
void setNewLabel(const LabelInfo &label);
2020
void setSelected(bool s);
21+
QString getName();
2122

2223
public : // override
2324
void paintEvent(QPaintEvent *event) override;

src/main_window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void MainWindow::changeLabel(QListWidgetItem* current, QListWidgetItem* previous
177177
label->setSelected(true);
178178

179179
QString str;
180-
QString key = label->text();
180+
QString key = label->getName();
181181
QTextStream sstr(&str);
182182
sstr <<"label=["<< key <<"] id=[" << labels[key].id << "] categorie=[" << labels[key].categorie << "] color=[" << labels[key].color.name() << "]" ;
183183
statusBar()->showMessage(str);

src/utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result
4747
}
4848
}
4949

50+
QColor readableColor(const QColor & color)
51+
{
52+
int r, g, b;
53+
color.getRgb(&r, &g, &b);
54+
55+
if ((r*0.299 + g*0.587 + b*0.114) > 150)
56+
return QColor(0, 0, 0);
57+
58+
return QColor(255, 255, 255);
59+
60+
}
5061
QColor invColor(const QColor& color) {
5162
int h, s, v;
5263
color.getHsv(&h, &s, &v);

src/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ QImage idToColor(const QImage &image_id, const Id2Labels& id_label);
1313
void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result);
1414
inline bool operator<(const QColor & a, const QColor & b) { return a.rgb() < b.rgb(); }
1515
QColor invColor(const QColor & color);
16+
QColor readableColor(const QColor & color);
1617
QVector<QColor> colorMap(int size);
1718
cv::Mat convertMat32StoRGBC3(const cv::Mat &mat);
1819
QImage watershed(const QImage& qimage, const QImage & qmarkers_mask);

0 commit comments

Comments
 (0)