Skip to content

Commit

Permalink
Color constrast on table
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Nov 8, 2022
1 parent 0bcb75f commit 22d3492
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/gui/raster/qgsloadrasterattributetabledialog.cpp
Expand Up @@ -30,13 +30,15 @@ QgsLoadRasterAttributeTableDialog::QgsLoadRasterAttributeTableDialog( QgsRasterL
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );

connect( mDbfPathWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString )
connect( mDbfPathWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & )
{
updateButtons();
} );

mRasterBand->setLayer( mRasterLayer );

mDbfPathWidget->setFilter( QStringLiteral( "VAT DBF Files (*.vat.dbf)" ) );

updateButtons();

QgsGui::enableAutoGeometryRestore( this );
Expand Down
7 changes: 3 additions & 4 deletions src/gui/raster/qgsrasterattributetablemodel.cpp
Expand Up @@ -341,10 +341,9 @@ QVariant QgsRasterAttributeTableModel::data( const QModelIndex &index, int role
case Qt::ItemDataRole::ForegroundRole:
{
// Choose black or white for a decent contrast.
// From https://forum.qt.io/topic/106362/best-way-to-set-text-color-for-maximum-contrast-on-background-color/3
const QColor tempColor { mRat->color( index.row() ).darker( 1 ) }; // Force conversion to color QML type object
const double a = 1 - ( 0.299 * tempColor.redF() + 0.587 * tempColor.greenF() + 0.114 * tempColor.blueF() );
return ( tempColor.alphaF() > 0 && a >= 0.3 ) ? QColor( Qt::GlobalColor::black ) : QColor( Qt::GlobalColor::white );
const QColor tempColor { mRat->color( index.row() ) };
const double darkness { 1 - ( 0.299 * tempColor.red() + 0.587 * tempColor.green() + 0.114 * tempColor.blue() ) / 255};
return darkness > 0.5 ? QColor( Qt::GlobalColor::white ) : QColor( Qt::GlobalColor::black );
}
case Qt::ItemDataRole::EditRole:
case Qt::ItemDataRole::BackgroundRole:
Expand Down

0 comments on commit 22d3492

Please sign in to comment.