Qt 4.8
Classes | Public Functions | Protected Functions | Private Functions | Static Private Functions | Properties | Static Private Attributes | Friends | List of all members
QGLColormap Class Reference

The QGLColormap class is used for installing custom colormaps into a QGLWidget. More...

#include <qglcolormap.h>

Classes

struct  QGLColormapData
 

Public Functions

void detach ()
 Detaches this QGLColormap from the shared block. More...
 
QColor entryColor (int idx) const
 Returns the QRgb value in the colorcell with index idx. More...
 
QRgb entryRgb (int idx) const
 Returns the QRgb value in the colorcell with index idx. More...
 
int find (QRgb color) const
 Returns the index of the color color. More...
 
int findNearest (QRgb color) const
 Returns the index of the color that is the closest match to color color. More...
 
bool isEmpty () const
 Returns true if the colormap is empty or it is not in use by a QGLWidget; otherwise returns false. More...
 
QGLColormapoperator= (const QGLColormap &)
 Assign a shallow copy of map to this QGLColormap. More...
 
 QGLColormap ()
 Construct a QGLColormap. More...
 
 QGLColormap (const QGLColormap &)
 Construct a shallow copy of map. More...
 
void setEntries (int count, const QRgb *colors, int base=0)
 Set an array of cells in this colormap. More...
 
void setEntry (int idx, QRgb color)
 Set cell at index idx in the colormap to color color. More...
 
void setEntry (int idx, const QColor &color)
 Set the cell with index idx in the colormap to color color. More...
 
int size () const
 Returns the number of colorcells in the colormap. More...
 
 ~QGLColormap ()
 Dereferences the QGLColormap and deletes it if this was the last reference to it. More...
 

Protected Functions

Qt::HANDLE handle ()
 Returns the handle for this color map. More...
 
void setHandle (Qt::HANDLE ahandle)
 Sets the handle for this color map to handle. More...
 

Private Functions

void detach_helper ()
 

Static Private Functions

static void cleanup (QGLColormapData *x)
 

Properties

QGLColormapDatad
 

Static Private Attributes

static struct QGLColormapData shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0 }
 

Friends

class QGLWidget
 
class QGLWidgetPrivate
 

Detailed Description

The QGLColormap class is used for installing custom colormaps into a QGLWidget.

OpenGL

QGLColormap provides a platform independent way of specifying and installing indexed colormaps for a QGLWidget. QGLColormap is especially useful when using the OpenGL color-index mode.

Under X11 you must use an X server that supports either a PseudoColor or DirectColor visual class. If your X server currently only provides a GrayScale, TrueColor, StaticColor or StaticGray visual, you will not be able to allocate colorcells for writing. If this is the case, try setting your X server to 8 bit mode. It should then provide you with at least a PseudoColor visual. Note that you may experience colormap flashing if your X server is running in 8 bit mode.

The size() of the colormap is always set to 256 colors. Note that under Windows you can also install colormaps in child widgets.

This class uses implicit sharing as a memory and speed optimization.

Example of use:

#include <QApplication>
#include <QGLColormap>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MySuperGLWidget widget; // a QGLWidget in color-index mode
QGLColormap colormap;
// This will fill the colormap with colors ranging from
// black to white.
const int size = 256;
for (int i = 0; i < size; ++i)
colormap.setEntry(i, qRgb(i, i, i));
widget.setColormap(colormap);
widget.show();
return app.exec();
}
See also
QGLWidget::setColormap(), QGLWidget::colormap()

Definition at line 54 of file qglcolormap.h.

Constructors and Destructors

◆ QGLColormap() [1/2]

QGLColormap::QGLColormap ( )

Construct a QGLColormap.

Definition at line 111 of file qglcolormap.cpp.

112  : d(&shared_null)
113 {
114  d->ref.ref();
115 }
static struct QGLColormapData shared_null
Definition: qglcolormap.h:87
QGLColormapData * d
Definition: qglcolormap.h:86

◆ QGLColormap() [2/2]

QGLColormap::QGLColormap ( const QGLColormap map)

Construct a shallow copy of map.

Definition at line 121 of file qglcolormap.cpp.

122  : d(map.d)
123 {
124  d->ref.ref();
125 }
QGLColormapData * d
Definition: qglcolormap.h:86

◆ ~QGLColormap()

QGLColormap::~QGLColormap ( )

Dereferences the QGLColormap and deletes it if this was the last reference to it.

Definition at line 131 of file qglcolormap.cpp.

132 {
133  if (!d->ref.deref())
134  cleanup(d);
135 }
QGLColormapData * d
Definition: qglcolormap.h:86
static void cleanup(QGLColormapData *x)

Functions

◆ cleanup()

void QGLColormap::cleanup ( QGLColormap::QGLColormapData x)
staticprivate

Definition at line 137 of file qglcolormap.cpp.

Referenced by detach_helper(), operator=(), and ~QGLColormap().

138 {
139  delete x->cells;
140  x->cells = 0;
141  delete x;
142 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82

◆ detach()

void QGLColormap::detach ( )
inline

Detaches this QGLColormap from the shared block.

Warning
This function is not part of the public interface.

Definition at line 95 of file qglcolormap.h.

Referenced by setEntries(), and setEntry().

96 {
97  if (d->ref != 1)
98  detach_helper();
99 }
void detach_helper()
QGLColormapData * d
Definition: qglcolormap.h:86

◆ detach_helper()

void QGLColormap::detach_helper ( )
private

Definition at line 166 of file qglcolormap.cpp.

167 {
168  QGLColormapData *x = new QGLColormapData;
169  x->ref = 1;
170  x->cmapHandle = 0;
171  x->cells = 0;
172  if (d->cells) {
173  x->cells = new QVector<QRgb>(256);
174  *x->cells = *d->cells;
175  }
176  if (!d->ref.deref())
177  cleanup(d);
178  d = x;
179 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
QGLColormapData * d
Definition: qglcolormap.h:86
static void cleanup(QGLColormapData *x)

◆ entryColor()

QColor QGLColormap::entryColor ( int  idx) const

Returns the QRgb value in the colorcell with index idx.

Definition at line 237 of file qglcolormap.cpp.

238 {
239  if (d == &shared_null || !d->cells)
240  return QColor();
241  else
242  return QColor(d->cells->at(idx));
243 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QVector< QRgb > * cells
Definition: qglcolormap.h:82
static struct QGLColormapData shared_null
Definition: qglcolormap.h:87
QGLColormapData * d
Definition: qglcolormap.h:86
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350

◆ entryRgb()

QRgb QGLColormap::entryRgb ( int  idx) const

Returns the QRgb value in the colorcell with index idx.

Definition at line 213 of file qglcolormap.cpp.

Referenced by qStoreColors().

214 {
215  if (d == &shared_null || !d->cells)
216  return 0;
217  else
218  return d->cells->at(idx);
219 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
static struct QGLColormapData shared_null
Definition: qglcolormap.h:87
QGLColormapData * d
Definition: qglcolormap.h:86
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350

◆ find()

int QGLColormap::find ( QRgb  color) const

Returns the index of the color color.

If color is not in the map, -1 is returned.

Definition at line 276 of file qglcolormap.cpp.

Referenced by findNearest().

277 {
278  if (d->cells)
279  return d->cells->indexOf(color);
280  return -1;
281 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
QGLColormapData * d
Definition: qglcolormap.h:86
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698

◆ findNearest()

int QGLColormap::findNearest ( QRgb  color) const

Returns the index of the color that is the closest match to color color.

Definition at line 287 of file qglcolormap.cpp.

288 {
289  int idx = find(color);
290  if (idx >= 0)
291  return idx;
292  int mapSize = size();
293  int mindist = 200000;
294  int r = qRed(color);
295  int g = qGreen(color);
296  int b = qBlue(color);
297  int rx, gx, bx, dist;
298  for (int i = 0; i < mapSize; ++i) {
299  QRgb ci = d->cells->at(i);
300  rx = r - qRed(ci);
301  gx = g - qGreen(ci);
302  bx = b - qBlue(ci);
303  dist = rx * rx + gx * gx + bx * bx; // calculate distance
304  if (dist < mindist) { // minimal?
305  mindist = dist;
306  idx = i;
307  }
308  }
309  return idx;
310 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
unsigned int QRgb
Definition: qrgb.h:53
int size() const
Returns the number of colorcells in the colormap.
Q_GUI_EXPORT_INLINE int qRed(QRgb rgb)
Definition: qrgb.h:57
QGLColormapData * d
Definition: qglcolormap.h:86
Q_GUI_EXPORT_INLINE int qBlue(QRgb rgb)
Definition: qrgb.h:63
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
Q_GUI_EXPORT_INLINE int qGreen(QRgb rgb)
Definition: qrgb.h:60
int find(QRgb color) const
Returns the index of the color color.

◆ handle()

Qt::HANDLE QGLColormap::handle ( )
inlineprotected

Returns the handle for this color map.

Warning
This function is not part of the public interface.

Definition at line 76 of file qglcolormap.h.

76 { return d ? d->cmapHandle : 0; }
QGLColormapData * d
Definition: qglcolormap.h:86

◆ isEmpty()

bool QGLColormap::isEmpty ( ) const

Returns true if the colormap is empty or it is not in use by a QGLWidget; otherwise returns false.

A colormap with no color values set is considered to be empty. For historical reasons, a colormap that has color values set but which is not in use by a QGLWidget is also considered empty.

Compare size() with zero to determine if the colormap is empty regardless of whether it is in use by a QGLWidget or not.

See also
size()

Definition at line 258 of file qglcolormap.cpp.

259 {
260  return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
261 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
static struct QGLColormapData shared_null
Definition: qglcolormap.h:87
QGLColormapData * d
Definition: qglcolormap.h:86
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ operator=()

QGLColormap & QGLColormap::operator= ( const QGLColormap map)

Assign a shallow copy of map to this QGLColormap.

Definition at line 147 of file qglcolormap.cpp.

148 {
149  map.d->ref.ref();
150  if (!d->ref.deref())
151  cleanup(d);
152  d = map.d;
153  return *this;
154 }
QGLColormapData * d
Definition: qglcolormap.h:86
static void cleanup(QGLColormapData *x)

◆ setEntries()

void QGLColormap::setEntries ( int  count,
const QRgb colors,
int  base = 0 
)

Set an array of cells in this colormap.

count is the number of colors that should be set, colors is the array of colors, and base is the starting index. The first element in colors is set at base in the colormap.

Definition at line 198 of file qglcolormap.cpp.

Referenced by QGLContextPrivate::updateFormatVersion().

199 {
200  detach();
201  if (!d->cells)
202  d->cells = new QVector<QRgb>(256);
203 
204  Q_ASSERT_X(colors && base >= 0 && (base + count) <= d->cells->size(), "QGLColormap::setEntries",
205  "preconditions not met");
206  for (int i = 0; i < count; ++i)
207  setEntry(base + i, colors[i]);
208 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
void setEntry(int idx, QRgb color)
Set cell at index idx in the colormap to color color.
static const uint base
Definition: qurl.cpp:268
QGLColormapData * d
Definition: qglcolormap.h:86
void detach()
Detaches this QGLColormap from the shared block.
Definition: qglcolormap.h:95
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ setEntry() [1/2]

void QGLColormap::setEntry ( int  idx,
QRgb  color 
)

Set cell at index idx in the colormap to color color.

Definition at line 184 of file qglcolormap.cpp.

Referenced by setEntries(), and setEntry().

185 {
186  detach();
187  if (!d->cells)
188  d->cells = new QVector<QRgb>(256);
189  d->cells->replace(idx, color);
190 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qvector.h:382
QGLColormapData * d
Definition: qglcolormap.h:86
void detach()
Detaches this QGLColormap from the shared block.
Definition: qglcolormap.h:95

◆ setEntry() [2/2]

void QGLColormap::setEntry ( int  idx,
const QColor color 
)

Set the cell with index idx in the colormap to color color.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 229 of file qglcolormap.cpp.

230 {
231  setEntry(idx, color.rgb());
232 }
void setEntry(int idx, QRgb color)
Set cell at index idx in the colormap to color color.
QRgb rgb() const
Returns the RGB value of the color.
Definition: qcolor.cpp:1051

◆ setHandle()

void QGLColormap::setHandle ( Qt::HANDLE  handle)
inlineprotected

Sets the handle for this color map to handle.

Warning
This function is not part of the public interface.

Definition at line 77 of file qglcolormap.h.

77 { d->cmapHandle = ahandle; }
QGLColormapData * d
Definition: qglcolormap.h:86

◆ size()

int QGLColormap::size ( ) const

Returns the number of colorcells in the colormap.

Definition at line 267 of file qglcolormap.cpp.

Referenced by findNearest(), qStoreColors(), and QGLWidgetPrivate::updateColormap().

268 {
269  return d->cells ? d->cells->size() : 0;
270 }
QVector< QRgb > * cells
Definition: qglcolormap.h:82
QGLColormapData * d
Definition: qglcolormap.h:86
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

Friends and Related Functions

◆ QGLWidget

friend class QGLWidget
friend

Definition at line 91 of file qglcolormap.h.

◆ QGLWidgetPrivate

friend class QGLWidgetPrivate
friend

Definition at line 92 of file qglcolormap.h.

Properties

◆ d

QGLColormapData* QGLColormap::d
private

◆ shared_null

QGLColormap::QGLColormapData QGLColormap::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0 }
staticprivate

Definition at line 87 of file qglcolormap.h.

Referenced by entryColor(), entryRgb(), and isEmpty().


The documentation for this class was generated from the following files: