Skip to content

Commit

Permalink
Regression Curves: Use a contrast color for the regression curves
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magnesj committed Oct 17, 2023
1 parent 4c0e1e6 commit 7994924
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
24 changes: 24 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaColorTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaColorTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RicCreateRegressionAnalysisCurveFeature.h"

#include "RiaColorTables.h"
#include "RiaColorTools.h"
#include "RiaSummaryTools.h"

Expand Down Expand Up @@ -84,12 +85,15 @@ void RicCreateRegressionAnalysisCurveFeature::setupActionLook( QAction* actionTo
RimSummaryRegressionAnalysisCurve*
RicCreateRegressionAnalysisCurveFeature::createRegressionAnalysisCurveAndAddToPlot( RimSummaryCurve* sourceCurve )
{
RimSummaryPlot* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();
auto* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();

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 );

Expand All @@ -100,7 +104,7 @@ RimSummaryRegressionAnalysisCurve*
newCurve->loadDataAndUpdate( true );
newCurve->updateConnectedEditors();

RimSummaryMultiPlot* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType<RimSummaryMultiPlot>();
auto* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType<RimSummaryMultiPlot>();
if ( summaryMultiPlot )
{
summaryMultiPlot->updatePlotTitles();
Expand All @@ -121,14 +125,18 @@ RimSummaryRegressionAnalysisCurve*
RimSummaryRegressionAnalysisCurve*
RicCreateRegressionAnalysisCurveFeature::createRegressionAnalysisCurveAndAddToPlot( RimEnsembleCurveSet* sourceCurveSet )
{
RimSummaryPlot* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();
auto* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();

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 );

Expand All @@ -141,7 +149,7 @@ RimSummaryRegressionAnalysisCurve*
newCurve->loadDataAndUpdate( true );
newCurve->updateConnectedEditors();

RimSummaryMultiPlot* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType<RimSummaryMultiPlot>();
auto* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType<RimSummaryMultiPlot>();
if ( summaryMultiPlot )
{
summaryMultiPlot->updatePlotTitles();
Expand Down

0 comments on commit 7994924

Please sign in to comment.