Changeset 8127 in ntrip for trunk/BNC/qwt/qwt_color_map.cpp
- Timestamp:
- May 10, 2017, 3:20:54 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_color_map.cpp
r4271 r8127 16 16 { 17 17 public: 18 ColorStops() 19 { 20 _stops.reserve( 256 ); 18 ColorStops(): 19 d_doAlpha( false ) 20 { 21 d_stops.reserve( 256 ); 21 22 } 22 23 … … 39 40 ColorStop( double p, const QColor &c ): 40 41 pos( p ), 41 rgb( c.rgb () )42 rgb( c.rgba() ) 42 43 { 43 44 r = qRed( rgb ); 44 45 g = qGreen( rgb ); 45 46 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; 46 71 } 47 72 48 73 double pos; 49 74 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; 51 81 }; 52 82 53 83 inline int findUpper( double pos ) const; 54 QVector<ColorStop> _stops; 84 QVector<ColorStop> d_stops; 85 bool d_doAlpha; 55 86 }; 56 87 … … 64 95 65 96 int index; 66 if ( _stops.size() == 0 )97 if ( d_stops.size() == 0 ) 67 98 { 68 99 index = 0; 69 _stops.resize( 1 );100 d_stops.resize( 1 ); 70 101 } 71 102 else 72 103 { 73 104 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] ); 84 123 } 85 124 86 125 inline QVector<double> QwtLinearColorMap::ColorStops::stops() const 87 126 { 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; 91 130 return positions; 92 131 } … … 95 134 { 96 135 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(); 100 139 101 140 while ( n > 0 ) … … 120 159 { 121 160 if ( pos <= 0.0 ) 122 return _stops[0].rgb;161 return d_stops[0].rgb; 123 162 if ( pos >= 1.0 ) 124 return _stops[_stops.size() - 1 ].rgb;163 return d_stops[ d_stops.size() - 1 ].rgb; 125 164 126 165 const int index = findUpper( pos ); 127 166 if ( mode == FixedColors ) 128 167 { 129 return _stops[index-1].rgb;168 return d_stops[index-1].rgb; 130 169 } 131 170 else 132 171 { 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 } 143 196 } 144 197 } … … 205 258 \param color1 Color used for the minimum value of the value interval 206 259 \param color2 Color used for the maximum value of the value interval 207 \param format Preferred format of the coor map260 \param format Preferred format for the color map 208 261 */ 209 262 QwtLinearColorMap::QwtLinearColorMap( const QColor &color1, … … 280 333 281 334 /*! 282 Return all positions of color stops in increasing order335 \return Positions of color stops in increasing order 283 336 */ 284 337 QVector<double> QwtLinearColorMap::colorStops() const … … 306 359 307 360 /*! 308 Map a value of a given interval into a rgbvalue361 Map a value of a given interval into a RGB value 309 362 310 363 \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 312 367 */ 313 368 QRgb QwtLinearColorMap::rgb( … … 315 370 { 316 371 if ( qIsNaN(value) ) 317 return qRgba(0, 0, 0, 0);372 return 0u; 318 373 319 374 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; 325 379 return d_data->colorStops.rgb( d_data->mode, ratio ); 326 380 } 327 381 328 382 /*! 329 Map a value of a given interval into a color index, between 0 and 255383 \brief Map a value of a given interval into a color index 330 384 331 385 \param interval Range for all values 332 386 \param value Value to map into a color index 387 388 \return Index, between 0 and 255 333 389 */ 334 390 unsigned char QwtLinearColorMap::colorIndex( … … 341 397 342 398 if ( value >= interval.maxValue() ) 343 return ( unsigned char )255;399 return 255; 344 400 345 401 const double ratio = ( value - interval.minValue() ) / width; … … 347 403 unsigned char index; 348 404 if ( d_data->mode == FixedColors ) 349 index = ( unsigned char )( ratio * 255 ); // always floor405 index = static_cast<unsigned char>( ratio * 255 ); // always floor 350 406 else 351 index = ( unsigned char )qRound( ratio * 255 );407 index = static_cast<unsigned char>( ratio * 255 + 0.5 ); 352 408 353 409 return index; … … 359 415 QColor color; 360 416 QRgb rgb; 417 QRgb rgbMax; 361 418 }; 362 419 … … 370 427 { 371 428 d_data = new PrivateData; 429 setColor( color ); 430 } 431 432 //! Destructor 433 QwtAlphaColorMap::~QwtAlphaColorMap() 434 { 435 delete d_data; 436 } 437 438 /*! 439 Set the color 440 441 \param color Color 442 \sa color() 443 */ 444 void QwtAlphaColorMap::setColor( const QColor &color ) 445 { 372 446 d_data->color = color; 373 447 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 ); 392 449 } 393 450 … … 407 464 408 465 \param interval Range for all values 409 \param value Value to map into a rgbvalue410 \return rgbvalue, with an alpha value466 \param value Value to map into a RGB value 467 \return RGB value, with an alpha value 411 468 */ 412 469 QRgb QwtAlphaColorMap::rgb( const QwtInterval &interval, double value ) const 413 470 { 471 if ( qIsNaN(value) ) 472 return 0u; 473 414 474 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 ); 427 486 } 428 487
Note:
See TracChangeset
for help on using the changeset viewer.