10 #include "qwt_color_map.h" 
   11 #include "qwt_interval.h" 
   15 static inline QRgb qwtHsvToRgb( 
int h, 
int s, 
int v, 
int a )
 
   18     return QColor::fromHsv( h, s, v, a ).rgb();
 
   21     const double vs = v * s / 255.0;
 
   22     const int p = v - qRound( vs );
 
   28             const double r = ( 60 - h ) / 60.0;
 
   29             return qRgba( v, v - qRound( r * vs ), p, a );
 
   33             const double r = ( h - 60 ) / 60.0;
 
   34             return qRgba( v - qRound( r * vs ), v, p, a );
 
   38             const double r = ( 180 - h ) / 60.0;
 
   39             return qRgba( p, v, v - qRound( r * vs ), a );
 
   43             const double r = ( h - 180 ) / 60.0;
 
   44             return qRgba( p, v - qRound( r * vs ), v, a );
 
   48             const double r = ( 300 - h ) / 60.0;
 
   49             return qRgba( v - qRound( r * vs ), p, v, a );
 
   54             const double r = ( h - 300 ) / 60.0;
 
   55             return qRgba( v, p, v - qRound( r * vs ), a );
 
   61 class QwtLinearColorMap::ColorStops
 
   67         m_stops.reserve( 256 );
 
   70     void insert( 
double pos, 
const QColor& 
color );
 
   86         ColorStop( 
double p, 
const QColor& c ):
 
  106             rStep = gStep = bStep = aStep = 0.0;
 
  110         void updateSteps( 
const ColorStop& nextStop )
 
  112             rStep = nextStop.r - r;
 
  113             gStep = nextStop.g - g;
 
  114             bStep = nextStop.b - b;
 
  115             aStep = nextStop.a - a;
 
  116             posStep = nextStop.pos - pos;
 
  124         double rStep, gStep, bStep, aStep;
 
  125         double r0, g0, b0, a0;
 
  129     inline int findUpper( 
double pos ) 
const;
 
  134 void QwtLinearColorMap::ColorStops::insert( 
double pos, 
const QColor& color )
 
  139     if ( pos < 0.0 || pos > 1.0 )
 
  143     if ( m_stops.size() == 0 )
 
  150         index = findUpper( pos );
 
  151         if ( index == m_stops.size() ||
 
  152             qAbs( m_stops[index].pos - pos ) >= 0.001 )
 
  154             m_stops.resize( m_stops.size() + 1 );
 
  155             for ( 
int i = m_stops.size() - 1; i > index; i-- )
 
  156                 m_stops[i] = m_stops[i - 1];
 
  160     m_stops[index] = ColorStop( pos, color );
 
  161     if ( color.alpha() != 255 )
 
  165         m_stops[index - 1].updateSteps( m_stops[index] );
 
  167     if ( index < m_stops.size() - 1 )
 
  168         m_stops[index].updateSteps( m_stops[index + 1] );
 
  174     for ( 
int i = 0; i < m_stops.size(); i++ )
 
  175         positions[i] = m_stops[i].pos;
 
  179 inline int QwtLinearColorMap::ColorStops::findUpper( 
double pos )
 const 
  182     int n = m_stops.size();
 
  184     const ColorStop* stops = m_stops.data();
 
  188         const int half = n >> 1;
 
  189         const int middle = index + half;
 
  191         if ( stops[middle].pos <= pos )
 
  203 inline QRgb QwtLinearColorMap::ColorStops::rgb(
 
  207         return m_stops[0].rgb;
 
  209         return m_stops[ m_stops.size() - 1 ].rgb;
 
  211     const int index = findUpper( pos );
 
  212     if ( mode == FixedColors )
 
  214         return m_stops[index - 1].rgb;
 
  218         const ColorStop& s1 = m_stops[index - 1];
 
  220         const double ratio = ( pos - s1.pos ) / ( s1.posStep );
 
  222         const int r = int( s1.r0 + ratio * s1.rStep );
 
  223         const int g = int( s1.g0 + ratio * s1.gStep );
 
  224         const int b = int( s1.b0 + ratio * s1.bStep );
 
  230                 const int a = int( s1.a0 + ratio * s1.aStep );
 
  231                 return qRgba( r, g, b, a );
 
  235                 return qRgba( r, g, b, s1.a );
 
  240             return qRgb( r, g, b );
 
  281     const double width = interval.
width();
 
  288     const int maxIndex = numColors - 1;
 
  292     const double v = maxIndex * ( ( value - interval.
minValue() ) / width );
 
  293     return static_cast< unsigned int >( v + 0.5 );
 
  310     for ( 
int i = 0; i < 256; i++ )
 
  311         table[i] = 
rgb( interval, i );
 
  331     const double step = 1.0 / ( numColors - 1 );
 
  332     for ( 
int i = 0; i < numColors; i++ )
 
  333         table[i] = 
rgb( interval, step * i );
 
  338 class QwtLinearColorMap::PrivateData
 
  354     m_data = 
new PrivateData;
 
  371     m_data = 
new PrivateData;
 
  416     const QColor& color1, 
const QColor& color2 )
 
  418     m_data->colorStops = ColorStops();
 
  419     m_data->colorStops.insert( 0.0, 
color1 );
 
  420     m_data->colorStops.insert( 1.0, 
color2 );
 
  435     if ( value >= 0.0 && value <= 1.0 )
 
  436         m_data->colorStops.insert( value, 
color );
 
  444     return m_data->colorStops.stops();
 
  453     return QColor::fromRgba( m_data->colorStops.rgb( m_data->mode, 0.0 ) );
 
  462     return QColor::fromRgba( m_data->colorStops.rgb( m_data->mode, 1.0 ) );
 
  476     const double width = interval.
width();
 
  480     const double ratio = ( value - interval.
minValue() ) / width;
 
  481     return m_data->colorStops.rgb( m_data->mode, ratio );
 
  497     const double width = interval.
width();
 
  505         return numColors - 1;
 
  507     const double v = ( numColors - 1 ) * ( value - interval.
minValue() ) / width;
 
  508     return static_cast< unsigned int >( ( m_data->mode == 
FixedColors ) ? v : v + 0.5 );
 
  511 class QwtAlphaColorMap::PrivateData
 
  542     m_data = 
new PrivateData;
 
  560     m_data->color = 
color;
 
  561     m_data->rgb = 
color.rgb() & qRgba( 255, 255, 255, 0 );
 
  563     m_data->rgbMin = m_data->rgb | ( m_data->alpha1 << 24 );
 
  564     m_data->rgbMax = m_data->rgb | ( m_data->alpha2 << 24 );
 
  573     return m_data->color;
 
  589     m_data->alpha1 = qBound( 0, 
alpha1, 255 );
 
  590     m_data->alpha2 = qBound( 0, 
alpha2, 255 );
 
  592     m_data->rgbMin = m_data->rgb | ( 
alpha1 << 24 );
 
  593     m_data->rgbMax = m_data->rgb | ( 
alpha2 << 24 );
 
  602     return m_data->alpha1;
 
  611     return m_data->alpha2;
 
  624     const double width = interval.
width();
 
  632         return m_data->rgbMax;
 
  634     const double ratio = ( value - interval.
minValue() ) / width;
 
  635     const int alpha = m_data->alpha1 + qRound( ratio * ( m_data->alpha2 - m_data->alpha1 ) );
 
  637     return m_data->rgb | ( alpha << 24 );
 
  640 class QwtHueColorMap::PrivateData
 
  658 QwtHueColorMap::PrivateData::PrivateData()
 
  668 void QwtHueColorMap::PrivateData::updateTable()
 
  670     const int p = qRound( value * ( 255 - saturation ) / 255.0 );
 
  671     const double vs = value * saturation / 255.0;
 
  673     for ( 
int i = 0; i < 60; i++ )
 
  675         const double r = ( 60 - i ) / 60.0;
 
  676         rgbTable[i] = qRgba( value, qRound( value - r * vs ), p, alpha );
 
  679     for ( 
int i = 60; i < 120; i++ )
 
  681         const double r = ( i - 60 ) / 60.0;
 
  682         rgbTable[i] = qRgba( qRound( value - r * vs ), value, p, alpha );
 
  685     for ( 
int i = 120; i < 180; i++ )
 
  687         const double r = ( 180 - i ) / 60.0;
 
  688         rgbTable[i] = qRgba( p, value, qRound( value - r * vs ), alpha );
 
  691     for ( 
int i = 180; i < 240; i++ )
 
  693         const double r = ( i - 180 ) / 60.0;
 
  694         rgbTable[i] = qRgba( p, qRound( value - r * vs ), value, alpha );
 
  697     for ( 
int i = 240; i < 300; i++ )
 
  699         const double r = ( 300 - i ) / 60.0;
 
  700         rgbTable[i] = qRgba( qRound( value - r * vs ), p, value, alpha );
 
  703     for ( 
int i = 300; i < 360; i++ )
 
  705         const double r = ( i - 300 ) / 60.0;
 
  706         rgbTable[i] = qRgba( value, p, qRound( value - r * vs ), alpha );
 
  709     rgbMin = rgbTable[ hue1 % 360 ];
 
  710     rgbMax = rgbTable[ hue2 % 360 ];
 
  726     m_data = 
new PrivateData;
 
  748     m_data->hue1 = qMax( 
hue1, 0 );
 
  749     m_data->hue2 = qMax( 
hue2, 0 );
 
  751     m_data->rgbMin = m_data->rgbTable[ 
hue1 % 360 ];
 
  752     m_data->rgbMax = m_data->rgbTable[ 
hue2 % 360 ];
 
  771         m_data->updateTable();
 
  788     if ( 
value != m_data->value )
 
  790         m_data->value = 
value;
 
  791         m_data->updateTable();
 
  809     if ( 
alpha != m_data->alpha )
 
  811         m_data->alpha = 
alpha;
 
  812         m_data->updateTable();
 
  840     return m_data->saturation;
 
  849     return m_data->value;
 
  858     return m_data->alpha;
 
  871     const double width = interval.
width();
 
  876         return m_data->rgbMin;
 
  879         return m_data->rgbMax;
 
  881     const double ratio = ( 
value - interval.
minValue() ) / width;
 
  883     int hue = m_data->hue1 + qRound( ratio * ( m_data->hue2 - m_data->hue1 ) );
 
  892     return m_data->rgbTable[hue];
 
  895 class QwtSaturationValueColorMap::PrivateData
 
  916             rgbTable.resize( 256 );
 
  918             for ( 
int i = 0; i < 256; i++ )
 
  921             tableType = Saturation;
 
  925             rgbTable.resize( 256 );
 
  927             for ( 
int i = 0; i < 256; i++ )
 
  928                 rgbTable[i] = qwtHsvToRgb( 
hue, sat1, i, 
alpha );
 
  934             rgbTable.resize( 256 * 256 );
 
  936             for ( 
int s = 0; s < 256; s++ )
 
  938                 const int v0 = s * 256;
 
  940                 for ( 
int v = 0; v < 256; v++ )
 
  941                     rgbTable[v0 + v] = qwtHsvToRgb( 
hue, s, v, 
alpha );
 
  974     m_data = 
new PrivateData;
 
  996     if ( 
hue != m_data->hue )
 
  999         m_data->updateTable();
 
 1017     int saturation1, 
int saturation2 )
 
 1027         m_data->updateTable();
 
 1048     if ( ( 
value1 != m_data->value1 ) || ( 
value2 != m_data->value2 ) )
 
 1053         m_data->updateTable();
 
 1071     if ( 
alpha != m_data->alpha )
 
 1073         m_data->alpha = 
alpha;
 
 1074         m_data->updateTable();
 
 1093     return m_data->sat1;
 
 1102     return m_data->sat2;
 
 1111     return m_data->value1;
 
 1120     return m_data->value2;
 
 1129     return m_data->alpha;
 
 1143     const double width = interval.
width();
 
 1147     const QRgb* rgbTable = m_data->rgbTable.constData();
 
 1149     switch( m_data->tableType )
 
 1151         case PrivateData::Saturation:
 
 1153             if ( value <= interval.
minValue() )
 
 1154                 return m_data->rgbTable[m_data->sat1];
 
 1156             if ( value >= interval.
maxValue() )
 
 1157                 return m_data->rgbTable[m_data->sat2];
 
 1159             const double ratio = ( value - interval.
minValue() ) / width;
 
 1160             const int sat = m_data->sat1
 
 1161                 + qRound( ratio * ( m_data->sat2 - m_data->sat1 ) );
 
 1163             return rgbTable[sat];
 
 1165         case PrivateData::Value:
 
 1167             if ( value <= interval.
minValue() )
 
 1168                 return m_data->rgbTable[m_data->value1];
 
 1170             if ( value >= interval.
maxValue() )
 
 1171                 return m_data->rgbTable[m_data->value2];
 
 1173             const double ratio = ( value - interval.
minValue() ) / width;
 
 1174             const int v = m_data->value1 +
 
 1175                 qRound( ratio * ( m_data->value2 - m_data->value1 ) );
 
 1177             return rgbTable[ v ];
 
 1182             if ( value <= interval.
minValue() )
 
 1187             else if ( value >= interval.
maxValue() )
 
 1194                 const double ratio = ( value - interval.
minValue() ) / width;
 
 1196                 v = m_data->value1 + qRound( ratio * ( m_data->value2 - m_data->value1 ) );
 
 1197                 s = m_data->sat1 + qRound( ratio * ( m_data->sat2 - m_data->sat1 ) );
 
 1200             return rgbTable[ 256 * s + v ];
 
virtual QRgb rgb(const QwtInterval &, double value) const override
Map a value of a given interval into a alpha value.
void setColor(const QColor &)
void setAlphaInterval(int alpha1, int alpha2)
virtual ~QwtAlphaColorMap()
Destructor.
QwtAlphaColorMap(const QColor &=QColor(Qt::gray))
Constructor.
QwtColorMap is used to map values into colors.
virtual uint colorIndex(int numColors, const QwtInterval &interval, double value) const
Map a value of a given interval into a color index.
QwtColorMap(Format=QwtColorMap::RGB)
QColor color(const QwtInterval &, double value) const
virtual QVector< QRgb > colorTable(int numColors) const
virtual QVector< QRgb > colorTable256() const
virtual QRgb rgb(const QwtInterval &interval, double value) const =0
virtual ~QwtColorMap()
Destructor.
QwtHueColorMap(QwtColorMap::Format=QwtColorMap::RGB)
Constructor.
void setSaturation(int saturation)
Set the the saturation coordinate.
void setHueInterval(int hue1, int hue2)
void setAlpha(int alpha)
Set the the alpha coordinate.
virtual ~QwtHueColorMap()
Destructor.
virtual QRgb rgb(const QwtInterval &, double value) const override
void setValue(int value)
Set the the value coordinate.
A class representing an interval.
double width() const
Return the width of an interval.
QwtLinearColorMap(QwtColorMap::Format=QwtColorMap::RGB)
QVector< double > colorStops() const
virtual uint colorIndex(int numColors, const QwtInterval &, double value) const override
Map a value of a given interval into a color index.
virtual ~QwtLinearColorMap()
Destructor.
void addColorStop(double value, const QColor &)
virtual QRgb rgb(const QwtInterval &, double value) const override
void setColorInterval(const QColor &color1, const QColor &color2)
@ ScaledColors
Interpolating the colors of the adjacent stops.
@ FixedColors
Return the color from the next lower color stop.
void setMode(Mode)
Set the mode of the color map.
void setSaturationInterval(int sat1, int sat2)
Set the interval for the saturation coordinate.
void setAlpha(int alpha)
Set the the alpha coordinate.
virtual QRgb rgb(const QwtInterval &, double value) const override
QwtSaturationValueColorMap()
Constructor.
virtual ~QwtSaturationValueColorMap()
Destructor.
void setValueInterval(int value1, int value2)
Set the interval for the value coordinate.
void setHue(int hue)
Set the the hue coordinate.