Workspace 6.21.5
Public Types | Public Member Functions | List of all members
ColorSpectrum Class Reference

Describes a color spectrum that maps a floating point number in the range [0,1] to a specific color.

#include <DataAnalysis/Color/colorspectrum.h>

Inheritance diagram for ColorSpectrum:
[legend]

Public Types

enum  PointInterpolationMode { NearestNeighbour , Linear }
 

Public Member Functions

 ColorSpectrum ()
 
 ColorSpectrum (const ColorSpectrum &spec)
 
 ~ColorSpectrum () override
 
void addEquallySpacedColors (const QVector< QColor > &colors)
 
void addPoint (double value, const QColor &color)
 
bool canSerialize () const override
 
void clearPoints ()
 
QBrush getAsBrush (int pixels, Qt::Orientation orientation) const
 
QImage getAsImage (int pixels, Qt::Orientation orientation) const
 
QColor getColor (double value) const
 
QColor getColorAtPoint (unsigned index) const
 
unsigned getNumPoints () const
 
PointInterpolationMode getPointInterpolationMode () const
 
double getValueAtPoint (unsigned index) const
 
void invert ()
 
bool load (const DataExecution::SerializedItem &item) override
 
bool operator!= (const ColorSpectrum &s) const
 
ColorSpectrumoperator= (const ColorSpectrum &spec)
 
bool operator== (const ColorSpectrum &s) const
 
void removePoint (unsigned index)
 
bool save (DataExecution::SerializedItem &item) const override
 
void setPointInterpolationMode (PointInterpolationMode mode)
 
- Public Member Functions inherited from Serialize
virtual ~Serialize ()=default
 
virtual bool canSerialize () const =0
 
virtual bool load (const SerializedItem &item)=0
 
virtual bool save (SerializedItem &item) const =0
 

Member Enumeration Documentation

◆ PointInterpolationMode

Enumerator
NearestNeighbour 
Linear 

Constructor & Destructor Documentation

◆ ColorSpectrum() [1/2]

The default color spectrum is blue through to red, roughly corresponding to a typical color bar used in plot legends.

◆ ColorSpectrum() [2/2]

ColorSpectrum ( const ColorSpectrum spec)

◆ ~ColorSpectrum()

~ColorSpectrum ( )
override

Member Function Documentation

◆ addEquallySpacedColors()

void addEquallySpacedColors ( const QVector< QColor > &  colors)
Parameters
colorsThe array of colors to use

This replaces any existing points

◆ addPoint()

void addPoint ( double  value,
const QColor &  color 
)
Parameters
valueThe interpolation value for the point being added. This must be in the range [0,1]. If it is not, it will be forced to this range.
colorThe color to assign to the specified value.

If value already exists in the set of points for this spectrum, the existing point will have its color updated. Otherwise, a new point will be inserted at the appropriate sort order for value.

◆ canSerialize()

bool canSerialize ( ) const
overridevirtual
Returns
True if this object can be serialized.

For objects that can be serialized, the save and load functions will be used to do the serialization. If this function returns false, both load() and save() must also return false.

Implements Serialize.

◆ clearPoints()

void clearPoints ( )

Clears all points from the spectrum. A spectrum with no points is invalid and will always return an invalid color from getColor().

◆ getAsBrush()

QBrush getAsBrush ( int  pixels,
Qt::Orientation  orientation 
) const
Parameters
pixelsSpecifies how many pixels the spectrum should span.
orientationThe desired orientation of the spectrum. The direction specified here will be the direction in which the color varies. For example, if orientation is Qt::Horizontal, the brush will vary the color left to right and the color at any given horizontal location will be constant vertically.
// Assume spectrum is a pre-existing ColorSpectrum object and
// painter is a pre-existing QPainter object. We will draw the
// spectrum as 15 pixels wide and 100 pixels high.
QBrush spectrumBrush(spectrum.getAsBrush(length, Qt::Vertical));
QTransform transform;
transform.scale(1, -1);
spectrumBrush.setTransform(transform);
painter.fillRect(0, 0, 15, 100, spectrumBrush);

If orientation is Qt::Horizontal, then pixels is the width of the spectrum. If orientation is Qt::Vertical, then pixels is the height of the spectrum.

Returns
A QBrush representing this color spectrum.

◆ getAsImage()

QImage getAsImage ( int  pixels,
Qt::Orientation  orientation 
) const
Parameters
pixelsSpecifies how many pixels the spectrum should span.
orientationThe desired orientation of the spectrum. The direction specified here will be the direction in which the color varies. For example, if orientation is Qt::Horizontal, the brush will vary the color left to right and the color at any given horizontal location will be constant vertically.

If orientation is Qt::Horizontal, then pixels is the width of the spectrum. If orientation is Qt::Vertical, then pixels is the height of the spectrum.

Returns
A 1 dimensional image representing this color spectrum.

◆ getColor()

QColor getColor ( double  value) const
Parameters
valueThe value for which a color should be determined. This value should be in the range [0,1] and if it falls outside this range, it will be forced to the closest end point.
Returns
The interpolated color for the specified value. If value lies before the first point or after the last point, it will have the same color as the first or last point respectively. In other words, color is interpolated between points, but never extrapolated beyond points in either direction.

◆ getColorAtPoint()

QColor getColorAtPoint ( unsigned  index) const
Parameters
indexThe index of the point whose color is desired. It must be less than the number of points returned from getNumPoints().
Returns
The color of the point at the specified index.

◆ getNumPoints()

unsigned getNumPoints ( ) const
Returns
The number of points defined in this color spectrum.

Every point in the spectrum has a different key value, so the return value is also the number of unique keys in the spectrum.

◆ getPointInterpolationMode()

ColorSpectrum::PointInterpolationMode getPointInterpolationMode ( ) const
Returns
The interpolation mode of the color spectrum.
See also
setPointInterpolationMode

◆ getValueAtPoint()

double getValueAtPoint ( unsigned  index) const
Parameters
indexThe index of the point whose value is desired. It must be less than the number of points returned from getNumPoints().
Returns
The value of the point at the specified index.

◆ invert()

void invert ( )

This inverts the colour scheme, so current point 0 maps to the end and vice-versa

◆ load()

bool load ( const DataExecution::SerializedItem item)
overridevirtual
Parameters
itemSupplies the state to be loaded into this object.

This function will load the state of this object from item. For many object types, this is most easily implemented in terms of the object's input operator (>>).

It would be reasonable for subclass implementations to assume that the data provided in item is in the same form as would have been saved from a call to save.

Returns
If the state of the object could not be loaded, the function returns false. A return value of true implies that the object state was successfully loaded. If canSerialize() returns false, this function must do nothing except return false as well.

Implements Serialize.

◆ operator!=()

bool operator!= ( const ColorSpectrum s) const
Parameters
sThe spectrum to compare against.
Returns
True if s and this spectrum are not the same.

◆ operator=()

ColorSpectrum & operator= ( const ColorSpectrum spec)
Parameters
specThe surface specifier to assign to this one.

◆ operator==()

bool operator== ( const ColorSpectrum s) const
Parameters
sThe spectrum to compare against.
Returns
True if s and this spectrum are the same.

◆ removePoint()

void removePoint ( unsigned  index)
Parameters
indexThe index of the point to remove. It must be less than the number of points returned from getNumPoints().

◆ save()

bool save ( DataExecution::SerializedItem item) const
overridevirtual
Parameters
itemWhere to save the state of this object.

This function will serialise the object by saving it to item. For most object types, this is most easily implemented in terms of the object's output operator (<<).

It is allowable for an object to only serialize part of itself. This would be useful if the data it represents has a set of defaults and only those values different to the defaults need to be serialized. Such an implementation then needs to be careful how it handles the complimentary load member function.

Returns
If an object could not be serialised or if the underlying object type does not support serialisation, this function should return false. A return value of true implies that the object was successfully saved to item. If canSerialize() returns false, this function must do nothing except return false as well.

Implements Serialize.

◆ setPointInterpolationMode()

void setPointInterpolationMode ( PointInterpolationMode  mode)

Sets the interpolation mode of the color spectrum. The interpolation mode controls how numbers are mapped to colors.

Parameters
modeThe desired interpolation mode of the color spectrum.