qwt_color_map.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_COLOR_MAP_H
  10. #define QWT_COLOR_MAP_H
  11. #include "qwt_global.h"
  12. #include "qwt_interval.h"
  13. #include <qcolor.h>
  14. #include <qvector.h>
  15. /*!
  16. \brief QwtColorMap is used to map values into colors.
  17. For displaying 3D data on a 2D plane the 3rd dimension is often
  18. displayed using colors, like f.e in a spectrogram.
  19. Each color map is optimized to return colors for only one of the
  20. following image formats:
  21. - QImage::Format_Indexed8\n
  22. - QImage::Format_ARGB32\n
  23. \sa QwtPlotSpectrogram, QwtScaleWidget
  24. */
  25. class QWT_EXPORT QwtColorMap
  26. {
  27. public:
  28. /*!
  29. - RGB\n
  30. The map is intended to map into QRgb values.
  31. - Indexed\n
  32. The map is intended to map into 8 bit values, that
  33. are indices into the color table.
  34. \sa rgb(), colorIndex(), colorTable()
  35. */
  36. enum Format
  37. {
  38. RGB,
  39. Indexed
  40. };
  41. QwtColorMap( Format = QwtColorMap::RGB );
  42. virtual ~QwtColorMap();
  43. Format format() const;
  44. /*!
  45. Map a value of a given interval into a rgb value.
  46. \param interval Range for the values
  47. \param value Value
  48. \return rgb value, corresponding to value
  49. */
  50. virtual QRgb rgb( const QwtInterval &interval,
  51. double value ) const = 0;
  52. /*!
  53. Map a value of a given interval into a color index
  54. \param interval Range for the values
  55. \param value Value
  56. \return color index, corresponding to value
  57. */
  58. virtual unsigned char colorIndex(
  59. const QwtInterval &interval, double value ) const = 0;
  60. QColor color( const QwtInterval &, double value ) const;
  61. virtual QVector<QRgb> colorTable( const QwtInterval & ) const;
  62. private:
  63. Format d_format;
  64. };
  65. /*!
  66. \brief QwtLinearColorMap builds a color map from color stops.
  67. A color stop is a color at a specific position. The valid
  68. range for the positions is [0.0, 1.0]. When mapping a value
  69. into a color it is translated into this interval. If
  70. mode() == FixedColors the color is calculated from the next lower
  71. color stop. If mode() == ScaledColors the color is calculated
  72. by interpolating the colors of the adjacent stops.
  73. */
  74. class QWT_EXPORT QwtLinearColorMap: public QwtColorMap
  75. {
  76. public:
  77. /*!
  78. Mode of color map
  79. \sa setMode(), mode()
  80. */
  81. enum Mode
  82. {
  83. FixedColors,
  84. ScaledColors
  85. };
  86. QwtLinearColorMap( QwtColorMap::Format = QwtColorMap::RGB );
  87. QwtLinearColorMap( const QColor &from, const QColor &to,
  88. QwtColorMap::Format = QwtColorMap::RGB );
  89. virtual ~QwtLinearColorMap();
  90. void setMode( Mode );
  91. Mode mode() const;
  92. void setColorInterval( const QColor &color1, const QColor &color2 );
  93. void addColorStop( double value, const QColor& );
  94. QVector<double> colorStops() const;
  95. QColor color1() const;
  96. QColor color2() const;
  97. virtual QRgb rgb( const QwtInterval &, double value ) const;
  98. virtual unsigned char colorIndex(
  99. const QwtInterval &, double value ) const;
  100. class ColorStops;
  101. private:
  102. // Disabled copy constructor and operator=
  103. QwtLinearColorMap( const QwtLinearColorMap & );
  104. QwtLinearColorMap &operator=( const QwtLinearColorMap & );
  105. class PrivateData;
  106. PrivateData *d_data;
  107. };
  108. /*!
  109. \brief QwtAlphaColorMap variies the alpha value of a color
  110. */
  111. class QWT_EXPORT QwtAlphaColorMap: public QwtColorMap
  112. {
  113. public:
  114. QwtAlphaColorMap( const QColor & = QColor( Qt::gray ) );
  115. virtual ~QwtAlphaColorMap();
  116. void setColor( const QColor & );
  117. QColor color() const;
  118. virtual QRgb rgb( const QwtInterval &, double value ) const;
  119. private:
  120. QwtAlphaColorMap( const QwtAlphaColorMap & );
  121. QwtAlphaColorMap &operator=( const QwtAlphaColorMap & );
  122. virtual unsigned char colorIndex(
  123. const QwtInterval &, double value ) const;
  124. class PrivateData;
  125. PrivateData *d_data;
  126. };
  127. /*!
  128. Map a value into a color
  129. \param interval Valid interval for values
  130. \param value Value
  131. \return Color corresponding to value
  132. \warning This method is slow for Indexed color maps. If it is
  133. necessary to map many values, its better to get the
  134. color table once and find the color using colorIndex().
  135. */
  136. inline QColor QwtColorMap::color(
  137. const QwtInterval &interval, double value ) const
  138. {
  139. if ( d_format == RGB )
  140. {
  141. return QColor( rgb( interval, value ) );
  142. }
  143. else
  144. {
  145. const unsigned int index = colorIndex( interval, value );
  146. return colorTable( interval )[index]; // slow
  147. }
  148. }
  149. /*!
  150. \return Intended format of the color map
  151. \sa Format
  152. */
  153. inline QwtColorMap::Format QwtColorMap::format() const
  154. {
  155. return d_format;
  156. }
  157. #endif