From d5514578e81dc5026bc835b65d93acd8882c4050 Mon Sep 17 00:00:00 2001 From: aba Date: Tue, 2 Aug 2016 14:48:45 +0300 Subject: [PATCH] 0027573: AIS_ColorScale::FindColor does not take into account custom colors Modified AIS_ColorScale::FindColor (Standard_Real, Quantity_Color&) to take into account custom colors --- src/AIS/AIS_ColorScale.cxx | 26 +++++++++ src/AIS/AIS_ColorScale.hxx | 5 +- src/ViewerTest/ViewerTest_ViewerCommands.cxx | 21 +++++++ tests/bugs/vis/bug27573 | 59 ++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/bugs/vis/bug27573 diff --git a/src/AIS/AIS_ColorScale.cxx b/src/AIS/AIS_ColorScale.cxx index 4648f0a574..daadd8ae67 100644 --- a/src/AIS/AIS_ColorScale.cxx +++ b/src/AIS/AIS_ColorScale.cxx @@ -501,6 +501,32 @@ Standard_Integer AIS_ColorScale::HueFromValue (const Standard_Integer theValue, Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue, Quantity_Color& theColor) const { + if (theValue < myMin || theValue > myMax || myMax < myMin) + { + theColor = Quantity_Color(); + return Standard_False; + } + + if (GetColorType() == Aspect_TOCSD_USER) + { + Standard_Integer anIndex = 0; + if (Abs (myMax - myMin) > Precision::Approximation()) + { + anIndex = (theValue - myMin < Precision::Confusion()) + ? 1 + : Standard_Integer (Ceiling (( theValue - myMin ) / ( (myMax - myMin) / myInterval))); + } + + if (anIndex <= 0 || anIndex > myColors.Length()) + { + theColor = Quantity_Color(); + return Standard_False; + } + + theColor = myColors.Value (anIndex); + return Standard_True; + } + return FindColor (theValue, myMin, myMax, myInterval, theColor); } diff --git a/src/AIS/AIS_ColorScale.hxx b/src/AIS/AIS_ColorScale.hxx index 7f352434de..ea2a146159 100644 --- a/src/AIS/AIS_ColorScale.hxx +++ b/src/AIS/AIS_ColorScale.hxx @@ -44,6 +44,7 @@ public: //! Calculate color according passed value; returns true if value is in range or false, if isn't Standard_EXPORT Standard_Boolean FindColor (const Standard_Real theValue, Quantity_Color& theColor) const; + //! Calculate color according passed value; returns true if value is in range or false, if isn't Standard_EXPORT static Standard_Boolean FindColor (const Standard_Real theValue, const Standard_Real theMin, const Standard_Real theMax, const Standard_Integer theColorsCount, Quantity_Color& theColor); //! Returns minimal value of color scale; @@ -280,13 +281,13 @@ private: Standard_Real myMax; TCollection_ExtendedString myTitle; TCollection_AsciiString myFormat; - Standard_Integer myInterval; + Standard_Integer myInterval; //! Number of intervals Aspect_TypeOfColorScaleData myColorType; Aspect_TypeOfColorScaleData myLabelType; Standard_Boolean myAtBorder; Standard_Boolean myReversed; Standard_Boolean myIsLogarithmic; - Aspect_SequenceOfColor myColors; + Aspect_SequenceOfColor myColors; //! Sequence of custom colors TColStd_SequenceOfExtendedString myLabels; Aspect_TypeOfColorScalePosition myLabelPos; Aspect_TypeOfColorScalePosition myTitlePos; diff --git a/src/ViewerTest/ViewerTest_ViewerCommands.cxx b/src/ViewerTest/ViewerTest_ViewerCommands.cxx index cb902fd9e3..52e2203334 100644 --- a/src/ViewerTest/ViewerTest_ViewerCommands.cxx +++ b/src/ViewerTest/ViewerTest_ViewerCommands.cxx @@ -3986,6 +3986,27 @@ static int VColorScale (Draw_Interpretor& theDI, aCS->SetColorType (Aspect_TOCSD_AUTO); aCS->SetLabelType (Aspect_TOCSD_AUTO); } + else if (aFlag == "-findcolor") + { + if (anArgIter + 1 >= theArgNb) + { + std::cout << "Error: wrong syntax at argument '" << anArg << "'!\n"; + return 1; + } + + TCollection_AsciiString anArg1 (theArgVec[++anArgIter]); + + if (!anArg1.IsRealValue()) + { + std::cout << "Error: the value should be real!\n"; + return 1; + } + + Quantity_Color aColor; + aCS->FindColor (anArg1.RealValue(), aColor); + theDI << Quantity_Color::StringName (aColor.Name()); + return 0; + } else { std::cout << "Error: wrong syntax at " << anArg << " - unknown argument!\n"; diff --git a/tests/bugs/vis/bug27573 b/tests/bugs/vis/bug27573 new file mode 100644 index 0000000000..4825781e6d --- /dev/null +++ b/tests/bugs/vis/bug27573 @@ -0,0 +1,59 @@ +puts "============" +puts "0027573" +puts "AIS_ColorScale::FindColor does not take into account custom colors." +puts "============" +puts "" + +vclose all +vinit +vclear +vaxo + +vcolorscale cs -demo +vcolorscale cs -range 0 20 5 + +# Set user-defined colors and labels for color scale +vcolorscale cs -colors white red green blue1 gray + +# Check the first interval border color +if {[vcolorscale cs -findcolor 0] != "WHITE"} { + puts "ERROR: Find color result for the first segment is wrong!" +} + +# Check first-second intervals border +if {[vcolorscale cs -findcolor 4] != "WHITE"} { + puts "ERROR: Find color result for the first segment border is wrong!" +} + +# Check the second interval color +if {[vcolorscale cs -findcolor 5] != "RED"} { + puts "ERROR: Find color result for the second segment is wrong!" +} + +# Check the second interval color +if {[vcolorscale cs -findcolor 9] != "GREEN"} { + puts "ERROR: Find color result for the third segment is wrong!" +} + +# Check the last interval border color +if {[vcolorscale cs -findcolor 20] != "GRAY"} { + puts "ERROR: Find color result for the last segment is wrong!" +} + +# Check negative value limits +vcolorscale cs -range -5 5 5 + +if {[vcolorscale cs -findcolor -5] != "WHITE"} { + puts "ERROR: Find color result for the first segment is wrong!" +} + +if {[vcolorscale cs -findcolor 0] != "GREEN"} { + puts "ERROR: Find color result for the middle segment is wrong!" +} + +if {[vcolorscale cs -findcolor 5] != "GRAY"} { + puts "ERROR: Find color result for the last segment is wrong!" +} + +# Dump result +set only_screen 1 \ No newline at end of file -- 2.20.1