1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 \brief The QGLColormap class is used for installing custom colormaps into
51 QGLColormap provides a platform independent way of specifying and
52 installing indexed colormaps for a QGLWidget. QGLColormap is
53 especially useful when using the OpenGL color-index mode.
55 Under X11 you must use an X server that supports either a \c
56 PseudoColor or \c DirectColor visual class. If your X server
57 currently only provides a \c GrayScale, \c TrueColor, \c
58 StaticColor or \c StaticGray visual, you will not be able to
59 allocate colorcells for writing. If this is the case, try setting
60 your X server to 8 bit mode. It should then provide you with at
61 least a \c PseudoColor visual. Note that you may experience
62 colormap flashing if your X server is running in 8 bit mode.
64 The size() of the colormap is always set to 256
65 colors. Note that under Windows you can also install colormaps
68 This class uses \l{implicit sharing} as a memory and speed
72 \snippet doc/src/snippets/code/src_opengl_qglcolormap.cpp 0
74 \sa QGLWidget::setColormap(), QGLWidget::colormap()
78 \fn Qt::HANDLE QGLColormap::handle()
82 Returns the handle for this color map.
86 \fn void QGLColormap::setHandle(Qt::HANDLE handle)
90 Sets the handle for this color map to \a handle.
93 #include "qglcolormap.h"
97 QGLColormap::QGLColormapData
QGLColormap::shared_null
= { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0 };
100 Construct a QGLColormap.
102 QGLColormap::QGLColormap()
110 Construct a shallow copy of \a map.
112 QGLColormap::QGLColormap(const QGLColormap
&map
)
119 Dereferences the QGLColormap and deletes it if this was the last
122 QGLColormap::~QGLColormap()
128 void QGLColormap::cleanup(QGLColormap::QGLColormapData
*x
)
136 Assign a shallow copy of \a map to this QGLColormap.
138 QGLColormap
& QGLColormap::operator=(const QGLColormap
&map
)
148 \fn void QGLColormap::detach()
151 Detaches this QGLColormap from the shared block.
154 void QGLColormap::detach_helper()
156 QGLColormapData
*x
= new QGLColormapData
;
161 x
->cells
= new QVector
<QRgb
>(256);
162 *x
->cells
= *d
->cells
;
170 Set cell at index \a idx in the colormap to color \a color.
172 void QGLColormap::setEntry(int idx
, QRgb color
)
176 d
->cells
= new QVector
<QRgb
>(256);
177 d
->cells
->replace(idx
, color
);
181 Set an array of cells in this colormap. \a count is the number of
182 colors that should be set, \a colors is the array of colors, and
183 \a base is the starting index. The first element in \a colors
184 is set at \a base in the colormap.
186 void QGLColormap::setEntries(int count
, const QRgb
*colors
, int base
)
190 d
->cells
= new QVector
<QRgb
>(256);
192 Q_ASSERT_X(colors
&& base
>= 0 && (base
+ count
) <= d
->cells
->size(), "QGLColormap::setEntries",
193 "preconditions not met");
194 for (int i
= 0; i
< count
; ++i
)
195 setEntry(base
+ i
, colors
[i
]);
199 Returns the QRgb value in the colorcell with index \a idx.
201 QRgb
QGLColormap::entryRgb(int idx
) const
203 if (d
== &shared_null
|| !d
->cells
)
206 return d
->cells
->at(idx
);
212 Set the cell with index \a idx in the colormap to color \a color.
214 void QGLColormap::setEntry(int idx
, const QColor
&color
)
216 setEntry(idx
, color
.rgb());
220 Returns the QRgb value in the colorcell with index \a idx.
222 QColor
QGLColormap::entryColor(int idx
) const
224 if (d
== &shared_null
|| !d
->cells
)
227 return QColor(d
->cells
->at(idx
));
231 Returns true if the colormap is empty or it is not in use
232 by a QGLWidget; otherwise returns false.
234 A colormap with no color values set is considered to be empty.
235 For historical reasons, a colormap that has color values set
236 but which is not in use by a QGLWidget is also considered empty.
238 Compare size() with zero to determine if the colormap is empty
239 regardless of whether it is in use by a QGLWidget or not.
243 bool QGLColormap::isEmpty() const
245 return d
== &shared_null
|| d
->cells
== 0 || d
->cells
->size() == 0 || d
->cmapHandle
== 0;
250 Returns the number of colorcells in the colormap.
252 int QGLColormap::size() const
254 return d
->cells
? d
->cells
->size() : 0;
258 Returns the index of the color \a color. If \a color is not in the
261 int QGLColormap::find(QRgb color
) const
264 return d
->cells
->indexOf(color
);
269 Returns the index of the color that is the closest match to color
272 int QGLColormap::findNearest(QRgb color
) const
274 int idx
= find(color
);
277 int mapSize
= size();
278 int mindist
= 200000;
280 int g
= qGreen(color
);
281 int b
= qBlue(color
);
282 int rx
, gx
, bx
, dist
;
283 for (int i
= 0; i
< mapSize
; ++i
) {
284 QRgb ci
= d
->cells
->at(i
);
288 dist
= rx
* rx
+ gx
* gx
+ bx
* bx
; // calculate distance
289 if (dist
< mindist
) { // minimal?