qwt_color_map.cpp 11.2 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_array.h"
#include "qwt_math.h"
#include "qwt_double_interval.h"
#include "qwt_color_map.h"

#if QT_VERSION < 0x040000
#include <qvaluelist.h>
typedef QValueVector<QRgb> QwtColorTable;
#else
typedef QVector<QRgb> QwtColorTable;
#endif

class QwtLinearColorMap::ColorStops
{
public:
25
    ColorStops() {
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#if QT_VERSION >= 0x040000
        _stops.reserve(256);
#endif
    }

    void insert(double pos, const QColor &color);
    QRgb rgb(QwtLinearColorMap::Mode, double pos) const;

    QwtArray<double> stops() const;

private:

    class ColorStop
    {
    public:
        ColorStop():
            pos(0.0),
43
            rgb(0) {
pixhawk's avatar
pixhawk committed
44 45 46 47
        };

        ColorStop(double p, const QColor &c):
            pos(p),
48
            rgb(c.rgb()) {
pixhawk's avatar
pixhawk committed
49 50 51 52 53 54 55 56 57 58
            r = qRed(rgb);
            g = qGreen(rgb);
            b = qBlue(rgb);
        }

        double pos;
        QRgb rgb;
        int r, g, b;
    };

59
    inline int findUpper(double pos) const;
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66 67 68 69 70 71
    QwtArray<ColorStop> _stops;
};

void QwtLinearColorMap::ColorStops::insert(double pos, const QColor &color)
{
    // Lookups need to be very fast, insertions are not so important.
    // Anyway, a balanced tree is what we need here. TODO ...

    if ( pos < 0.0 || pos > 1.0 )
        return;

    int index;
72
    if ( _stops.size() == 0 ) {
pixhawk's avatar
pixhawk committed
73 74 75 76 77 78
        index = 0;
#if QT_VERSION < 0x040000
        _stops.resize(1, QGArray::SpeedOptim);
#else
        _stops.resize(1);
#endif
79
    } else {
pixhawk's avatar
pixhawk committed
80
        index = findUpper(pos);
81 82
        if ( index == (int)_stops.size() ||
                qwtAbs(_stops[index].pos - pos) >= 0.001 ) {
pixhawk's avatar
pixhawk committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#if QT_VERSION < 0x040000
            _stops.resize(_stops.size() + 1, QGArray::SpeedOptim);
#else
            _stops.resize(_stops.size() + 1);
#endif
            for ( int i = _stops.size() - 1; i > index; i-- )
                _stops[i] = _stops[i-1];
        }
    }

    _stops[index] = ColorStop(pos, color);
}

inline QwtArray<double> QwtLinearColorMap::ColorStops::stops() const
{
    QwtArray<double> positions(_stops.size());
    for ( int i = 0; i < (int)_stops.size(); i++ )
        positions[i] = _stops[i].pos;
    return positions;
}

inline int QwtLinearColorMap::ColorStops::findUpper(double pos) const
{
    int index = 0;
    int n = _stops.size();

    const ColorStop *stops = _stops.data();
110 111

    while (n > 0) {
pixhawk's avatar
pixhawk committed
112 113 114
        const int half = n >> 1;
        const int middle = index + half;

115
        if ( stops[middle].pos <= pos ) {
pixhawk's avatar
pixhawk committed
116 117
            index = middle + 1;
            n -= half + 1;
118
        } else
pixhawk's avatar
pixhawk committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            n = half;
    }

    return index;
}

inline QRgb QwtLinearColorMap::ColorStops::rgb(
    QwtLinearColorMap::Mode mode, double pos) const
{
    if ( pos <= 0.0 )
        return _stops[0].rgb;
    if ( pos >= 1.0 )
        return _stops[(int)(_stops.size() - 1)].rgb;

    const int index = findUpper(pos);
134
    if ( mode == FixedColors ) {
pixhawk's avatar
pixhawk committed
135
        return _stops[index-1].rgb;
136
    } else {
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144
        const ColorStop &s1 = _stops[index-1];
        const ColorStop &s2 = _stops[index];

        const double ratio = (pos - s1.pos) / (s2.pos - s1.pos);

        const int r = s1.r + qRound(ratio * (s2.r - s1.r));
        const int g = s1.g + qRound(ratio * (s2.g - s1.g));
        const int b = s1.b + qRound(ratio * (s2.b - s1.b));
145

pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        return qRgb(r, g, b);
    }
}

//! Constructor
QwtColorMap::QwtColorMap(Format format):
    d_format(format)
{
}

//! Destructor
QwtColorMap::~QwtColorMap()
{
}

/*!
   Build and return a color map of 256 colors

   The color table is needed for rendering indexed images in combination
165
   with using colorIndex().
pixhawk's avatar
pixhawk committed
166 167 168 169 170 171 172 173 174

   \param interval Range for the values
   \return A color table, that can be used for a QImage
*/
QwtColorTable QwtColorMap::colorTable(
    const QwtDoubleInterval &interval) const
{
    QwtColorTable table(256);

175
    if ( interval.isValid() ) {
pixhawk's avatar
pixhawk committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        const double step = interval.width() / (table.size() - 1);
        for ( int i = 0; i < (int) table.size(); i++ )
            table[i] = rgb(interval, interval.minValue() + step * i);
    }

    return table;
}

class QwtLinearColorMap::PrivateData
{
public:
    ColorStops colorStops;
    QwtLinearColorMap::Mode mode;
};

191
/*!
pixhawk's avatar
pixhawk committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
   Build a color map with two stops at 0.0 and 1.0. The color
   at 0.0 is Qt::blue, at 1.0 it is Qt::yellow.

   \param format Preferred format of the color map
*/
QwtLinearColorMap::QwtLinearColorMap(QwtColorMap::Format format):
    QwtColorMap(format)
{
    d_data = new PrivateData;
    d_data->mode = ScaledColors;

    setColorInterval( Qt::blue, Qt::yellow);
}

//! Copy constructor
QwtLinearColorMap::QwtLinearColorMap(const QwtLinearColorMap &other):
    QwtColorMap(other)
{
    d_data = new PrivateData;
    *this = other;
}

/*!
215
   Build a color map with two stops at 0.0 and 1.0.
pixhawk's avatar
pixhawk committed
216 217 218 219 220

   \param color1 Color used for the minimum value of the value interval
   \param color2 Color used for the maximum value of the value interval
   \param format Preferred format of the coor map
*/
221 222
QwtLinearColorMap::QwtLinearColorMap(const QColor &color1,
                                     const QColor &color2, QwtColorMap::Format format):
pixhawk's avatar
pixhawk committed
223 224 225 226
    QwtColorMap(format)
{
    d_data = new PrivateData;
    d_data->mode = ScaledColors;
227
    setColorInterval(color1, color2);
pixhawk's avatar
pixhawk committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

//! Destructor
QwtLinearColorMap::~QwtLinearColorMap()
{
    delete d_data;
}

//! Assignment operator
QwtLinearColorMap &QwtLinearColorMap::operator=(
    const QwtLinearColorMap &other)
{
    QwtColorMap::operator=(other);
    *d_data = *other.d_data;
    return *this;
}

//! Clone the color map
QwtColorMap *QwtLinearColorMap::copy() const
{
    QwtLinearColorMap* map = new QwtLinearColorMap();
    *map = *this;

    return map;
}

/*!
   \brief Set the mode of the color map

   FixedColors means the color is calculated from the next lower
   color stop. ScaledColors means the color is calculated
259
   by interpolating the colors of the adjacent stops.
pixhawk's avatar
pixhawk committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

   \sa mode()
*/
void QwtLinearColorMap::setMode(Mode mode)
{
    d_data->mode = mode;
}

/*!
   \return Mode of the color map
   \sa setMode()
*/
QwtLinearColorMap::Mode QwtLinearColorMap::mode() const
{
    return d_data->mode;
}

/*!
278
   Set the color range
pixhawk's avatar
pixhawk committed
279

280
   Add stops at 0.0 and 1.0.
pixhawk's avatar
pixhawk committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

   \param color1 Color used for the minimum value of the value interval
   \param color2 Color used for the maximum value of the value interval

   \sa color1(), color2()
*/
void QwtLinearColorMap::setColorInterval(
    const QColor &color1, const QColor &color2)
{
    d_data->colorStops = ColorStops();
    d_data->colorStops.insert(0.0, color1);
    d_data->colorStops.insert(1.0, color2);
}

/*!
   Add a color stop

298
   The value has to be in the range [0.0, 1.0].
pixhawk's avatar
pixhawk committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
   F.e. a stop at position 17.0 for a range [10.0,20.0] must be
   passed as: (17.0 - 10.0) / (20.0 - 10.0)

   \param value Value between [0.0, 1.0]
   \param color Color stop
*/
void QwtLinearColorMap::addColorStop(double value, const QColor& color)
{
    if ( value >= 0.0 && value <= 1.0 )
        d_data->colorStops.insert(value, color);
}

/*!
   Return all positions of color stops in increasing order
*/
QwtArray<double> QwtLinearColorMap::colorStops() const
{
    return d_data->colorStops.stops();
}

319
/*!
pixhawk's avatar
pixhawk committed
320 321 322 323 324 325 326 327
  \return the first color of the color range
  \sa setColorInterval()
*/
QColor QwtLinearColorMap::color1() const
{
    return QColor(d_data->colorStops.rgb(d_data->mode, 0.0));
}

328
/*!
pixhawk's avatar
pixhawk committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  \return the second color of the color range
  \sa setColorInterval()
*/
QColor QwtLinearColorMap::color2() const
{
    return QColor(d_data->colorStops.rgb(d_data->mode, 1.0));
}

/*!
  Map a value of a given interval into a rgb value

  \param interval Range for all values
  \param value Value to map into a rgb value
*/
QRgb QwtLinearColorMap::rgb(
    const QwtDoubleInterval &interval, double value) const
{
    const double width = interval.width();

    double ratio = 0.0;
    if ( width > 0.0 )
        ratio = (value - interval.minValue()) / width;

    return d_data->colorStops.rgb(d_data->mode, ratio);
}

/*!
  Map a value of a given interval into a color index, between 0 and 255

  \param interval Range for all values
  \param value Value to map into a color index
*/
unsigned char QwtLinearColorMap::colorIndex(
    const QwtDoubleInterval &interval, double value) const
{
    const double width = interval.width();

    if ( width <= 0.0 || value <= interval.minValue() )
        return 0;

    if ( value >= interval.maxValue() )
        return (unsigned char)255;

    const double ratio = (value - interval.minValue()) / width;
373

pixhawk's avatar
pixhawk committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    unsigned char index;
    if ( d_data->mode == FixedColors )
        index = (unsigned char)(ratio * 255); // always floor
    else
        index = (unsigned char)qRound(ratio * 255);

    return index;
}

class QwtAlphaColorMap::PrivateData
{
public:
    QColor color;
    QRgb rgb;
};


391
/*!
pixhawk's avatar
pixhawk committed
392 393 394 395 396 397 398 399 400 401 402
   Constructor
   \param color Color of the map
*/
QwtAlphaColorMap::QwtAlphaColorMap(const QColor &color):
    QwtColorMap(QwtColorMap::RGB)
{
    d_data = new PrivateData;
    d_data->color = color;
    d_data->rgb = color.rgb() & qRgba(255, 255, 255, 0);
}

403
/*!
pixhawk's avatar
pixhawk committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
   Copy constructor
   \param other Other color map
*/
QwtAlphaColorMap::QwtAlphaColorMap(const QwtAlphaColorMap &other):
    QwtColorMap(other)
{
    d_data = new PrivateData;
    *this = other;
}

//! Destructor
QwtAlphaColorMap::~QwtAlphaColorMap()
{
    delete d_data;
}

420
/*!
pixhawk's avatar
pixhawk committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
   Assignment operator
   \param other Other color map
   \return *this
*/
QwtAlphaColorMap &QwtAlphaColorMap::operator=(
    const QwtAlphaColorMap &other)
{
    QwtColorMap::operator=(other);
    *d_data = *other.d_data;
    return *this;
}

//! Clone the color map
QwtColorMap *QwtAlphaColorMap::copy() const
{
    QwtAlphaColorMap* map = new QwtAlphaColorMap();
    *map = *this;

    return map;
}

/*!
443
   Set the color
pixhawk's avatar
pixhawk committed
444 445 446 447 448 449 450 451 452 453

   \param color Color
   \sa color()
*/
void QwtAlphaColorMap::setColor(const QColor &color)
{
    d_data->color = color;
    d_data->rgb = color.rgb();
}

454 455
/*!
  \return the color
pixhawk's avatar
pixhawk committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  \sa setColor()
*/
QColor QwtAlphaColorMap::color() const
{
    return d_data->color;
}

/*!
  \brief Map a value of a given interval into a alpha value

  alpha := (value - interval.minValue()) / interval.width();

  \param interval Range for all values
  \param value Value to map into a rgb value
  \return rgb value, with an alpha value
*/
QRgb QwtAlphaColorMap::rgb(const QwtDoubleInterval &interval,
473
                           double value) const
pixhawk's avatar
pixhawk committed
474 475
{
    const double width = interval.width();
476
    if ( width >= 0.0 ) {
pixhawk's avatar
pixhawk committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490
        const double ratio = (value - interval.minValue()) / width;
        int alpha = qRound(255 * ratio);
        if ( alpha < 0 )
            alpha = 0;
        if ( alpha > 255 )
            alpha = 255;

        return d_data->rgb | (alpha << 24);
    }
    return d_data->rgb;
}

/*!
  Dummy function, needed to be implemented as it is pure virtual
491
  in QwtColorMap. Color indices make no sense in combination with
pixhawk's avatar
pixhawk committed
492 493 494 495 496 497 498 499 500
  an alpha channel.

  \return Always 0
*/
unsigned char QwtAlphaColorMap::colorIndex(
    const QwtDoubleInterval &, double) const
{
    return 0;
}