4545#include " ui_mainwindow.h"
4646#include " wizard.h"
4747
48+ static QColor bestContrastingTextColor (const QColor &bg) {
49+ auto channel = [](double c) {
50+ c /= 255.0 ;
51+ return (c <= 0.03928 ) ? (c / 12.92 ) : std::pow ((c + 0.055 ) / 1.055 , 2.4 );
52+ };
53+
54+ double r = channel (bg.red ());
55+ double g = channel (bg.green ());
56+ double b = channel (bg.blue ());
57+ double luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
58+
59+ double contrastBlack = (luminance + 0.05 ) / 0.05 ;
60+ double contrastWhite = (1.05 ) / (luminance + 0.05 );
61+
62+ return (contrastBlack > contrastWhite) ? QColor (Qt::black)
63+ : QColor (Qt::white);
64+ }
65+
4866/* *
4967 * A small delegate class to allow rich text rendering in main table cells.
5068 */
@@ -53,12 +71,21 @@ class SubtitleTextDelegate : public QStyledItemDelegate {
5371 const QModelIndex &index) const override {
5472 QTextDocument document;
5573 QVariant value = index.data (Qt::DisplayRole);
56- QString textColor =
57- option.state & QStyle::State_Selected
58- ? option.palette .color (QPalette::HighlightedText).name ()
59- : option.palette .color (QPalette::Text).name ();
6074
61- // Draw background with cell style
75+ // Decide text color (respecting foreground role)
76+ QColor textColor;
77+ if (option.state & QStyle::State_Selected) {
78+ textColor = option.palette .color (QPalette::HighlightedText);
79+ } else {
80+ QVariant fg = index.data (Qt::ForegroundRole);
81+ if (fg.isValid () && fg.canConvert <QColor>()) {
82+ textColor = fg.value <QColor>();
83+ } else {
84+ textColor = option.palette .color (QPalette::Text);
85+ }
86+ }
87+
88+ // Draw background only
6289 QStyleOptionViewItem opt (option);
6390 initStyleOption (&opt, index);
6491 opt.text = QString (); // remove default text drawing
@@ -74,8 +101,8 @@ class SubtitleTextDelegate : public QStyledItemDelegate {
74101 if (index.data (Qt::UserRole).toBool ()) {
75102 html = QString (" <b>%2</b>" ).arg (html);
76103 }
77- document.setHtml (
78- QString ( " <span style='color:%1;'>%2</span> " ) .arg (textColor, html));
104+ document.setHtml (QString ( " <span style='color:%1;'>%2</span> " )
105+ .arg (textColor. name () , html));
79106
80107 QFont font = document.defaultFont ();
81108 font.setPointSizeF (font.pointSizeF () * m_zoomFactor);
@@ -151,6 +178,18 @@ MainWindow::MainWindow(QWidget *parent)
151178 m_windowShown(false ) {
152179 ui->setupUi (this );
153180 m_defaultPalette = qApp->palette ();
181+
182+ // Precompute contrasting text colors
183+ QColor offColor = m_defaultPalette.color (QPalette::Base);
184+ QColor onColor = m_defaultPalette.color (QPalette::Highlight).lighter (130 );
185+ QColor nextColor = m_defaultPalette.color (QPalette::Highlight).lighter (170 );
186+ m_itemColorOffBackground = QBrush (offColor);
187+ m_itemColorOnBackground = QBrush (onColor);
188+ m_itemColorNextBackground = QBrush (nextColor);
189+ m_itemColorOffText = bestContrastingTextColor (offColor);
190+ m_itemColorOnText = bestContrastingTextColor (onColor);
191+ m_itemColorNextText = bestContrastingTextColor (nextColor);
192+
154193 ui->tableWidget ->setItemDelegateForColumn (COLUMN_START,
155194 new SubtitleDurationDelegate ());
156195 ui->tableWidget ->setItemDelegateForColumn (COLUMN_END,
@@ -1141,15 +1180,12 @@ void MainWindow::subtitleChanged(QList<Subtitle *> p_currentSubtitles) {
11411180}
11421181
11431182void MainWindow::highlightSubtitles (qlonglong elapsed) {
1144- QBrush off = QBrush (qApp->palette ().color (QPalette::Base));
1145- QBrush on = QBrush (qApp->palette ().color (QPalette::Highlight).lighter (130 ));
1146- QBrush next = QBrush (qApp->palette ().color (QPalette::Highlight).lighter (170 ));
1147-
1148- // First reset all
1183+ // Reset all
11491184 for (int row = 0 ; row < ui->tableWidget ->rowCount (); row++) {
11501185 for (int col = 0 ; col < ui->tableWidget ->columnCount (); col++) {
11511186 QTableWidgetItem *item = ui->tableWidget ->item (row, col);
1152- item->setBackground (off);
1187+ item->setBackground (m_itemColorOffBackground);
1188+ item->setForeground (m_itemColorOffText);
11531189 QFont f = item->font ();
11541190 f.setBold (false );
11551191 item->setFont (f);
@@ -1158,16 +1194,18 @@ void MainWindow::highlightSubtitles(qlonglong elapsed) {
11581194 }
11591195 }
11601196
1161- // Then highlight next subtitles
11621197 if (m_script) {
1198+ // Highlight next subtitles
11631199 foreach (Subtitle *e, m_script->nextSubtitles (elapsed)) {
11641200 int row = m_tableMapping[e];
11651201 for (int col = 0 ; col < ui->tableWidget ->columnCount (); col++) {
11661202 QTableWidgetItem *item = ui->tableWidget ->item (row, col);
1167- item->setBackground (next);
1203+ item->setBackground (m_itemColorNextBackground);
1204+ item->setForeground (m_itemColorNextText);
11681205 }
11691206 }
1170- // Finally highlight current subtitles
1207+
1208+ // Highlight current subtitles
11711209 foreach (Subtitle *e, m_currentSubtitles) {
11721210 int row = m_tableMapping[e];
11731211 for (int col = 0 ; col < ui->tableWidget ->columnCount (); col++) {
@@ -1177,7 +1215,8 @@ void MainWindow::highlightSubtitles(qlonglong elapsed) {
11771215 f.setBold (true );
11781216 item->setFont (f);
11791217 }
1180- item->setBackground (on);
1218+ item->setBackground (m_itemColorOnBackground);
1219+ item->setForeground (m_itemColorOnText);
11811220 if (col == COLUMN_TEXT)
11821221 item->setData (Qt::UserRole, !ui->actionHide ->isChecked ());
11831222 }
0 commit comments