Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_color_map.cpp


Ignore:
Timestamp:
Nov 3, 2026, 10:53:35 AM (3 weeks ago)
Author:
stuerze
Message:

deprecated functions exchanged libs updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/qwt/qwt_color_map.cpp

    r9383 r11158  
    1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
     1/******************************************************************************
    22 * Qwt Widget Library
    33 * Copyright (C) 1997   Josef Wilgen
    … …  
    99
    1010#include "qwt_color_map.h"
    11 #include "qwt_math.h"
    1211#include "qwt_interval.h"
    13 #include <qnumeric.h>
     12
     13#include <qvector.h>
     14
     15static inline QRgb qwtHsvToRgb( int h, int s, int v, int a )
     16{
     17#if 0
     18    return QColor::fromHsv( h, s, v, a ).rgb();
     19#else
     20
     21    const double vs = v * s / 255.0;
     22    const int p = v - qRound( vs );
     23
     24    switch( h / 60 )
     25    {
     26        case 0:
     27        {
     28            const double r = ( 60 - h ) / 60.0;
     29            return qRgba( v, v - qRound( r * vs ), p, a );
     30        }
     31        case 1:
     32        {
     33            const double r = ( h - 60 ) / 60.0;
     34            return qRgba( v - qRound( r * vs ), v, p, a );
     35        }
     36        case 2:
     37        {
     38            const double r = ( 180 - h ) / 60.0;
     39            return qRgba( p, v, v - qRound( r * vs ), a );
     40        }
     41        case 3:
     42        {
     43            const double r = ( h - 180 ) / 60.0;
     44            return qRgba( p, v - qRound( r * vs ), v, a );
     45        }
     46        case 4:
     47        {
     48            const double r = ( 300 - h ) / 60.0;
     49            return qRgba( v - qRound( r * vs ), p, v, a );
     50        }
     51        case 5:
     52        default:
     53        {
     54            const double r = ( h - 300 ) / 60.0;
     55            return qRgba( v, p, v - qRound( r * vs ), a );
     56        }
     57    }
     58#endif
     59}
    1460
    1561class QwtLinearColorMap::ColorStops
    1662{
    17 public:
     63  public:
    1864    ColorStops():
    19         d_doAlpha( false )
    20     {
    21         d_stops.reserve( 256 );
    22     }
    23 
    24     void insert( double pos, const QColor &color );
     65        m_doAlpha( false )
     66    {
     67        m_stops.reserve( 256 );
     68    }
     69
     70    void insert( double pos, const QColor& color );
    2571    QRgb rgb( QwtLinearColorMap::Mode, double pos ) const;
    2672
    27     QVector<double> stops() const;
    28 
    29 private:
     73    QVector< double > stops() const;
     74
     75  private:
    3076
    3177    class ColorStop
    3278    {
    33     public:
     79      public:
    3480        ColorStop():
    3581            pos( 0.0 ),
    … …  
    3884        };
    3985
    40         ColorStop( double p, const QColor &c ):
     86        ColorStop( double p, const QColor& c ):
    4187            pos( p ),
    4288            rgb( c.rgba() )
    … …  
    4894
    4995            /*
    50                 when mapping a value to rgb we will have to calcualate:
     96                when mapping a value to rgb we will have to calculate:
    5197                   - const int v = int( ( s1.v0 + ratio * s1.vStep ) + 0.5 );
    5298
    … …  
    62108        }
    63109
    64         void updateSteps( const ColorStop &nextStop )
     110        void updateSteps( const ColorStop& nextStop )
    65111        {
    66112            rStep = nextStop.r - r;
    … …  
    82128
    83129    inline int findUpper( double pos ) const;
    84     QVector<ColorStop> d_stops;
    85     bool d_doAlpha;
     130    QVector< ColorStop > m_stops;
     131    bool m_doAlpha;
    86132};
    87133
    88 void QwtLinearColorMap::ColorStops::insert( double pos, const QColor &color )
     134void QwtLinearColorMap::ColorStops::insert( double pos, const QColor& color )
    89135{
    90136    // Lookups need to be very fast, insertions are not so important.
    … …  
    95141
    96142    int index;
    97     if ( d_stops.size() == 0 )
     143    if ( m_stops.size() == 0 )
    98144    {
    99145        index = 0;
    100         d_stops.resize( 1 );
     146        m_stops.resize( 1 );
    101147    }
    102148    else
    103149    {
    104150        index = findUpper( pos );
    105         if ( index == d_stops.size() ||
    106                 qAbs( d_stops[index].pos - pos ) >= 0.001 )
    107         {
    108             d_stops.resize( d_stops.size() + 1 );
    109             for ( int i = d_stops.size() - 1; i > index; i-- )
    110                 d_stops[i] = d_stops[i-1];
    111         }
    112     }
    113 
    114     d_stops[index] = ColorStop( pos, color );
     151        if ( index == m_stops.size() ||
     152            qAbs( m_stops[index].pos - pos ) >= 0.001 )
     153        {
     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];
     157        }
     158    }
     159
     160    m_stops[index] = ColorStop( pos, color );
    115161    if ( color.alpha() != 255 )
    116         d_doAlpha = true;
     162        m_doAlpha = true;
    117163
    118164    if ( index > 0 )
    119         d_stops[index-1].updateSteps( d_stops[index] );
    120 
    121     if ( index < d_stops.size() - 1 )
    122         d_stops[index].updateSteps( d_stops[index+1] );
    123 }
    124 
    125 inline QVector<double> QwtLinearColorMap::ColorStops::stops() const
    126 {
    127     QVector<double> positions( d_stops.size() );
    128     for ( int i = 0; i < d_stops.size(); i++ )
    129         positions[i] = d_stops[i].pos;
     165        m_stops[index - 1].updateSteps( m_stops[index] );
     166
     167    if ( index < m_stops.size() - 1 )
     168        m_stops[index].updateSteps( m_stops[index + 1] );
     169}
     170
     171inline QVector< double > QwtLinearColorMap::ColorStops::stops() const
     172{
     173    QVector< double > positions( m_stops.size() );
     174    for ( int i = 0; i < m_stops.size(); i++ )
     175        positions[i] = m_stops[i].pos;
    130176    return positions;
    131177}
    … …  
    134180{
    135181    int index = 0;
    136     int n = d_stops.size();
    137 
    138     const ColorStop *stops = d_stops.data();
     182    int n = m_stops.size();
     183
     184    const ColorStop* stops = m_stops.data();
    139185
    140186    while ( n > 0 )
    … …  
    159205{
    160206    if ( pos <= 0.0 )
    161         return d_stops[0].rgb;
     207        return m_stops[0].rgb;
    162208    if ( pos >= 1.0 )
    163         return d_stops[ d_stops.size() - 1 ].rgb;
     209        return m_stops[ m_stops.size() - 1 ].rgb;
    164210
    165211    const int index = findUpper( pos );
    166212    if ( mode == FixedColors )
    167213    {
    168         return d_stops[index-1].rgb;
     214        return m_stops[index - 1].rgb;
    169215    }
    170216    else
    171217    {
    172         const ColorStop &s1 = d_stops[index-1];
     218        const ColorStop& s1 = m_stops[index - 1];
    173219
    174220        const double ratio = ( pos - s1.pos ) / ( s1.posStep );
    … …  
    178224        const int b = int( s1.b0 + ratio * s1.bStep );
    179225
    180         if ( d_doAlpha )
     226        if ( m_doAlpha )
    181227        {
    182228            if ( s1.aStep )
    … …  
    197243}
    198244
    199 //! Constructor
    200 QwtColorMap::QwtColorMap( Format format ):
    201     d_format( format )
     245/*!
     246   Constructor
     247   \param format Format of the color map
     248 */
     249QwtColorMap::QwtColorMap( Format format )
     250    : m_format( format )
    202251{
    203252}
    … …  
    209258
    210259/*!
     260   Set the format of the color map
     261
     262   \param format Format of the color map
     263 */
     264void QwtColorMap::setFormat( Format format )
     265{
     266    m_format = format;
     267}
     268
     269/*!
     270   \brief Map a value of a given interval into a color index
     271
     272   \param numColors Number of colors
     273   \param interval Range for all values
     274   \param value Value to map into a color index
     275
     276   \return Index, between 0 and numColors - 1, or -1 for an invalid value
     277 */
     278uint QwtColorMap::colorIndex( int numColors,
     279    const QwtInterval& interval, double value ) const
     280{
     281    const double width = interval.width();
     282    if ( width <= 0.0 )
     283        return 0;
     284
     285    if ( value <= interval.minValue() )
     286        return 0;
     287
     288    const int maxIndex = numColors - 1;
     289    if ( value >= interval.maxValue() )
     290        return maxIndex;
     291
     292    const double v = maxIndex * ( ( value - interval.minValue() ) / width );
     293    return static_cast< unsigned int >( v + 0.5 );
     294}
     295
     296/*!
    211297   Build and return a color map of 256 colors
    212298
    … …  
    214300   with using colorIndex().
    215301
    216    \param interval Range for the values
    217302   \return A color table, that can be used for a QImage
    218 */
    219 QVector<QRgb> QwtColorMap::colorTable( const QwtInterval &interval ) const
    220 {
    221     QVector<QRgb> table( 256 );
    222 
    223     if ( interval.isValid() )
    224     {
    225         const double step = interval.width() / ( table.size() - 1 );
    226         for ( int i = 0; i < table.size(); i++ )
    227             table[i] = rgb( interval, interval.minValue() + step * i );
    228     }
     303 */
     304QVector< QRgb > QwtColorMap::colorTable256() const
     305{
     306    QVector< QRgb > table( 256 );
     307
     308    const QwtInterval interval( 0, 256 );
     309
     310    for ( int i = 0; i < 256; i++ )
     311        table[i] = rgb( interval, i );
    229312
    230313    return table;
    231314}
    232315
     316/*!
     317   Build and return a color map of arbitrary number of colors
     318
     319   The color table is needed for rendering indexed images in combination
     320   with using colorIndex().
     321
     322   \param numColors Number of colors
     323   \return A color table
     324 */
     325QVector< QRgb > QwtColorMap::colorTable( int numColors ) const
     326{
     327    QVector< QRgb > table( numColors );
     328
     329    const QwtInterval interval( 0.0, 1.0 );
     330
     331    const double step = 1.0 / ( numColors - 1 );
     332    for ( int i = 0; i < numColors; i++ )
     333        table[i] = rgb( interval, step * i );
     334
     335    return table;
     336}
     337
    233338class QwtLinearColorMap::PrivateData
    234339{
    235 public:
     340  public:
    236341    ColorStops colorStops;
    237342    QwtLinearColorMap::Mode mode;
    … …  
    243348
    244349   \param format Preferred format of the color map
    245 */
     350 */
    246351QwtLinearColorMap::QwtLinearColorMap( QwtColorMap::Format format ):
    247352    QwtColorMap( format )
    248353{
    249     d_data = new PrivateData;
    250     d_data->mode = ScaledColors;
     354    m_data = new PrivateData;
     355    m_data->mode = ScaledColors;
    251356
    252357    setColorInterval( Qt::blue, Qt::yellow );
    … …  
    259364   \param color2 Color used for the maximum value of the value interval
    260365   \param format Preferred format for the color map
    261 */
    262 QwtLinearColorMap::QwtLinearColorMap( const QColor &color1,
    263         const QColor &color2, QwtColorMap::Format format ):
    264     QwtColorMap( format )
    265 {
    266     d_data = new PrivateData;
    267     d_data->mode = ScaledColors;
     366 */
     367QwtLinearColorMap::QwtLinearColorMap( const QColor& color1,
     368        const QColor& color2, QwtColorMap::Format format )
     369    : QwtColorMap( format )
     370{
     371    m_data = new PrivateData;
     372    m_data->mode = ScaledColors;
    268373    setColorInterval( color1, color2 );
    269374}
    … …  
    272377QwtLinearColorMap::~QwtLinearColorMap()
    273378{
    274     delete d_data;
     379    delete m_data;
    275380}
    276381
    … …  
    283388
    284389   \sa mode()
    285 */
     390 */
    286391void QwtLinearColorMap::setMode( Mode mode )
    287392{
    288     d_data->mode = mode;
     393    m_data->mode = mode;
    289394}
    290395
    … …  
    292397   \return Mode of the color map
    293398   \sa setMode()
    294 */
     399 */
    295400QwtLinearColorMap::Mode QwtLinearColorMap::mode() const
    296401{
    297     return d_data->mode;
     402    return m_data->mode;
    298403}
    299404
    … …  
    307412
    308413   \sa color1(), color2()
    309 */
     414 */
    310415void QwtLinearColorMap::setColorInterval(
    311     const QColor &color1, const QColor &color2 )
    312 {
    313     d_data->colorStops = ColorStops();
    314     d_data->colorStops.insert( 0.0, color1 );
    315     d_data->colorStops.insert( 1.0, color2 );
     416    const QColor& color1, const QColor& color2 )
     417{
     418    m_data->colorStops = ColorStops();
     419    m_data->colorStops.insert( 0.0, color1 );
     420    m_data->colorStops.insert( 1.0, color2 );
    316421}
    317422
    … …  
    325430   \param value Value between [0.0, 1.0]
    326431   \param color Color stop
    327 */
     432 */
    328433void QwtLinearColorMap::addColorStop( double value, const QColor& color )
    329434{
    330435    if ( value >= 0.0 && value <= 1.0 )
    331         d_data->colorStops.insert( value, color );
     436        m_data->colorStops.insert( value, color );
    332437}
    333438
    334439/*!
    335440   \return Positions of color stops in increasing order
    336 */
    337 QVector<double> QwtLinearColorMap::colorStops() const
    338 {
    339     return d_data->colorStops.stops();
    340 }
    341 
    342 /*!
    343   \return the first color of the color range
    344   \sa setColorInterval()
    345 */
     441 */
     442QVector< double > QwtLinearColorMap::colorStops() const
     443{
     444    return m_data->colorStops.stops();
     445}
     446
     447/*!
     448   \return the first color of the color range
     449   \sa setColorInterval()
     450 */
    346451QColor QwtLinearColorMap::color1() const
    347452{
    348     return QColor::fromRgba( d_data->colorStops.rgb( d_data->mode, 0.0 ) );
    349 }
    350 
    351 /*!
    352   \return the second color of the color range
    353   \sa setColorInterval()
    354 */
     453    return QColor::fromRgba( m_data->colorStops.rgb( m_data->mode, 0.0 ) );
     454}
     455
     456/*!
     457   \return the second color of the color range
     458   \sa setColorInterval()
     459 */
    355460QColor QwtLinearColorMap::color2() const
    356461{
    357     return QColor::fromRgba( d_data->colorStops.rgb( d_data->mode, 1.0 ) );
    358 }
    359 
    360 /*!
    361   Map a value of a given interval into a RGB value
    362 
    363   \param interval Range for all values
    364   \param value Value to map into a RGB value
    365 
    366   \return RGB value for value
    367 */
     462    return QColor::fromRgba( m_data->colorStops.rgb( m_data->mode, 1.0 ) );
     463}
     464
     465/*!
     466   Map a value of a given interval into a RGB value
     467
     468   \param interval Range for all values
     469   \param value Value to map into a RGB value
     470
     471   \return RGB value for value
     472 */
    368473QRgb QwtLinearColorMap::rgb(
    369     const QwtInterval &interval, double value ) const
    370 {
    371     if ( qIsNaN(value) )
    372         return 0u;
    373 
     474    const QwtInterval& interval, double value ) const
     475{
    374476    const double width = interval.width();
    375477    if ( width <= 0.0 )
    … …  
    377479
    378480    const double ratio = ( value - interval.minValue() ) / width;
    379     return d_data->colorStops.rgb( d_data->mode, ratio );
    380 }
    381 
    382 /*!
    383   \brief Map a value of a given interval into a color index
    384 
    385   \param interval Range for all values
    386   \param value Value to map into a color index
    387 
    388   \return Index, between 0 and 255
    389 */
    390 unsigned char QwtLinearColorMap::colorIndex(
    391     const QwtInterval &interval, double value ) const
     481    return m_data->colorStops.rgb( m_data->mode, ratio );
     482}
     483
     484/*!
     485   \brief Map a value of a given interval into a color index
     486
     487   \param numColors Size of the color table
     488   \param interval Range for all values
     489   \param value Value to map into a color index
     490
     491   \return Index, between 0 and 255
     492   \note NaN values are mapped to 0
     493 */
     494uint QwtLinearColorMap::colorIndex( int numColors,
     495    const QwtInterval& interval, double value ) const
    392496{
    393497    const double width = interval.width();
    394 
    395     if ( qIsNaN(value) || width <= 0.0 || value <= interval.minValue() )
     498    if ( width <= 0.0 )
    396499        return 0;
    397500
     501    if ( value <= interval.minValue() )
     502        return 0;
     503
    398504    if ( value >= interval.maxValue() )
    399         return 255;
    400 
    401     const double ratio = ( value - interval.minValue() ) / width;
    402 
    403     unsigned char index;
    404     if ( d_data->mode == FixedColors )
    405         index = static_cast<unsigned char>( ratio * 255 ); // always floor
    406     else
    407         index = static_cast<unsigned char>( ratio * 255 + 0.5 );
    408 
    409     return index;
     505        return numColors - 1;
     506
     507    const double v = ( numColors - 1 ) * ( value - interval.minValue() ) / width;
     508    return static_cast< unsigned int >( ( m_data->mode == FixedColors ) ? v : v + 0.5 );
    410509}
    411510
    412511class QwtAlphaColorMap::PrivateData
    413512{
    414 public:
     513  public:
     514    PrivateData()
     515        : alpha1(0)
     516        , alpha2(255)
     517    {
     518    }
     519
     520    int alpha1, alpha2;
     521
    415522    QColor color;
    416523    QRgb rgb;
     524
     525    QRgb rgbMin;
    417526    QRgb rgbMax;
    418527};
    … …  
    420529
    421530/*!
    422    Constructor
     531   \brief Constructor
     532
     533   The alpha interval is initialized by 0 to 255.
     534
    423535   \param color Color of the map
    424 */
    425 QwtAlphaColorMap::QwtAlphaColorMap( const QColor &color ):
    426     QwtColorMap( QwtColorMap::RGB )
    427 {
    428     d_data = new PrivateData;
     536
     537   \sa setColor(), setAlphaInterval()
     538 */
     539QwtAlphaColorMap::QwtAlphaColorMap( const QColor& color )
     540    : QwtColorMap( QwtColorMap::RGB )
     541{
     542    m_data = new PrivateData;
    429543    setColor( color );
    430544}
    … …  
    433547QwtAlphaColorMap::~QwtAlphaColorMap()
    434548{
    435     delete d_data;
     549    delete m_data;
    436550}
    437551
    … …  
    441555   \param color Color
    442556   \sa color()
    443 */
    444 void QwtAlphaColorMap::setColor( const QColor &color )
    445 {
    446     d_data->color = color;
    447     d_data->rgb = color.rgb() & qRgba( 255, 255, 255, 0 );
    448     d_data->rgbMax = d_data->rgb | ( 255 << 24 );
    449 }
    450 
    451 /*!
    452   \return the color
    453   \sa setColor()
    454 */
     557 */
     558void QwtAlphaColorMap::setColor( const QColor& color )
     559{
     560    m_data->color = color;
     561    m_data->rgb = color.rgb() & qRgba( 255, 255, 255, 0 );
     562
     563    m_data->rgbMin = m_data->rgb | ( m_data->alpha1 << 24 );
     564    m_data->rgbMax = m_data->rgb | ( m_data->alpha2 << 24 );
     565}
     566
     567/*!
     568   \return the color
     569   \sa setColor()
     570 */
    455571QColor QwtAlphaColorMap::color() const
    456572{
    457     return d_data->color;
    458 }
    459 
    460 /*!
    461   \brief Map a value of a given interval into a alpha value
    462 
    463   alpha := (value - interval.minValue()) / interval.width();
    464 
    465   \param interval Range for all values
    466   \param value Value to map into a RGB value
    467   \return RGB value, with an alpha value
    468 */
    469 QRgb QwtAlphaColorMap::rgb( const QwtInterval &interval, double value ) const
    470 {
    471     if ( qIsNaN(value) )
    472         return 0u;
    473 
     573    return m_data->color;
     574}
     575
     576/*!
     577   Set the interval for the alpha coordinate
     578
     579   alpha1/alpha2 need to be in the range 0 to 255,
     580   where 255 means opaque and 0 means transparent.
     581
     582   \param alpha1 First alpha coordinate
     583   \param alpha2 Second alpha coordinate
     584
     585   \sa alpha1(), alpha2()
     586 */
     587void QwtAlphaColorMap::setAlphaInterval( int alpha1, int alpha2 )
     588{
     589    m_data->alpha1 = qBound( 0, alpha1, 255 );
     590    m_data->alpha2 = qBound( 0, alpha2, 255 );
     591
     592    m_data->rgbMin = m_data->rgb | ( alpha1 << 24 );
     593    m_data->rgbMax = m_data->rgb | ( alpha2 << 24 );
     594}
     595
     596/*!
     597   \return First alpha coordinate
     598   \sa setAlphaInterval()
     599 */
     600int QwtAlphaColorMap::alpha1() const
     601{
     602    return m_data->alpha1;
     603}
     604
     605/*!
     606   \return Second alpha coordinate
     607   \sa setAlphaInterval()
     608 */
     609int QwtAlphaColorMap::alpha2() const
     610{
     611    return m_data->alpha2;
     612}
     613
     614/*!
     615   \brief Map a value of a given interval into a alpha value
     616
     617   \param interval Range for all values
     618   \param value Value to map into a RGB value
     619
     620   \return RGB value, with an alpha value
     621 */
     622QRgb QwtAlphaColorMap::rgb( const QwtInterval& interval, double value ) const
     623{
    474624    const double width = interval.width();
    475625    if ( width <= 0.0 )
    … …  
    477627
    478628    if ( value <= interval.minValue() )
    479         return d_data->rgb;
     629        return m_data->rgb;
    480630
    481631    if ( value >= interval.maxValue() )
    482         return d_data->rgbMax;
     632        return m_data->rgbMax;
    483633
    484634    const double ratio = ( value - interval.minValue() ) / width;
    485     return d_data->rgb | ( qRound( 255 * ratio ) << 24 );
    486 }
    487 
    488 /*!
    489   Dummy function, needed to be implemented as it is pure virtual
    490   in QwtColorMap. Color indices make no sense in combination with
    491   an alpha channel.
    492 
    493   \return Always 0
    494 */
    495 unsigned char QwtAlphaColorMap::colorIndex(
    496     const QwtInterval &, double ) const
    497 {
    498     return 0;
    499 }
     635    const int alpha = m_data->alpha1 + qRound( ratio * ( m_data->alpha2 - m_data->alpha1 ) );
     636
     637    return m_data->rgb | ( alpha << 24 );
     638}
     639
     640class QwtHueColorMap::PrivateData
     641{
     642  public:
     643    PrivateData();
     644
     645    void updateTable();
     646
     647    int hue1, hue2;
     648    int saturation;
     649    int value;
     650    int alpha;
     651
     652    QRgb rgbMin;
     653    QRgb rgbMax;
     654
     655    QRgb rgbTable[360];
     656};
     657
     658QwtHueColorMap::PrivateData::PrivateData()
     659    : hue1(0)
     660    , hue2(359)
     661    , saturation(255)
     662    , value(255)
     663    , alpha(255)
     664{
     665    updateTable();
     666}
     667
     668void QwtHueColorMap::PrivateData::updateTable()
     669{
     670    const int p = qRound( value * ( 255 - saturation ) / 255.0 );
     671    const double vs = value * saturation / 255.0;
     672
     673    for ( int i = 0; i < 60; i++ )
     674    {
     675        const double r = ( 60 - i ) / 60.0;
     676        rgbTable[i] = qRgba( value, qRound( value - r * vs ), p, alpha );
     677    }
     678
     679    for ( int i = 60; i < 120; i++ )
     680    {
     681        const double r = ( i - 60 ) / 60.0;
     682        rgbTable[i] = qRgba( qRound( value - r * vs ), value, p, alpha );
     683    }
     684
     685    for ( int i = 120; i < 180; i++ )
     686    {
     687        const double r = ( 180 - i ) / 60.0;
     688        rgbTable[i] = qRgba( p, value, qRound( value - r * vs ), alpha );
     689    }
     690
     691    for ( int i = 180; i < 240; i++ )
     692    {
     693        const double r = ( i - 180 ) / 60.0;
     694        rgbTable[i] = qRgba( p, qRound( value - r * vs ), value, alpha );
     695    }
     696
     697    for ( int i = 240; i < 300; i++ )
     698    {
     699        const double r = ( 300 - i ) / 60.0;
     700        rgbTable[i] = qRgba( qRound( value - r * vs ), p, value, alpha );
     701    }
     702
     703    for ( int i = 300; i < 360; i++ )
     704    {
     705        const double r = ( i - 300 ) / 60.0;
     706        rgbTable[i] = qRgba( value, p, qRound( value - r * vs ), alpha );
     707    }
     708
     709    rgbMin = rgbTable[ hue1 % 360 ];
     710    rgbMax = rgbTable[ hue2 % 360 ];
     711}
     712
     713/*!
     714   \brief Constructor
     715
     716   The hue interval is initialized by 0 to 359. All other coordinates
     717   are set to 255.
     718
     719   \param format Format of the color map
     720
     721   \sa setHueInterval(), setSaturation(), setValue(), setValue()
     722 */
     723QwtHueColorMap::QwtHueColorMap( QwtColorMap::Format format )
     724    : QwtColorMap( format )
     725{
     726    m_data = new PrivateData;
     727}
     728
     729//! Destructor
     730QwtHueColorMap::~QwtHueColorMap()
     731{
     732    delete m_data;
     733}
     734
     735/*!
     736   Set the interval for the hue coordinate
     737
     738   hue1/hue2 need to be positive number and can be > 360 to define cycles.
     739   F.e. 420 to 240 defines a map yellow/red/magenta/blue.
     740
     741   \param hue1 First hue coordinate
     742   \param hue2 Second hue coordinate
     743
     744   \sa hue1(), hue2()
     745 */
     746void QwtHueColorMap::setHueInterval( int hue1, int hue2 )
     747{
     748    m_data->hue1 = qMax( hue1, 0 );
     749    m_data->hue2 = qMax( hue2, 0 );
     750
     751    m_data->rgbMin = m_data->rgbTable[ hue1 % 360 ];
     752    m_data->rgbMax = m_data->rgbTable[ hue2 % 360 ];
     753}
     754
     755/*!
     756   \brief Set the the saturation coordinate
     757
     758   saturation needs to be in the range 0 to 255,
     759
     760   \param saturation Saturation coordinate
     761
     762   \sa saturation()
     763 */
     764void QwtHueColorMap::setSaturation( int saturation )
     765{
     766    saturation = qBound( 0, saturation, 255 );
     767
     768    if ( saturation != m_data->saturation )
     769    {
     770        m_data->saturation = saturation;
     771        m_data->updateTable();
     772    }
     773}
     774
     775/*!
     776   \brief Set the the value coordinate
     777
     778   value needs to be in the range 0 to 255,
     779
     780   \param value Value coordinate
     781
     782   \sa value()
     783 */
     784void QwtHueColorMap::setValue( int value )
     785{
     786    value = qBound( 0, value, 255 );
     787
     788    if ( value != m_data->value )
     789    {
     790        m_data->value = value;
     791        m_data->updateTable();
     792    }
     793}
     794
     795/*!
     796   \brief Set the the alpha coordinate
     797
     798   alpha needs to be in the range 0 to 255,
     799   where 255 means opaque and 0 means transparent.
     800
     801   \param alpha Alpha coordinate
     802
     803   \sa alpha()
     804 */
     805void QwtHueColorMap::setAlpha( int alpha )
     806{
     807    alpha = qBound( 0, alpha, 255 );
     808
     809    if ( alpha != m_data->alpha )
     810    {
     811        m_data->alpha = alpha;
     812        m_data->updateTable();
     813    }
     814}
     815
     816/*!
     817   \return First hue coordinate
     818   \sa setHueInterval()
     819 */
     820int QwtHueColorMap::hue1() const
     821{
     822    return m_data->hue1;
     823}
     824
     825/*!
     826   \return Second hue coordinate
     827   \sa setHueInterval()
     828 */
     829int QwtHueColorMap::hue2() const
     830{
     831    return m_data->hue2;
     832}
     833
     834/*!
     835   \return Saturation coordinate
     836   \sa setSaturation()
     837 */
     838int QwtHueColorMap::saturation() const
     839{
     840    return m_data->saturation;
     841}
     842
     843/*!
     844   \return Value coordinate
     845   \sa setValue()
     846 */
     847int QwtHueColorMap::value() const
     848{
     849    return m_data->value;
     850}
     851
     852/*!
     853   \return Alpha coordinate
     854   \sa setAlpha()
     855 */
     856int QwtHueColorMap::alpha() const
     857{
     858    return m_data->alpha;
     859}
     860
     861/*!
     862   Map a value of a given interval into a RGB value
     863
     864   \param interval Range for all values
     865   \param value Value to map into a RGB value
     866
     867   \return RGB value for value
     868 */
     869QRgb QwtHueColorMap::rgb( const QwtInterval& interval, double value ) const
     870{
     871    const double width = interval.width();
     872    if ( width <= 0 )
     873        return 0u;
     874
     875    if ( value <= interval.minValue() )
     876        return m_data->rgbMin;
     877
     878    if ( value >= interval.maxValue() )
     879        return m_data->rgbMax;
     880
     881    const double ratio = ( value - interval.minValue() ) / width;
     882
     883    int hue = m_data->hue1 + qRound( ratio * ( m_data->hue2 - m_data->hue1 ) );
     884    if ( hue >= 360 )
     885    {
     886        hue -= 360;
     887
     888        if ( hue >= 360 )
     889            hue = hue % 360;
     890    }
     891
     892    return m_data->rgbTable[hue];
     893}
     894
     895class QwtSaturationValueColorMap::PrivateData
     896{
     897  public:
     898    PrivateData()
     899        : hue(0)
     900        , sat1(255)
     901        , sat2(255)
     902        , value1(0)
     903        , value2(255)
     904        , alpha(255)
     905        , tableType(Invalid)
     906    {
     907        updateTable();
     908    }
     909
     910    void updateTable()
     911    {
     912        tableType = Invalid;
     913
     914        if ( ( value1 == value2 ) && ( sat1 != sat2 ) )
     915        {
     916            rgbTable.resize( 256 );
     917
     918            for ( int i = 0; i < 256; i++ )
     919                rgbTable[i] = qwtHsvToRgb( hue, i, value1, alpha );
     920
     921            tableType = Saturation;
     922        }
     923        else if ( ( value1 != value2 ) && ( sat1 == sat2 ) )
     924        {
     925            rgbTable.resize( 256 );
     926
     927            for ( int i = 0; i < 256; i++ )
     928                rgbTable[i] = qwtHsvToRgb( hue, sat1, i, alpha );
     929
     930            tableType = Value;
     931        }
     932        else
     933        {
     934            rgbTable.resize( 256 * 256 );
     935
     936            for ( int s = 0; s < 256; s++ )
     937            {
     938                const int v0 = s * 256;
     939
     940                for ( int v = 0; v < 256; v++ )
     941                    rgbTable[v0 + v] = qwtHsvToRgb( hue, s, v, alpha );
     942            }
     943        }
     944    }
     945
     946    int hue;
     947    int sat1, sat2;
     948    int value1, value2;
     949    int alpha;
     950
     951    enum
     952    {
     953        Invalid,
     954        Value,
     955        Saturation
     956
     957    } tableType;
     958
     959    QVector< QRgb > rgbTable;
     960};
     961
     962/*!
     963   \brief Constructor
     964
     965   The value interval is initialized by 0 to 255,
     966   saturation by 255 to 255. Hue to 0 and alpha to 255.
     967
     968   So the default setting interpolates the value coordinate only.
     969
     970   \sa setHueInterval(), setSaturation(), setValue(), setValue()
     971 */
     972QwtSaturationValueColorMap::QwtSaturationValueColorMap()
     973{
     974    m_data = new PrivateData;
     975}
     976
     977//! Destructor
     978QwtSaturationValueColorMap::~QwtSaturationValueColorMap()
     979{
     980    delete m_data;
     981}
     982
     983/*!
     984   \brief Set the the hue coordinate
     985
     986   Hue coordinates outside 0 to 359 will be interpreted as hue % 360..
     987
     988   \param hue Hue coordinate
     989
     990   \sa hue()
     991 */
     992void QwtSaturationValueColorMap::setHue( int hue )
     993{
     994    hue = hue % 360;
     995
     996    if ( hue != m_data->hue )
     997    {
     998        m_data->hue = hue;
     999        m_data->updateTable();
     1000    }
     1001}
     1002
     1003/*!
     1004   \brief Set the interval for the saturation coordinate
     1005
     1006   When saturation1 == saturation2 the map interpolates between
     1007   the value coordinates only
     1008
     1009   saturation1/saturation2 need to be in the range 0 to 255.
     1010
     1011   \param saturation1 First saturation
     1012   \param saturation2 Second saturation
     1013
     1014   \sa saturation1(), saturation2(), setValueInterval()
     1015 */
     1016void QwtSaturationValueColorMap::setSaturationInterval(
     1017    int saturation1, int saturation2 )
     1018{
     1019    saturation1 = qBound( 0, saturation1, 255 );
     1020    saturation2 = qBound( 0, saturation2, 255 );
     1021
     1022    if ( ( saturation1 != m_data->sat1 ) || ( saturation2 != m_data->sat2 ) )
     1023    {
     1024        m_data->sat1 = saturation1;
     1025        m_data->sat2 = saturation2;
     1026
     1027        m_data->updateTable();
     1028    }
     1029}
     1030
     1031/*!
     1032   \brief Set the interval for the value coordinate
     1033
     1034   When value1 == value2 the map interpolates between the saturation coordinates only.
     1035
     1036   value1/value2 need to be in the range 0 to 255.
     1037
     1038   \param value1 First value
     1039   \param value2 Second value
     1040
     1041   \sa value1(), value2(), setSaturationInterval()
     1042 */
     1043void QwtSaturationValueColorMap::setValueInterval( int value1, int value2 )
     1044{
     1045    value1 = qBound( 0, value1, 255 );
     1046    value2 = qBound( 0, value2, 255 );
     1047
     1048    if ( ( value1 != m_data->value1 ) || ( value2 != m_data->value2 ) )
     1049    {
     1050        m_data->value1 = value1;
     1051        m_data->value2 = value2;
     1052
     1053        m_data->updateTable();
     1054    }
     1055}
     1056
     1057/*!
     1058   \brief Set the the alpha coordinate
     1059
     1060   alpha needs to be in the range 0 to 255,
     1061   where 255 means opaque and 0 means transparent.
     1062
     1063   \param alpha Alpha coordinate
     1064
     1065   \sa alpha()
     1066 */
     1067void QwtSaturationValueColorMap::setAlpha( int alpha )
     1068{
     1069    alpha = qBound( 0, alpha, 255 );
     1070
     1071    if ( alpha != m_data->alpha )
     1072    {
     1073        m_data->alpha = alpha;
     1074        m_data->updateTable();
     1075    }
     1076}
     1077
     1078/*!
     1079   \return Hue coordinate
     1080   \sa setHue()
     1081 */
     1082int QwtSaturationValueColorMap::hue() const
     1083{
     1084    return m_data->hue;
     1085}
     1086
     1087/*!
     1088   \return First saturation coordinate
     1089   \sa setSaturationInterval()
     1090 */
     1091int QwtSaturationValueColorMap::saturation1() const
     1092{
     1093    return m_data->sat1;
     1094}
     1095
     1096/*!
     1097   \return Second saturation coordinate
     1098   \sa setSaturationInterval()
     1099 */
     1100int QwtSaturationValueColorMap::saturation2() const
     1101{
     1102    return m_data->sat2;
     1103}
     1104
     1105/*!
     1106   \return First value coordinate
     1107   \sa setValueInterval()
     1108 */
     1109int QwtSaturationValueColorMap::value1() const
     1110{
     1111    return m_data->value1;
     1112}
     1113
     1114/*!
     1115   \return Second value coordinate
     1116   \sa setValueInterval()
     1117 */
     1118int QwtSaturationValueColorMap::value2() const
     1119{
     1120    return m_data->value2;
     1121}
     1122
     1123/*!
     1124   \return Alpha coordinate
     1125   \sa setAlpha()
     1126 */
     1127int QwtSaturationValueColorMap::alpha() const
     1128{
     1129    return m_data->alpha;
     1130}
     1131
     1132/*!
     1133   Map a value of a given interval into a RGB value
     1134
     1135   \param interval Range for all values
     1136   \param value Value to map into a RGB value
     1137
     1138   \return RGB value for value
     1139 */
     1140QRgb QwtSaturationValueColorMap::rgb(
     1141    const QwtInterval& interval, double value ) const
     1142{
     1143    const double width = interval.width();
     1144    if ( width <= 0 )
     1145        return 0u;
     1146
     1147    const QRgb* rgbTable = m_data->rgbTable.constData();
     1148
     1149    switch( m_data->tableType )
     1150    {
     1151        case PrivateData::Saturation:
     1152        {
     1153            if ( value <= interval.minValue() )
     1154                return m_data->rgbTable[m_data->sat1];
     1155
     1156            if ( value >= interval.maxValue() )
     1157                return m_data->rgbTable[m_data->sat2];
     1158
     1159            const double ratio = ( value - interval.minValue() ) / width;
     1160            const int sat = m_data->sat1
     1161                + qRound( ratio * ( m_data->sat2 - m_data->sat1 ) );
     1162
     1163            return rgbTable[sat];
     1164        }
     1165        case PrivateData::Value:
     1166        {
     1167            if ( value <= interval.minValue() )
     1168                return m_data->rgbTable[m_data->value1];
     1169
     1170            if ( value >= interval.maxValue() )
     1171                return m_data->rgbTable[m_data->value2];
     1172
     1173            const double ratio = ( value - interval.minValue() ) / width;
     1174            const int v = m_data->value1 +
     1175                qRound( ratio * ( m_data->value2 - m_data->value1 ) );
     1176
     1177            return rgbTable[ v ];
     1178        }
     1179        default:
     1180        {
     1181            int s, v;
     1182            if ( value <= interval.minValue() )
     1183            {
     1184                s = m_data->sat1;
     1185                v = m_data->value1;
     1186            }
     1187            else if ( value >= interval.maxValue() )
     1188            {
     1189                s = m_data->sat2;
     1190                v = m_data->value2;
     1191            }
     1192            else
     1193            {
     1194                const double ratio = ( value - interval.minValue() ) / width;
     1195
     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 ) );
     1198            }
     1199
     1200            return rgbTable[ 256 * s + v ];
     1201        }
     1202    }
     1203}
Note: See TracChangeset for help on using the changeset viewer.