From 79949246157362ffa57fced5a1dc95dd24e48aa7 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Tue, 17 Oct 2023 11:35:36 +0200 Subject: [PATCH] Regression Curves: Use a contrast color for the regression curves Use a list of predefined colors, and select the color with the larges RGB difference to the source curve. This will ensure that we avoid ending up with fully white or fully black curves, unable to see. --- .../Application/Tools/RiaColorTools.cpp | 24 +++++++++++++++++++ .../Application/Tools/RiaColorTools.h | 2 ++ ...icCreateRegressionAnalysisCurveFeature.cpp | 20 +++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/ApplicationLibCode/Application/Tools/RiaColorTools.cpp b/ApplicationLibCode/Application/Tools/RiaColorTools.cpp index ac69ef10f87..5e21f255946 100644 --- a/ApplicationLibCode/Application/Tools/RiaColorTools.cpp +++ b/ApplicationLibCode/Application/Tools/RiaColorTools.cpp @@ -136,6 +136,30 @@ cvf::Color3f RiaColorTools::fromQColorTo3f( QColor color ) return cvf::Color3f( color.redF(), color.greenF(), color.blueF() ); } +//-------------------------------------------------------------------------------------------------- +/// Find the color with larges distance to the given color based on RGB distance +//-------------------------------------------------------------------------------------------------- +cvf::Color3f RiaColorTools::selectContrastColorFromCandiates( cvf::Color3f color, const cvf::Color3fArray& candidates ) +{ + if ( candidates.size() == 0 ) return color; + + float maxDiff = 0.0f; + cvf::Color3f selectedColor = color; + + for ( const auto& candidate : candidates ) + { + const auto diff = std::fabs( color.r() - candidate.r() ) + std::fabs( color.g() - candidate.g() ) + + std::fabs( color.b() - candidate.b() ); + if ( diff > maxDiff ) + { + maxDiff = diff; + selectedColor = candidate; + } + } + + return selectedColor; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/Application/Tools/RiaColorTools.h b/ApplicationLibCode/Application/Tools/RiaColorTools.h index a1469711c55..7052d5493ae 100644 --- a/ApplicationLibCode/Application/Tools/RiaColorTools.h +++ b/ApplicationLibCode/Application/Tools/RiaColorTools.h @@ -42,6 +42,8 @@ class RiaColorTools static QColor toQColor( cvf::Color4f color ); static cvf::Color3f fromQColorTo3f( QColor ); + static cvf::Color3f selectContrastColorFromCandiates( cvf::Color3f color, const cvf::Color3fArray& candidates ); + static QColor textColor(); static cvf::Color3f textColor3f(); diff --git a/ApplicationLibCode/Commands/SummaryPlotCommands/RicCreateRegressionAnalysisCurveFeature.cpp b/ApplicationLibCode/Commands/SummaryPlotCommands/RicCreateRegressionAnalysisCurveFeature.cpp index 0abf8c56f2e..071bdafbd1c 100644 --- a/ApplicationLibCode/Commands/SummaryPlotCommands/RicCreateRegressionAnalysisCurveFeature.cpp +++ b/ApplicationLibCode/Commands/SummaryPlotCommands/RicCreateRegressionAnalysisCurveFeature.cpp @@ -18,6 +18,7 @@ #include "RicCreateRegressionAnalysisCurveFeature.h" +#include "RiaColorTables.h" #include "RiaColorTools.h" #include "RiaSummaryTools.h" @@ -84,12 +85,15 @@ void RicCreateRegressionAnalysisCurveFeature::setupActionLook( QAction* actionTo RimSummaryRegressionAnalysisCurve* RicCreateRegressionAnalysisCurveFeature::createRegressionAnalysisCurveAndAddToPlot( RimSummaryCurve* sourceCurve ) { - RimSummaryPlot* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject(); + auto* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject(); auto newCurve = new RimSummaryRegressionAnalysisCurve(); RiaSummaryTools::copyCurveDataSources( *newCurve, *sourceCurve ); - newCurve->setColor( sourceCurve->color() ); + auto candidates = RiaColorTables::summaryCurveDefaultPaletteColors(); + auto contrastColor = RiaColorTools::selectContrastColorFromCandiates( sourceCurve->color(), candidates.color3fArray() ); + + newCurve->setColor( contrastColor ); newCurve->setSymbol( RiuPlotCurveSymbol::PointSymbolEnum::SYMBOL_RECT ); newCurve->setSymbolSkipDistance( 50 ); @@ -100,7 +104,7 @@ RimSummaryRegressionAnalysisCurve* newCurve->loadDataAndUpdate( true ); newCurve->updateConnectedEditors(); - RimSummaryMultiPlot* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType(); + auto* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType(); if ( summaryMultiPlot ) { summaryMultiPlot->updatePlotTitles(); @@ -121,14 +125,18 @@ RimSummaryRegressionAnalysisCurve* RimSummaryRegressionAnalysisCurve* RicCreateRegressionAnalysisCurveFeature::createRegressionAnalysisCurveAndAddToPlot( RimEnsembleCurveSet* sourceCurveSet ) { - RimSummaryPlot* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject(); + auto* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject(); auto newCurve = new RimSummaryRegressionAnalysisCurve(); newCurve->setEnsembleCurveSet( sourceCurveSet ); auto color = RiaColorTools::fromQColorTo3f( sourceCurveSet->mainEnsembleColor() ); - newCurve->setColor( color ); + + auto candidates = RiaColorTables::summaryCurveDefaultPaletteColors(); + auto contrastColor = RiaColorTools::selectContrastColorFromCandiates( color, candidates.color3fArray() ); + + newCurve->setColor( contrastColor ); newCurve->setSymbol( RiuPlotCurveSymbol::PointSymbolEnum::SYMBOL_RECT ); newCurve->setSymbolSkipDistance( 50 ); @@ -141,7 +149,7 @@ RimSummaryRegressionAnalysisCurve* newCurve->loadDataAndUpdate( true ); newCurve->updateConnectedEditors(); - RimSummaryMultiPlot* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType(); + auto* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType(); if ( summaryMultiPlot ) { summaryMultiPlot->updatePlotTitles();