1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the opengl module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
\brief The QGLColormap class is used for installing custom colormaps into
37
QGLColormap provides a platform independent way of specifying and
38
installing indexed colormaps into QGLWidgets. QGLColormap is
39
especially useful when using the \link opengl.html OpenGL\endlink
42
Under X11 you must use an X server that supports either a \c
43
PseudoColor or \c DirectColor visual class. If your X server
44
currently only provides a \c GrayScale, \c TrueColor, \c
45
StaticColor or \c StaticGray visual, you will not be able to
46
allocate colorcells for writing. If this is the case, try setting
47
your X server to 8 bit mode. It should then provide you with at
48
least a \c PseudoColor visual. Note that you may experience
49
colormap flashing if your X server is running in 8 bit mode.
51
Under Windows the size of the colormap is always set to 256
52
colors. Note that under Windows you can also install colormaps
55
This class uses explicit sharing (see \link shclass.html Shared
60
#include <qapplication.h>
61
#include <qglcolormap.h>
65
QApplication a(argc, argv);
67
MySuperGLWidget widget(0); // A QGLWidget in color-index mode
70
// This will fill the colormap with colors ranging from
72
for (int i = 0; i < colormap.size(); i++)
73
colormap.setEntry(i, qRgb(i, i, i));
75
widget.setColormap(colormap);
81
\sa QGLWidget::setColormap(), QGLWidget::colormap()
85
\fn Qt::HANDLE QGLColormap::handle()
89
Returns the handle for this color map.
93
\fn void QGLColormap::setHandle(Qt::HANDLE handle)
97
Sets the handle for this color map to \a handle.
100
#include "qglcolormap.h"
102
QGLColormap::QGLColormapData QGLColormap::shared_null = { Q_ATOMIC_INIT(1), 0, 0 };
105
Construct a QGLColormap.
107
QGLColormap::QGLColormap()
115
Construct a shallow copy of \a map.
117
QGLColormap::QGLColormap(const QGLColormap &map)
124
Dereferences the QGLColormap and deletes it if this was the last
127
QGLColormap::~QGLColormap()
133
void QGLColormap::cleanup(QGLColormap::QGLColormapData *x)
141
Assign a shallow copy of \a map to this QGLColormap.
143
QGLColormap & QGLColormap::operator=(const QGLColormap &map)
145
QGLColormapData *x = map.d;
147
x = qAtomicSetPtr(&d, x);
154
\fn void QGLColormap::detach()
155
Detaches this QGLColormap from the shared block.
158
void QGLColormap::detach_helper()
160
QGLColormapData *x = new QGLColormapData;
165
x->cells = new QVector<QRgb>(256);
166
*x->cells = *d->cells;
168
x = qAtomicSetPtr(&d, x);
174
Set cell at index \a idx in the colormap to color \a color.
176
void QGLColormap::setEntry(int idx, QRgb color)
180
d->cells = new QVector<QRgb>(256);
181
d->cells->insert(idx, color);
185
Set an array of cells in this colormap. \a count is the number of
186
colors that should be set, \a colors is the array of colors, and
187
\a base is the starting index.
189
void QGLColormap::setEntries(int count, const QRgb *colors, int base)
193
d->cells = new QVector<QRgb>(256);
195
Q_ASSERT_X(!colors || base >= 0 || base + count < d->cells->size(), "QGLColormap::setEntries",
196
"preconditions not met");
197
for (int i = base; i < base + count; ++i)
198
setEntry(i, colors[i]);
202
Returns the QRgb value in the colorcell with index \a idx.
204
QRgb QGLColormap::entryRgb(int idx) const
206
if (d == &shared_null || !d->cells)
209
return d->cells->at(idx);
215
Set the cell with index \a idx in the colormap to color \a color.
217
void QGLColormap::setEntry(int idx, const QColor &color)
219
setEntry(idx, color.rgb());
223
Returns the QRgb value in the colorcell with index \a idx.
225
QColor QGLColormap::entryColor(int idx) const
227
if (d == &shared_null || !d->cells)
230
return QColor(d->cells->at(idx));
234
Returns true if the colormap is empty; otherwise returns false. A
235
colormap with no color values set is considered to be empty.
237
bool QGLColormap::isEmpty() const
239
return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
244
Returns the number of colorcells in the colormap.
246
int QGLColormap::size() const
248
return d->cells ? d->cells->size() : 0;
252
Returns the index of the color \a color. If \a color is not in the
255
int QGLColormap::find(QRgb color) const
258
return d->cells->indexOf(color);
263
Returns the index of the color that is the closest match to color
266
int QGLColormap::findNearest(QRgb color) const
268
int idx = find(color);
271
int mapSize = size();
272
int mindist = 200000;
274
int g = qGreen(color);
275
int b = qBlue(color);
276
int rx, gx, bx, dist;
277
for (int i = 0; i < mapSize; ++i) {
278
QRgb ci = d->cells->at(i);
282
dist = rx * rx + gx * gx + bx * bx; // calculate distance
283
if (dist < mindist) { // minimal?