1 // Created on: 2015-02-03
2 // Copyright (c) 2015 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _AIS_ColorScale_HeaderFile
16 #define _AIS_ColorScale_HeaderFile
18 #include <AIS_InteractiveObject.hxx>
19 #include <Aspect_TypeOfColorScaleData.hxx>
20 #include <Aspect_TypeOfColorScalePosition.hxx>
21 #include <Aspect_SequenceOfColor.hxx>
22 #include <Standard.hxx>
23 #include <Standard_DefineHandle.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <TColStd_SequenceOfExtendedString.hxx>
27 //! Class for drawing a custom color scale.
29 //! The color scale consists of rectangular color bar (composed of fixed
30 //! number of color intervals), optional labels, and title.
31 //! The labels can be positioned either at the boundaries of the intervals,
32 //! or at the middle of each interval.
33 //! Colors and labels can be either defined automatically or set by the user.
34 //! Automatic labels are calculated from numerical limits of the scale,
35 //! its type (logarithmic or plain), and formatted by specified format string.
36 class AIS_ColorScale : public AIS_InteractiveObject
38 DEFINE_STANDARD_RTTIEXT(AIS_ColorScale, AIS_InteractiveObject)
41 //! Calculate color according passed value; returns true if value is in range or false, if isn't
42 Standard_EXPORT static Standard_Boolean FindColor (const Standard_Real theValue,
43 const Standard_Real theMin,
44 const Standard_Real theMax,
45 const Standard_Integer theColorsCount,
46 const Graphic3d_Vec3d& theColorHlsMin,
47 const Graphic3d_Vec3d& theColorHlsMax,
48 Quantity_Color& theColor);
50 //! Calculate color according passed value; returns true if value is in range or false, if isn't
51 static Standard_Boolean FindColor (const Standard_Real theValue,
52 const Standard_Real theMin,
53 const Standard_Real theMax,
54 const Standard_Integer theColorsCount,
55 Quantity_Color& theColor)
57 return FindColor (theValue, theMin, theMax, theColorsCount,
58 Graphic3d_Vec3d (230.0, 1.0, 1.0),
59 Graphic3d_Vec3d (0.0, 1.0, 1.0),
63 //! Shift hue into valid range.
64 //! Lightness and Saturation should be specified in valid range [0.0, 1.0],
65 //! however Hue might be given out of Quantity_Color range to specify desired range for interpolation.
66 static Standard_Real hueToValidRange (const Standard_Real theHue)
68 Standard_Real aHue = theHue;
69 while (aHue < 0.0) { aHue += 360.0; }
70 while (aHue > 360.0) { aHue -= 360.0; }
76 //! Default constructor.
77 Standard_EXPORT AIS_ColorScale();
79 //! Calculate color according passed value; returns true if value is in range or false, if isn't
80 Standard_EXPORT Standard_Boolean FindColor (const Standard_Real theValue, Quantity_Color& theColor) const;
82 //! Returns minimal value of color scale, 0.0 by default.
83 Standard_Real GetMin() const { return myMin; }
85 //! Sets the minimal value of color scale.
86 void SetMin (const Standard_Real theMin) { SetRange (theMin, GetMax()); }
88 //! Returns maximal value of color scale, 1.0 by default.
89 Standard_Real GetMax() const { return myMax; }
91 //! Sets the maximal value of color scale.
92 void SetMax (const Standard_Real theMax) { SetRange (GetMin(), theMax); }
94 //! Returns minimal and maximal values of color scale, 0.0 to 1.0 by default.
95 void GetRange (Standard_Real& theMin, Standard_Real& theMax) const
101 //! Sets the minimal and maximal value of color scale.
102 //! Note that values order will be ignored - the minimum and maximum values will be swapped if needed.
103 //! ::SetReversed() should be called to swap displaying order.
104 Standard_EXPORT void SetRange (const Standard_Real theMin, const Standard_Real theMax);
106 //! Returns the hue angle corresponding to minimum value, 230 by default (blue).
107 Standard_Real HueMin() const { return myColorHlsMin[0]; }
109 //! Returns the hue angle corresponding to maximum value, 0 by default (red).
110 Standard_Real HueMax() const { return myColorHlsMax[0]; }
112 //! Returns the hue angle range corresponding to minimum and maximum values, 230 to 0 by default (blue to red).
113 void HueRange (Standard_Real& theMinAngle,
114 Standard_Real& theMaxAngle) const
116 theMinAngle = myColorHlsMin[0];
117 theMaxAngle = myColorHlsMax[0];
120 //! Sets hue angle range corresponding to minimum and maximum values.
121 //! The valid angle range is [0, 360], see Quantity_Color and Quantity_TOC_HLS for more details.
122 void SetHueRange (const Standard_Real theMinAngle,
123 const Standard_Real theMaxAngle)
125 myColorHlsMin[0] = theMinAngle;
126 myColorHlsMax[0] = theMaxAngle;
129 //! Returns color range corresponding to minimum and maximum values, blue to red by default.
130 void ColorRange (Quantity_Color& theMinColor,
131 Quantity_Color& theMaxColor) const
133 theMinColor.SetValues (hueToValidRange (myColorHlsMin[0]), myColorHlsMin[1], myColorHlsMin[2], Quantity_TOC_HLS);
134 theMaxColor.SetValues (hueToValidRange (myColorHlsMin[0]), myColorHlsMin[1], myColorHlsMin[2], Quantity_TOC_HLS);
137 //! Sets color range corresponding to minimum and maximum values.
138 void SetColorRange (const Quantity_Color& theMinColor,
139 const Quantity_Color& theMaxColor)
141 theMinColor.Values (myColorHlsMin[0], myColorHlsMin[1], myColorHlsMin[2], Quantity_TOC_HLS);
142 theMaxColor.Values (myColorHlsMax[0], myColorHlsMax[1], myColorHlsMax[2], Quantity_TOC_HLS);
145 //! Returns the type of labels, Aspect_TOCSD_AUTO by default.
146 //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
147 //! Aspect_TOCSD_USER - user specified label is used
148 Aspect_TypeOfColorScaleData GetLabelType() const { return myLabelType; }
150 //! Sets the type of labels.
151 //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
152 //! Aspect_TOCSD_USER - user specified label is used
153 void SetLabelType (const Aspect_TypeOfColorScaleData theType) { myLabelType = theType; }
155 //! Returns the type of colors, Aspect_TOCSD_AUTO by default.
156 //! Aspect_TOCSD_AUTO - value between Red and Blue
157 //! Aspect_TOCSD_USER - user specified color from color map
158 Aspect_TypeOfColorScaleData GetColorType() const { return myColorType; }
160 //! Sets the type of colors.
161 //! Aspect_TOCSD_AUTO - value between Red and Blue
162 //! Aspect_TOCSD_USER - user specified color from color map
163 void SetColorType (const Aspect_TypeOfColorScaleData theType) { myColorType = theType; }
165 //! Returns the number of color scale intervals, 10 by default.
166 Standard_Integer GetNumberOfIntervals() const { return myNbIntervals; }
168 //! Sets the number of color scale intervals.
169 Standard_EXPORT void SetNumberOfIntervals (const Standard_Integer theNum);
171 //! Returns the color scale title string, empty string by default.
172 const TCollection_ExtendedString& GetTitle() const { return myTitle; }
174 //! Sets the color scale title string.
175 void SetTitle (const TCollection_ExtendedString& theTitle) { myTitle = theTitle; }
177 //! Returns the format for numbers, "%.4g" by default.
178 //! The same like format for function printf().
179 //! Used if GetLabelType() is TOCSD_AUTO;
180 const TCollection_AsciiString& GetFormat() const { return myFormat; }
182 //! Returns the format of text.
183 const TCollection_AsciiString& Format() const { return myFormat; }
185 //! Sets the color scale auto label format specification.
186 void SetFormat (const TCollection_AsciiString& theFormat) { myFormat = theFormat; }
188 //! Returns the user specified label with index theIndex.
189 //! Index is in range from 1 to GetNumberOfIntervals() or to
190 //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
191 //! Returns empty string if label not defined.
192 Standard_EXPORT TCollection_ExtendedString GetLabel (const Standard_Integer theIndex) const;
194 //! Returns the user specified color from color map with index (starts at 1).
195 //! Returns default color if index is out of range in color map.
196 Standard_EXPORT Quantity_Color GetIntervalColor (const Standard_Integer theIndex) const;
198 //! Sets the color of the specified interval.
199 //! Note that list is automatically resized to include specified index.
200 //! @param theColor color value to set
201 //! @param theIndex index in range [1, GetNumberOfIntervals()];
202 //! appended to the end of list if -1 is specified
203 Standard_EXPORT void SetIntervalColor (const Quantity_Color& theColor, const Standard_Integer theIndex);
205 //! Returns the user specified labels.
206 Standard_EXPORT void GetLabels (TColStd_SequenceOfExtendedString& theLabels) const;
208 //! Returns the user specified labels.
209 const TColStd_SequenceOfExtendedString& Labels() const { return myLabels; }
211 //! Sets the color scale labels.
212 //! The length of the sequence should be equal to GetNumberOfIntervals() or to GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
213 //! If length of the sequence does not much the number of intervals,
214 //! then these labels will be considered as "free" and will be located
215 //! at the virtual intervals corresponding to the number of labels
216 //! (with flag IsLabelAtBorder() having the same effect as in normal case).
217 Standard_EXPORT void SetLabels (const TColStd_SequenceOfExtendedString& theSeq);
219 //! Returns the user specified colors.
220 Standard_EXPORT void GetColors (Aspect_SequenceOfColor& theColors) const;
222 //! Returns the user specified colors.
223 const Aspect_SequenceOfColor& GetColors() const { return myColors; }
225 //! Sets the color scale colors.
226 //! The length of the sequence should be equal to GetNumberOfIntervals().
227 Standard_EXPORT void SetColors (const Aspect_SequenceOfColor& theSeq);
229 //! Returns the position of labels concerning color filled rectangles, Aspect_TOCSP_RIGHT by default.
230 Aspect_TypeOfColorScalePosition GetLabelPosition() const { return myLabelPos; }
232 //! Sets the color scale labels position relative to color bar.
233 void SetLabelPosition (const Aspect_TypeOfColorScalePosition thePos) { myLabelPos = thePos; }
235 //! Returns the position of color scale title, Aspect_TOCSP_LEFT by default.
236 Aspect_TypeOfColorScalePosition GetTitlePosition() const { return myTitlePos; }
238 //! Sets the color scale title position.
239 Standard_DEPRECATED("AIS_ColorScale::SetTitlePosition() has no effect!")
240 void SetTitlePosition (const Aspect_TypeOfColorScalePosition thePos) { myTitlePos = thePos; }
242 //! Returns TRUE if the labels and colors used in reversed order, FALSE by default.
243 //! - Normal, bottom-up order with Minimal value on the Bottom and Maximum value on Top.
244 //! - Reversed, top-down order with Maximum value on the Bottom and Minimum value on Top.
245 Standard_Boolean IsReversed() const { return myIsReversed; }
247 //! Sets true if the labels and colors used in reversed order.
248 void SetReversed (const Standard_Boolean theReverse) { myIsReversed = theReverse; }
250 //! Return TRUE if color transition between neighbor intervals
251 //! should be linearly interpolated, FALSE by default.
252 Standard_Boolean IsSmoothTransition() const { return myIsSmooth; }
254 //! Setup smooth color transition.
255 void SetSmoothTransition (const Standard_Boolean theIsSmooth) { myIsSmooth = theIsSmooth; }
257 //! Returns TRUE if the labels are placed at border of color intervals, TRUE by default.
258 //! The automatically generated label will show value exactly on the current position:
259 //! - value connecting two neighbor intervals (TRUE)
260 //! - value in the middle of interval (FALSE)
261 Standard_Boolean IsLabelAtBorder() const { return myIsLabelAtBorder; }
263 //! Sets true if the labels are placed at border of color intervals (TRUE by default).
264 //! If set to False, labels will be drawn at color intervals rather than at borders.
265 void SetLabelAtBorder (const Standard_Boolean theOn) { myIsLabelAtBorder = theOn; }
267 //! Returns TRUE if the color scale has logarithmic intervals, FALSE by default.
268 Standard_Boolean IsLogarithmic() const { return myIsLogarithmic; }
270 //! Sets true if the color scale has logarithmic intervals.
271 void SetLogarithmic (const Standard_Boolean isLogarithmic) { myIsLogarithmic = isLogarithmic; }
273 //! Sets the color scale label at index.
274 //! Note that list is automatically resized to include specified index.
275 //! @param theLabel new label text
276 //! @param theIndex index in range [1, GetNumberOfIntervals()] or [1, GetNumberOfIntervals() + 1] if IsLabelAtBorder() is true;
277 //! label is appended to the end of list if negative index is specified
278 Standard_EXPORT void SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer theIndex);
280 //! Returns the size of color bar, 0 and 0 by default
281 //! (e.g. should be set by user explicitly before displaying).
282 void GetSize (Standard_Integer& theBreadth, Standard_Integer& theHeight) const
284 theBreadth = myBreadth;
285 theHeight = myHeight;
288 //! Sets the size of color bar.
289 void SetSize (const Standard_Integer theBreadth, const Standard_Integer theHeight)
291 myBreadth = theBreadth;
292 myHeight = theHeight;
295 //! Returns the breadth of color bar, 0 by default
296 //! (e.g. should be set by user explicitly before displaying).
297 Standard_Integer GetBreadth() const { return myBreadth; }
299 //! Sets the width of color bar.
300 void SetBreadth (const Standard_Integer theBreadth) { myBreadth = theBreadth; }
302 //! Returns the height of color bar, 0 by default
303 //! (e.g. should be set by user explicitly before displaying).
304 Standard_Integer GetHeight() const { return myHeight; }
306 //! Sets the height of color bar.
307 void SetHeight (const Standard_Integer theHeight) { myHeight = theHeight; }
309 //! Returns the bottom-left position of color scale, 0x0 by default.
310 void GetPosition (Standard_Real& theX, Standard_Real& theY) const
316 //! Sets the position of color scale.
317 void SetPosition (const Standard_Integer theX, const Standard_Integer theY)
323 //! Returns the left position of color scale, 0 by default.
324 Standard_Integer GetXPosition() const { return myXPos; }
326 //! Sets the left position of color scale.
327 void SetXPosition (const Standard_Integer theX) { myXPos = theX; }
329 //! Returns the bottom position of color scale, 0 by default.
330 Standard_Integer GetYPosition() const { return myYPos; }
332 //! Sets the bottom position of color scale.
333 void SetYPosition (const Standard_Integer theY) { myYPos = theY; }
335 //! Returns the font height of text labels, 20 by default.
336 Standard_Integer GetTextHeight() const { return myTextHeight; }
338 //! Sets the height of text of color scale.
339 void SetTextHeight (const Standard_Integer theHeight) { myTextHeight = theHeight; }
343 //! Returns the width of text.
344 //! @param theText [in] the text of which to calculate width.
345 Standard_EXPORT Standard_Integer TextWidth (const TCollection_ExtendedString& theText) const;
347 //! Returns the height of text.
348 //! @param theText [in] the text of which to calculate height.
349 Standard_EXPORT Standard_Integer TextHeight (const TCollection_ExtendedString& theText) const;
351 Standard_EXPORT void TextSize (const TCollection_ExtendedString& theText,
352 const Standard_Integer theHeight,
353 Standard_Integer& theWidth,
354 Standard_Integer& theAscent,
355 Standard_Integer& theDescent) const;
359 //! Return true if specified display mode is supported.
360 virtual Standard_Boolean AcceptDisplayMode (const Standard_Integer theMode) const Standard_OVERRIDE { return theMode == 0; }
362 //! Compute presentation.
363 Standard_EXPORT virtual void Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
364 const Handle(Prs3d_Presentation)& thePresentation,
365 const Standard_Integer theMode) Standard_OVERRIDE;
367 //! Compute selection - not implemented for color scale.
368 virtual void ComputeSelection (const Handle(SelectMgr_Selection)& /*aSelection*/,
369 const Standard_Integer /*aMode*/) Standard_OVERRIDE {}
373 //! Returns the size of color scale.
374 //! @param theWidth [out] the width of color scale.
375 //! @param theHeight [out] the height of color scale.
376 void SizeHint (Standard_Integer& theWidth, Standard_Integer& theHeight) const;
378 //! Returns the upper value of given interval, or minimum for theIndex = 0.
379 Standard_Real GetIntervalValue (const Standard_Integer theIndex) const;
381 //! Returns the color for the given value in the given interval.
382 //! @param theValue [in] the current value of interval
383 //! @param theMin [in] the min value of interval
384 //! @param theMax [in] the max value of interval
385 Quantity_Color colorFromValue (const Standard_Real theValue,
386 const Standard_Real theMin,
387 const Standard_Real theMax) const;
389 //! Initialize text aspect for drawing the labels.
390 void updateTextAspect();
392 //! Simple alias for Prs3d_Text::Draw().
393 //! @param theGroup [in] presentation group
394 //! @param theText [in] text to draw
395 //! @param theX [in] X coordinate of text position
396 //! @param theY [in] Y coordinate of text position
397 //! @param theVertAlignment [in] text vertical alignment
398 void drawText (const Handle(Graphic3d_Group)& theGroup,
399 const TCollection_ExtendedString& theText,
400 const Standard_Integer theX, const Standard_Integer theY,
401 const Graphic3d_VerticalTextAlignment theVertAlignment);
403 //! Determine the maximum text label width in pixels.
404 Standard_Integer computeMaxLabelWidth (const TColStd_SequenceOfExtendedString& theLabels) const;
407 void drawLabels (const Handle(Prs3d_Presentation)& thePrs,
408 const TColStd_SequenceOfExtendedString& theLabels,
409 const Standard_Integer theBarBottom,
410 const Standard_Integer theBarHeight,
411 const Standard_Integer theMaxLabelWidth,
412 const Standard_Integer theColorBreadth);
414 //! Draw a color bar.
415 void drawColorBar (const Handle(Prs3d_Presentation)& thePrs,
416 const Standard_Integer theBarBottom,
417 const Standard_Integer theBarHeight,
418 const Standard_Integer theMaxLabelWidth,
419 const Standard_Integer theColorBreadth);
422 //! @param theX [in] the X coordinate of frame position.
423 //! @param theY [in] the Y coordinate of frame position.
424 //! @param theWidth [in] the width of frame.
425 //! @param theHeight [in] the height of frame.
426 //! @param theColor [in] the color of frame.
427 void drawFrame (const Handle(Prs3d_Presentation)& thePrs,
428 const Standard_Integer theX, const Standard_Integer theY,
429 const Standard_Integer theWidth, const Standard_Integer theHeight,
430 const Quantity_Color& theColor);
434 Standard_Real myMin; //!< values range - minimal value
435 Standard_Real myMax; //!< values range - maximal value
436 Graphic3d_Vec3d myColorHlsMin; //!< HLS color corresponding to minimum value
437 Graphic3d_Vec3d myColorHlsMax; //!< HLS color corresponding to maximum value
438 TCollection_ExtendedString myTitle; //!< optional title string
439 TCollection_AsciiString myFormat; //!< sprintf() format for generating label from value
440 Standard_Integer myNbIntervals; //!< number of intervals
441 Aspect_TypeOfColorScaleData myColorType; //!< color type
442 Aspect_TypeOfColorScaleData myLabelType; //!< label type
443 Standard_Boolean myIsLabelAtBorder; //!< at border
444 Standard_Boolean myIsReversed; //!< flag indicating reversed order
445 Standard_Boolean myIsLogarithmic; //!< flag indicating logarithmic scale
446 Standard_Boolean myIsSmooth; //!< flag indicating smooth transition between the colors
447 Aspect_SequenceOfColor myColors; //!< sequence of custom colors
448 TColStd_SequenceOfExtendedString myLabels; //!< sequence of custom text labels
449 Aspect_TypeOfColorScalePosition myLabelPos; //!< label position relative to the color scale
450 Aspect_TypeOfColorScalePosition myTitlePos; //!< title position
451 Standard_Integer myXPos; //!< left position
452 Standard_Integer myYPos; //!< bottom position
453 Standard_Integer myBreadth; //!< color scale breadth
454 Standard_Integer myHeight; //!< height of the color scale
455 Standard_Integer mySpacing; //!< extra spacing between element
456 Standard_Integer myTextHeight; //!< label font height