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


Ignore:
Timestamp:
May 10, 2017, 3:20:54 PM (7 years ago)
Author:
stoecker
Message:

update qwt and qwtpolar, many QT5 fixes (unfinished)

File:
1 edited

Legend:

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

    r4271 r8127  
    1616{
    1717public:
    18     ColorStops()
    19     {
    20         _stops.reserve( 256 );
     18    ColorStops():
     19        d_doAlpha( false )
     20    {
     21        d_stops.reserve( 256 );
    2122    }
    2223
     
    3940        ColorStop( double p, const QColor &c ):
    4041            pos( p ),
    41             rgb( c.rgb() )
     42            rgb( c.rgba() )
    4243        {
    4344            r = qRed( rgb );
    4445            g = qGreen( rgb );
    4546            b = qBlue( rgb );
     47            a = qAlpha( rgb );
     48
     49            /*
     50                when mapping a value to rgb we will have to calcualate:
     51                   - const int v = int( ( s1.v0 + ratio * s1.vStep ) + 0.5 );
     52
     53                Thus adding 0.5 ( for rounding ) can be done in advance
     54             */
     55            r0 = r + 0.5;
     56            g0 = g + 0.5;
     57            b0 = b + 0.5;
     58            a0 = a + 0.5;
     59
     60            rStep = gStep = bStep = aStep = 0.0;
     61            posStep = 0.0;
     62        }
     63
     64        void updateSteps( const ColorStop &nextStop )
     65        {
     66            rStep = nextStop.r - r;
     67            gStep = nextStop.g - g;
     68            bStep = nextStop.b - b;
     69            aStep = nextStop.a - a;
     70            posStep = nextStop.pos - pos;
    4671        }
    4772
    4873        double pos;
    4974        QRgb rgb;
    50         int r, g, b;
     75        int r, g, b, a;
     76
     77        // precalculated values
     78        double rStep, gStep, bStep, aStep;
     79        double r0, g0, b0, a0;
     80        double posStep;
    5181    };
    5282
    5383    inline int findUpper( double pos ) const;
    54     QVector<ColorStop> _stops;
     84    QVector<ColorStop> d_stops;
     85    bool d_doAlpha;
    5586};
    5687
     
    6495
    6596    int index;
    66     if ( _stops.size() == 0 )
     97    if ( d_stops.size() == 0 )
    6798    {
    6899        index = 0;
    69         _stops.resize( 1 );
     100        d_stops.resize( 1 );
    70101    }
    71102    else
    72103    {
    73104        index = findUpper( pos );
    74         if ( index == _stops.size() ||
    75                 qAbs( _stops[index].pos - pos ) >= 0.001 )
    76         {
    77             _stops.resize( _stops.size() + 1 );
    78             for ( int i = _stops.size() - 1; i > index; i-- )
    79                 _stops[i] = _stops[i-1];
    80         }
    81     }
    82 
    83     _stops[index] = ColorStop( pos, color );
     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 );
     115    if ( color.alpha() != 255 )
     116        d_doAlpha = true;
     117
     118    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] );
    84123}
    85124
    86125inline QVector<double> QwtLinearColorMap::ColorStops::stops() const
    87126{
    88     QVector<double> positions( _stops.size() );
    89     for ( int i = 0; i < _stops.size(); i++ )
    90         positions[i] = _stops[i].pos;
     127    QVector<double> positions( d_stops.size() );
     128    for ( int i = 0; i < d_stops.size(); i++ )
     129        positions[i] = d_stops[i].pos;
    91130    return positions;
    92131}
     
    95134{
    96135    int index = 0;
    97     int n = _stops.size();
    98 
    99     const ColorStop *stops = _stops.data();
     136    int n = d_stops.size();
     137
     138    const ColorStop *stops = d_stops.data();
    100139
    101140    while ( n > 0 )
     
    120159{
    121160    if ( pos <= 0.0 )
    122         return _stops[0].rgb;
     161        return d_stops[0].rgb;
    123162    if ( pos >= 1.0 )
    124         return _stops[ _stops.size() - 1 ].rgb;
     163        return d_stops[ d_stops.size() - 1 ].rgb;
    125164
    126165    const int index = findUpper( pos );
    127166    if ( mode == FixedColors )
    128167    {
    129         return _stops[index-1].rgb;
     168        return d_stops[index-1].rgb;
    130169    }
    131170    else
    132171    {
    133         const ColorStop &s1 = _stops[index-1];
    134         const ColorStop &s2 = _stops[index];
    135 
    136         const double ratio = ( pos - s1.pos ) / ( s2.pos - s1.pos );
    137 
    138         const int r = s1.r + qRound( ratio * ( s2.r - s1.r ) );
    139         const int g = s1.g + qRound( ratio * ( s2.g - s1.g ) );
    140         const int b = s1.b + qRound( ratio * ( s2.b - s1.b ) );
    141 
    142         return qRgb( r, g, b );
     172        const ColorStop &s1 = d_stops[index-1];
     173
     174        const double ratio = ( pos - s1.pos ) / ( s1.posStep );
     175
     176        const int r = int( s1.r0 + ratio * s1.rStep );
     177        const int g = int( s1.g0 + ratio * s1.gStep );
     178        const int b = int( s1.b0 + ratio * s1.bStep );
     179
     180        if ( d_doAlpha )
     181        {
     182            if ( s1.aStep )
     183            {
     184                const int a = int( s1.a0 + ratio * s1.aStep );
     185                return qRgba( r, g, b, a );
     186            }
     187            else
     188            {
     189                return qRgba( r, g, b, s1.a );
     190            }
     191        }
     192        else
     193        {
     194            return qRgb( r, g, b );
     195        }
    143196    }
    144197}
     
    205258   \param color1 Color used for the minimum value of the value interval
    206259   \param color2 Color used for the maximum value of the value interval
    207    \param format Preferred format of the coor map
     260   \param format Preferred format for the color map
    208261*/
    209262QwtLinearColorMap::QwtLinearColorMap( const QColor &color1,
     
    280333
    281334/*!
    282    Return all positions of color stops in increasing order
     335   \return Positions of color stops in increasing order
    283336*/
    284337QVector<double> QwtLinearColorMap::colorStops() const
     
    306359
    307360/*!
    308   Map a value of a given interval into a rgb value
     361  Map a value of a given interval into a RGB value
    309362
    310363  \param interval Range for all values
    311   \param value Value to map into a rgb value
     364  \param value Value to map into a RGB value
     365
     366  \return RGB value for value
    312367*/
    313368QRgb QwtLinearColorMap::rgb(
     
    315370{
    316371    if ( qIsNaN(value) )
    317         return qRgba(0, 0, 0, 0);
     372        return 0u;
    318373
    319374    const double width = interval.width();
    320 
    321     double ratio = 0.0;
    322     if ( width > 0.0 )
    323         ratio = ( value - interval.minValue() ) / width;
    324 
     375    if ( width <= 0.0 )
     376        return 0u;
     377
     378    const double ratio = ( value - interval.minValue() ) / width;
    325379    return d_data->colorStops.rgb( d_data->mode, ratio );
    326380}
    327381
    328382/*!
    329   Map a value of a given interval into a color index, between 0 and 255
     383  \brief Map a value of a given interval into a color index
    330384
    331385  \param interval Range for all values
    332386  \param value Value to map into a color index
     387
     388  \return Index, between 0 and 255
    333389*/
    334390unsigned char QwtLinearColorMap::colorIndex(
     
    341397
    342398    if ( value >= interval.maxValue() )
    343         return ( unsigned char )255;
     399        return 255;
    344400
    345401    const double ratio = ( value - interval.minValue() ) / width;
     
    347403    unsigned char index;
    348404    if ( d_data->mode == FixedColors )
    349         index = ( unsigned char )( ratio * 255 ); // always floor
     405        index = static_cast<unsigned char>( ratio * 255 ); // always floor
    350406    else
    351         index = ( unsigned char )qRound( ratio * 255 );
     407        index = static_cast<unsigned char>( ratio * 255 + 0.5 );
    352408
    353409    return index;
     
    359415    QColor color;
    360416    QRgb rgb;
     417    QRgb rgbMax;
    361418};
    362419
     
    370427{
    371428    d_data = new PrivateData;
     429    setColor( color );
     430}
     431
     432//! Destructor
     433QwtAlphaColorMap::~QwtAlphaColorMap()
     434{
     435    delete d_data;
     436}
     437
     438/*!
     439   Set the color
     440
     441   \param color Color
     442   \sa color()
     443*/
     444void QwtAlphaColorMap::setColor( const QColor &color )
     445{
    372446    d_data->color = color;
    373447    d_data->rgb = color.rgb() & qRgba( 255, 255, 255, 0 );
    374 }
    375 
    376 //! Destructor
    377 QwtAlphaColorMap::~QwtAlphaColorMap()
    378 {
    379     delete d_data;
    380 }
    381 
    382 /*!
    383    Set the color
    384 
    385    \param color Color
    386    \sa color()
    387 */
    388 void QwtAlphaColorMap::setColor( const QColor &color )
    389 {
    390     d_data->color = color;
    391     d_data->rgb = color.rgb();
     448    d_data->rgbMax = d_data->rgb | ( 255 << 24 );
    392449}
    393450
     
    407464
    408465  \param interval Range for all values
    409   \param value Value to map into a rgb value
    410   \return rgb value, with an alpha value
     466  \param value Value to map into a RGB value
     467  \return RGB value, with an alpha value
    411468*/
    412469QRgb QwtAlphaColorMap::rgb( const QwtInterval &interval, double value ) const
    413470{
     471    if ( qIsNaN(value) )
     472        return 0u;
     473
    414474    const double width = interval.width();
    415     if ( !qIsNaN(value) && width >= 0.0 )
    416     {
    417         const double ratio = ( value - interval.minValue() ) / width;
    418         int alpha = qRound( 255 * ratio );
    419         if ( alpha < 0 )
    420             alpha = 0;
    421         if ( alpha > 255 )
    422             alpha = 255;
    423 
    424         return d_data->rgb | ( alpha << 24 );
    425     }
    426     return d_data->rgb;
     475    if ( width <= 0.0 )
     476        return 0u;
     477
     478    if ( value <= interval.minValue() )
     479        return d_data->rgb;
     480
     481    if ( value >= interval.maxValue() )
     482        return d_data->rgbMax;
     483
     484    const double ratio = ( value - interval.minValue() ) / width;
     485    return d_data->rgb | ( qRound( 255 * ratio ) << 24 );
    427486}
    428487
Note: See TracChangeset for help on using the changeset viewer.