myTitlePos (Aspect_TOCSP_CENTER),
myXPos (0),
myYPos (0),
-myWidth (0),
+myBreadth (0),
myHeight (0),
-myTextHeight(20),
-myBgColor (Quantity_NOC_BLACK)
+myTextHeight(20)
{
}
{
if (GetLabelType() == Aspect_TOCSD_USER)
{
- if (theIndex < 0
- || theIndex >= myLabels.Length())
+ if (theIndex <= 0 || theIndex > myLabels.Length())
{
return "";
}
-
- return myLabels.Value (theIndex + 1);
+ return myLabels.Value (theIndex);
}
- Standard_Real aVal = IsLogarithmic() ? GetLogNumber(theIndex) : GetNumber (theIndex);
+
+ // value to be shown depends on label position
+ Standard_Real aVal = IsLabelAtBorder() ? GetIntervalValue (theIndex - 1) :
+ 0.5 * (GetIntervalValue (theIndex - 1) + GetIntervalValue (theIndex));
+
const TCollection_AsciiString aFormat = Format();
Standard_Character aBuf[1024];
sprintf (aBuf, aFormat.ToCString(), aVal);
}
//=======================================================================
-//function : GetColor
+//function : GetIntervalColor
//purpose :
//=======================================================================
-Quantity_Color AIS_ColorScale::GetColor (const Standard_Integer theIndex) const
+Quantity_Color AIS_ColorScale::GetIntervalColor (const Standard_Integer theIndex) const
{
if (GetColorType() == Aspect_TOCSD_USER)
{
- if (theIndex < 0
- || theIndex >= myColors.Length())
+ if (theIndex <= 0 || theIndex > myColors.Length())
{
return Quantity_Color();
}
-
- return myColors.Value (theIndex + 1);
+ return myColors.Value (theIndex);
}
- return Quantity_Color (HueFromValue (theIndex, 0, GetNumberOfIntervals() - 1), 1.0, 1.0, Quantity_TOC_HLS);
+
+ return Quantity_Color (HueFromValue (theIndex - 1, 0, GetNumberOfIntervals() - 1), 1.0, 1.0, Quantity_TOC_HLS);
}
//=======================================================================
//=======================================================================
void AIS_ColorScale::SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer theIndex)
{
- Standard_Integer i = theIndex < 0 ? myLabels.Length() + 1 : theIndex + 1;
- if (i <= myLabels.Length())
- {
- myLabels.SetValue (i, theLabel);
- }
- else
- {
- while (i > myLabels.Length())
- myLabels.Append (TCollection_ExtendedString());
- myLabels.SetValue (i, theLabel);
- }
+ Standard_Integer i = (theIndex <= 0 ? myLabels.Length() + 1 : theIndex);
+ while (i > myLabels.Length())
+ myLabels.Append (TCollection_ExtendedString());
+ myLabels.SetValue (i, theLabel);
}
//=======================================================================
-//function : SetColor
+//function : SetIntervalColor
//purpose :
//=======================================================================
-void AIS_ColorScale::SetColor (const Quantity_Color& theColor, const Standard_Integer theIndex)
+void AIS_ColorScale::SetIntervalColor (const Quantity_Color& theColor, const Standard_Integer theIndex)
{
- Standard_Integer i = theIndex < 0 ? myColors.Length() + 1 : theIndex + 1;
- if (i <= myColors.Length())
- {
- myColors.SetValue (i, theColor);
- }
- else
- {
- while (i > myColors.Length())
- myColors.Append (Quantity_Color());
- myColors.SetValue (i, theColor);
- }
+ Standard_Integer i = (theIndex <= 0 ? myColors.Length() + 1 : theIndex);
+ while (i > myColors.Length())
+ myColors.Append (Quantity_Color());
+ myColors.SetValue (i, theColor);
}
//=======================================================================
//function : GetSize
//purpose :
//=======================================================================
-void AIS_ColorScale::GetSize (Standard_Integer& theWidth, Standard_Integer& theHeight) const
+void AIS_ColorScale::GetSize (Standard_Integer& theBreadth, Standard_Integer& theHeight) const
{
- theWidth = myWidth;
+ theBreadth = myBreadth;
theHeight = myHeight;
}
//function : SetSize
//purpose :
//=======================================================================
-void AIS_ColorScale::SetSize (const Standard_Integer theWidth, const Standard_Integer theHeight)
+void AIS_ColorScale::SetSize (const Standard_Integer theBreadth, const Standard_Integer theHeight)
{
- if (myWidth == theWidth && myHeight == theHeight)
+ if (myBreadth == theBreadth && myHeight == theHeight)
return;
- myWidth = theWidth;
+ myBreadth = theBreadth;
myHeight = theHeight;
}
//=======================================================================
-//function : SetWidth
+//function : SetBreadth
//purpose :
//=======================================================================
-void AIS_ColorScale::SetWidth (const Standard_Integer theWidth)
+void AIS_ColorScale::SetBreadth (const Standard_Integer theWidth)
{
SetSize (theWidth, GetHeight());
}
//=======================================================================
void AIS_ColorScale::SetHeight (const Standard_Integer theHeight)
{
- SetSize (GetWidth(), theHeight);
+ SetSize (GetBreadth(), theHeight);
}
//=======================================================================
Standard_Integer aColorWidth = 20;
if (GetLabelPosition() != Aspect_TOCSP_NONE)
- for (Standard_Integer idx = 0; idx < aNum; idx++)
- aTextWidth = Max (aTextWidth, TextWidth (GetLabel (idx + 1)));
+ {
+ for (Standard_Integer idx = (IsLabelAtBorder() ? 0 : 1); idx <= aNum; idx++)
+ aTextWidth = Max (aTextWidth, TextWidth (GetLabel (idx)));
+ }
Standard_Integer aScaleWidth = 0;
Standard_Integer aScaleHeight = 0;
- Standard_Integer aTitleWidth = 0;
- Standard_Integer aTitleHeight = 0;
-
- if (IsLabelAtBorder())
- {
- aNum++;
- if (GetTitle().Length())
- aTitleHeight += 10;
- }
-
aScaleWidth = aColorWidth + aTextWidth + ( aTextWidth ? 3 : 2 ) * aSpacer;
- aScaleHeight = (Standard_Integer)( 1.5 * ( aNum + 1 ) * aTextHeight );
+ aScaleHeight = (Standard_Integer)( 1.5 * ( aNum + (IsLabelAtBorder() ? 2 : 1) ) * aTextHeight );
+ Standard_Integer aTitleWidth = 0;
+ Standard_Integer aTitleHeight = 0;
if (GetTitle().Length())
{
aTitleHeight = TextHeight (GetTitle()) + aSpacer;
}
//=======================================================================
-//function : GetNumber
+//function : GetIntervalValue
//purpose :
//=======================================================================
-Standard_Real AIS_ColorScale::GetNumber (const Standard_Integer theIndex) const
+Standard_Real AIS_ColorScale::GetIntervalValue (const Standard_Integer theIndex) const
{
- Standard_Real aNum = 0;
- if (GetNumberOfIntervals() > 0)
- aNum = GetMin() + theIndex * ( Abs (GetMax() - GetMin()) / GetNumberOfIntervals() );
- return aNum;
-}
+ if (GetNumberOfIntervals() <= 0)
+ return 0.;
-//=======================================================================
-//function : GetLogNumber
-//purpose :
-//=======================================================================
-Standard_Real AIS_ColorScale::GetLogNumber (const Standard_Integer theIndex) const
-{
- if (GetNumberOfIntervals() > 0)
+ if (IsLogarithmic())
{
Standard_Real aMin = myMin > 0 ? myMin : 1.0;
Standard_Real aDivisor = std::pow (myMax/aMin, 1.0/myInterval);
return aMin*std::pow (aDivisor,theIndex);
}
- return 0;
+
+ Standard_Real aNum = 0;
+ if (GetNumberOfIntervals() > 0)
+ aNum = GetMin() + theIndex * ( Abs (GetMax() - GetMin()) / GetNumberOfIntervals() );
+ return aNum;
}
//=======================================================================
{
Handle(V3d_Viewer) aViewer= GetContext()->CurrentViewer();
aViewer->InitActiveViews();
- Quantity_Color aBgColor = myBgColor;
Standard_Integer aNum = GetNumberOfIntervals();
Aspect_TypeOfColorScalePosition aLabPos = GetLabelPosition();
Standard_Boolean toDrawLabel = GetLabelPosition() != Aspect_TOCSP_NONE;
TCollection_ExtendedString aTitle = GetTitle();
Standard_Integer aTitleHeight = aSpacer;
- Standard_Integer aGray = (Standard_Integer)(255 * ( aBgColor.Red() * 11 + aBgColor.Green() * 16 + aBgColor.Blue() * 5 ) / 32);
- Quantity_Color aFgColor (aGray < 128 ? Quantity_NOC_WHITE : Quantity_NOC_BLACK);
+ Quantity_Color aFgColor (hasOwnColor ? myOwnColor : Quantity_NOC_WHITE);
// Draw title
if (aTitle.Length())
Standard_Boolean toReverse = IsReversed();
Aspect_SequenceOfColor aColors;
- TColStd_SequenceOfExtendedString aLabels;
- for (Standard_Integer i = 0; i < aNum; i++)
+ for (Standard_Integer i = 1; i <= aNum; i++)
{
if (toReverse)
{
- aColors.Prepend (GetColor (i));
- aLabels.Prepend (GetLabel (i));
+ aColors.Prepend (GetIntervalColor (i));
}
else
{
- aColors.Append (GetColor (i));
- aLabels.Append (GetLabel (i));
+ aColors.Append (GetIntervalColor (i));
}
}
- if (IsLabelAtBorder())
+ TColStd_SequenceOfExtendedString aLabels;
+ Standard_Integer aLabCount = IsLabelAtBorder() ? aNum + 1 : aNum;
+ for (Standard_Integer i = 1; i <= aLabCount; i++)
{
if (toReverse)
- aLabels.Prepend (GetLabel (aNum));
+ {
+ aLabels.Prepend (GetLabel (i));
+ }
else
- aLabels.Append (GetLabel (aNum));
+ {
+ aLabels.Append (GetLabel (i));
+ }
}
if (toDrawLabel)
for (Standard_Integer i = 1; i <= aLabels.Length(); i++)
aTextWidth = Max (aTextWidth, TextWidth (aLabels.Value (i)));
- Standard_Integer aLabCount = aLabels.Length();
-
Standard_Integer aSpc = ( myHeight - ( ( Min (aLabCount, 2) + Abs (aLabCount - aNum - 1) ) * aTextHeight ) - aTitleHeight );
Standard_Real aVal = aSpc != 0 ? 1.0 * ( aLabCount - Min (aLabCount, 0) ) * aTextHeight / aSpc : 0;
Standard_Real anIPart;
Standard_Real aStep = 1.0 * ( myHeight - (aLabCount - aNum + Abs (aLabCount - aNum - 1)) * aTextHeight - aTitleHeight ) / aNum;
Standard_Integer anAscent = 0;
- Standard_Integer aColorWidth = Max (5, Min (20, myWidth - aTextWidth - 3 * aSpacer));
+ Standard_Integer aColorBreadth = Max (5, Min (20, myBreadth - aTextWidth - 3 * aSpacer));
if (aLabPos == Aspect_TOCSP_CENTER || !toDrawLabel)
- aColorWidth += aTextWidth;
+ aColorBreadth += aTextWidth;
// Draw colors
Standard_Integer aX = (Standard_Integer)myXPos + aSpacer;
Standard_Integer aY = (Standard_Integer)( myYPos + ( i - 1 )* aStep + anOffset );
Standard_Integer aColorHeight = (Standard_Integer)( myYPos + ( i ) * aStep + anOffset ) - aY;
aPrim->AddVertex (gp_Pnt (aX, aY, 0.0), aColors.Value( i ));
- aPrim->AddVertex (gp_Pnt (aX+aColorWidth, aY, 0.0), aColors.Value( i ));
+ aPrim->AddVertex (gp_Pnt (aX+aColorBreadth, aY, 0.0), aColors.Value( i ));
aPrim->AddVertex (gp_Pnt (aX, aY+aColorHeight, 0.0), aColors.Value( i ));
- aPrim->AddVertex (gp_Pnt (aX+aColorWidth, aY+aColorHeight, 0.0), aColors.Value( i ));
+ aPrim->AddVertex (gp_Pnt (aX+aColorBreadth, aY+aColorHeight, 0.0), aColors.Value( i ));
aPrim->AddEdge(aVertIndex);
aPrim->AddEdge(aVertIndex+1);
aPrim->AddEdge(aVertIndex+2);
aGroup->AddPrimitiveArray (aPrim);
if (aStep > 0)
- DrawFrame (thePresentation, aX - 1, (Standard_Integer)(myYPos + anOffset - 1), aColorWidth + 2, (Standard_Integer)(aColors.Length() * aStep + 2), aFgColor);
+ DrawFrame (thePresentation, aX - 1, (Standard_Integer)(myYPos + anOffset - 1), aColorBreadth + 2, (Standard_Integer)(aColors.Length() * aStep + 2), aFgColor);
// Draw Labels
anOffset = 1.0 * Abs (aLabCount - aNum - 1) * ( aStep - aTextHeight ) / 2 + 1.0 * Abs (aLabCount - aNum - 1) * aTextHeight / 2;
case Aspect_TOCSP_LEFT:
break;
case Aspect_TOCSP_CENTER:
- aX += ( aColorWidth - aTextWidth ) / 2;
+ aX += ( aColorBreadth - aTextWidth ) / 2;
break;
case Aspect_TOCSP_RIGHT:
- aX += aColorWidth + aSpacer;
+ aX += aColorBreadth + aSpacer;
break;
}
while (i2 - i1 >= aFilter || ( i2 == 0 && i1 == 0 ))
aPrim->AddVertex (theX+theWidth,theY+theHeight,0.0);
aPrim->AddVertex (theX,theY+theHeight,0.0);
aPrim->AddVertex (theX,theY,0.0);
- Handle(Prs3d_LineAspect) anAspect = new Prs3d_LineAspect (theColor, Aspect_TOL_SOLID, 1.0);
+ Handle(Prs3d_LineAspect) anAspect =
+ new Prs3d_LineAspect (theColor, Aspect_TOL_SOLID, 1.0);
anAspect->SetColor (theColor);
aGroup->SetPrimitivesAspect (anAspect->Aspect());
aGroup->AddPrimitiveArray (aPrim);
#include <TCollection_ExtendedString.hxx>
#include <TColStd_SequenceOfExtendedString.hxx>
+//! Class for drawing a custom color scale.
+//!
+//! The color scale consists of rectangular color bar (composed of fixed
+//! number of color intervals), optional labels, and title.
+//! The labels can be positioned either at the boundaries of the intervals,
+//! or at the middle of each interval.
+//! Colors and labels can be either defined automatically or set by the user.
+//! Automatic labels are calculated from numerical limits of the scale,
+//! its type (logarithmic or plain), and formatted by specified format string.
-//! Class for drawing a custom color scale
class AIS_ColorScale : public AIS_InteractiveObject {
public:
//! Used if GetLabelType() is TOCSD_AUTO;
Standard_EXPORT TCollection_AsciiString GetFormat() const { return myFormat; }
- //! Returns the user specified label with index <anIndex>.
+ //! Returns the user specified label with index theIndex.
+ //! Index is in range from 1 to GetNumberOfIntervals() or to
+ //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
//! Returns empty string if label not defined.
Standard_EXPORT TCollection_ExtendedString GetLabel (const Standard_Integer theIndex) const;
- //! Returns the user specified color from color map with index <anIndex>.
- //! Returns default color if index out of range in color map.
- Standard_EXPORT Quantity_Color GetColor (const Standard_Integer theIndex) const;
+ //! Returns the user specified color from color map with index <anIndex> (starts at 1).
+ //! Returns default color if index is out of range in color map.
+ Standard_EXPORT Quantity_Color GetIntervalColor (const Standard_Integer theIndex) const;
//! Returns the user specified labels.
Standard_EXPORT void GetLabels (TColStd_SequenceOfExtendedString& theLabels) const;
//! Returns true if the labels and colors used in reversed order.
Standard_EXPORT Standard_Boolean IsReversed() const { return myReversed; }
- //! Returns true if the labels placed at border of color filled rectangles.
+ //! Returns true if the labels are placed at border of color intervals.
Standard_EXPORT Standard_Boolean IsLabelAtBorder() const { return myAtBorder; }
//! Returns true if the color scale has logarithmic intervals
//! Sets the color scale auto label format specification.
Standard_EXPORT void SetFormat (const TCollection_AsciiString& theFormat);
- //! Sets the color scale label at index. Index started from 1.
- Standard_EXPORT void SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer anIndex = -1);
+ //! Sets the color scale label at index.
+ //! Index is in range from 1 to GetNumberOfIntervals() or to
+ //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
+ Standard_EXPORT void SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer anIndex);
- //! Sets the color scale color at index. Index started from 1.
- Standard_EXPORT void SetColor (const Quantity_Color& theColor, const Standard_Integer theIndex = -1);
+ //! Sets the color of the specified interval.
+ //! Index is in range from 1 to GetNumberOfIntervals().
+ Standard_EXPORT void SetIntervalColor (const Quantity_Color& theColor, const Standard_Integer theIndex);
//! Sets the color scale labels.
+ //! The length of the sequence should be equal to GetNumberOfIntervals() or to
+ //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
Standard_EXPORT void SetLabels (const TColStd_SequenceOfExtendedString& theSeq);
//! Sets the color scale colors.
+ //! The length of the sequence should be equal to GetNumberOfIntervals().
Standard_EXPORT void SetColors (const Aspect_SequenceOfColor& theSeq);
- //! Sets the color scale labels position concerning color filled rectangles.
+ //! Sets the color scale labels position relative to color bar.
Standard_EXPORT void SetLabelPosition (const Aspect_TypeOfColorScalePosition thePos);
//! Sets the color scale title position.
//! Sets true if the labels and colors used in reversed order.
Standard_EXPORT void SetReversed (const Standard_Boolean theReverse);
- //! Sets true if the labels placed at border of color filled rectangles.
+ //! Sets true if the labels are placed at border of color intervals (true by default).
+ //! If set to False, labels will be drawn at color intervals rather than at borders.
Standard_EXPORT void SetLabelAtBorder (const Standard_Boolean theOn);
//! Sets true if the color scale has logarithmic intervals.
void SetLogarithmic (const Standard_Boolean isLogarithmic) { myIsLogarithmic = isLogarithmic; };
- //! Returns the size of color scale.
- Standard_EXPORT void GetSize (Standard_Integer& theWidth, Standard_Integer& theHeight) const;
+ //! Returns the size of color bar.
+ Standard_EXPORT void GetSize (Standard_Integer& theBreadth, Standard_Integer& theHeight) const;
- //! Returns the width of color scale.
- Standard_EXPORT Standard_Integer GetWidth() const { return myWidth; }
+ //! Returns the breadth of color bar.
+ Standard_EXPORT Standard_Integer GetBreadth() const { return myBreadth; }
- //! Returns the height of color scale.
+ //! Returns the height of color bar.
Standard_EXPORT Standard_Integer GetHeight() const { return myHeight; }
- //! Returns the background color of color scale.
- const Quantity_Color& GetBgColor() const { return myBgColor; }
-
- //! Sets the size of color scale.
+ //! Sets the size of color bar.
Standard_EXPORT void SetSize (const Standard_Integer theWidth, const Standard_Integer theHeight);
- //! Sets the width of color scale.
- Standard_EXPORT void SetWidth (const Standard_Integer theWidth);
+ //! Sets the width of color bar.
+ Standard_EXPORT void SetBreadth (const Standard_Integer theBreadth);
- //! Sets the height of color scale.
+ //! Sets the height of color bar.
Standard_EXPORT void SetHeight (const Standard_Integer theHeight);
- //! Sets the background color of color scale.
- void SetBGColor (const Quantity_Color& theBgColor) { myBgColor = theBgColor; };
-
//! Returns the position of color scale.
Standard_EXPORT void GetPosition (Standard_Real& theX, Standard_Real& theY) const;
//! Returns the format of text.
TCollection_AsciiString Format() const;
- //! Returns the value of given interval.
- Standard_Real GetNumber (const Standard_Integer theIndex) const;
-
- //! Returns the value of given logarithmic interval.
- Standard_Real GetLogNumber (const Standard_Integer theIndex) const;
+ //! Returns the upper value of given interval, or minimum for theIndex = 0.
+ Standard_Real GetIntervalValue (const Standard_Integer theIndex) const;
//! Returns the color's hue for the given value in the given interval.
//! @param theValue [in] the current value of interval.
Aspect_TypeOfColorScalePosition myTitlePos;
Standard_Integer myXPos;
Standard_Integer myYPos;
- Standard_Integer myWidth;
+ Standard_Integer myBreadth;
Standard_Integer myHeight;
Standard_Integer myTextHeight;
Quantity_Color myBgColor;
Standard_Real aMinRange = aCS->GetMin();
Standard_Real aMaxRange = aCS->GetMax();
- Standard_Integer aWidth = aCS->GetWidth();
+ Standard_Integer aBreadth = aCS->GetBreadth();
Standard_Integer aHeight = aCS->GetHeight();
Standard_Integer aNbIntervals = aCS->GetNumberOfIntervals();
Standard_Integer aTextHeight = aCS->GetTextHeight();
return 1;
}
- aWidth = aW.IntegerValue();
+ aBreadth = aW.IntegerValue();
}
else if (aFlag == "-height"
|| aFlag == "-h")
}
Standard_Integer anIndex = anInd.IntegerValue();
- if (anIndex < 0
- || anIndex > aNbIntervals - 1)
+ if (anIndex <= 0 || anIndex > aNbIntervals)
{
- std::cout << "Error: Index value should be within range 0..." << (aNbIntervals - 1) <<"!\n";
+ std::cout << "Error: Index value should be within range 1.." << aNbIntervals <<"!\n";
return 1;
}
if (Quantity_Color::ColorFromName (theArgVec[anArgIter + 2], aColorName))
{
- aCS->SetColor (Quantity_Color (aColorName), anIndex);
+ aCS->SetIntervalColor (Quantity_Color (aColorName), anIndex);
aCS->SetColorType (Aspect_TOCSD_USER);
anArgIter += 2;
continue;
{
return 1;
}
- aCS->SetColor (Quantity_Color (aRedValue, aGreenValue, aBlueValue, Quantity_TOC_RGB), anIndex);
+ aCS->SetIntervalColor (Quantity_Color (aRedValue, aGreenValue, aBlueValue, Quantity_TOC_RGB), anIndex);
aCS->SetColorType (Aspect_TOCSD_USER);
anArgIter += 4;
}
}
Standard_Integer anIndex = Draw::Atoi (theArgVec[anArgIter + 1]);
- if (anIndex < 0
- || anIndex > aNbIntervals)
+ if (anIndex <= 0 || anIndex > aNbIntervals+1)
{
- std::cout << "Error: Index value should be within range 0..." << aNbIntervals <<"!\n";
+ std::cout << "Error: Index value should be within range 1.." << aNbIntervals+1 <<"!\n";
return 1;
}
aMinRange = 0.0;
aMaxRange = 100;
aNbIntervals = 10;
- aWidth = 0;
+ aBreadth = 0;
aHeight = 0;
aLabPosition = Aspect_TOCSP_RIGHT;
aCS->SetColorType (Aspect_TOCSD_AUTO);
return 1;
}
}
- if (!aWidth || !aHeight)
+ if (!aBreadth || !aHeight)
{
Standard_Integer aWinWidth, aWinHeight;
aView->Window()->Size (aWinWidth, aWinHeight);
- if (!aWidth)
+ if (!aBreadth)
{
- aWidth = aWinWidth;
+ aBreadth = aWinWidth;
}
if (!aHeight)
{
aHeight = aWinHeight;
}
}
- aCS->SetSize (aWidth, aHeight);
+ aCS->SetSize (aBreadth, aHeight);
aCS->SetPosition (aPosX, aPosY);
aCS->SetTextHeight (aTextHeight);
aCS->SetRange (aMinRange, aMaxRange);
aCS->SetNumberOfIntervals (aNbIntervals);
aCS->SetLabelPosition (aLabPosition);
- aCS->SetBGColor (aView->BackgroundColor());
+// aCS->SetColor (aView->BackgroundColor().Invert());
aCS->SetToUpdate();
aContext->Display (aCS);