Qt 4.8
qglcolormap.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
102 #include "qglcolormap.h"
103 
105 
107 
112  : d(&shared_null)
113 {
114  d->ref.ref();
115 }
116 
117 
122  : d(map.d)
123 {
124  d->ref.ref();
125 }
126 
132 {
133  if (!d->ref.deref())
134  cleanup(d);
135 }
136 
138 {
139  delete x->cells;
140  x->cells = 0;
141  delete x;
142 }
143 
148 {
149  map.d->ref.ref();
150  if (!d->ref.deref())
151  cleanup(d);
152  d = map.d;
153  return *this;
154 }
155 
167 {
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 }
180 
184 void QGLColormap::setEntry(int idx, QRgb color)
185 {
186  detach();
187  if (!d->cells)
188  d->cells = new QVector<QRgb>(256);
189  d->cells->replace(idx, color);
190 }
191 
198 void QGLColormap::setEntries(int count, const QRgb *colors, int base)
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 }
209 
214 {
215  if (d == &shared_null || !d->cells)
216  return 0;
217  else
218  return d->cells->at(idx);
219 }
220 
229 void QGLColormap::setEntry(int idx, const QColor &color)
230 {
231  setEntry(idx, color.rgb());
232 }
233 
238 {
239  if (d == &shared_null || !d->cells)
240  return QColor();
241  else
242  return QColor(d->cells->at(idx));
243 }
244 
259 {
260  return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
261 }
262 
263 
267 int QGLColormap::size() const
268 {
269  return d->cells ? d->cells->size() : 0;
270 }
271 
276 int QGLColormap::find(QRgb color) const
277 {
278  if (d->cells)
279  return d->cells->indexOf(color);
280  return -1;
281 }
282 
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 }
311 
QColor entryColor(int idx) const
Returns the QRgb value in the colorcell with index idx.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
QVector< QRgb > * cells
Definition: qglcolormap.h:82
unsigned int QRgb
Definition: qrgb.h:53
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qvector.h:382
bool isEmpty() const
Returns true if the colormap is empty or it is not in use by a QGLWidget; otherwise returns false...
int size() const
Returns the number of colorcells in the colormap.
static struct QGLColormapData shared_null
Definition: qglcolormap.h:87
void setEntry(int idx, QRgb color)
Set cell at index idx in the colormap to color color.
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
void detach_helper()
static const uint base
Definition: qurl.cpp:268
QGLColormap()
Construct a QGLColormap.
Q_GUI_EXPORT_INLINE int qRed(QRgb rgb)
Definition: qrgb.h:57
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QGLColormapData * d
Definition: qglcolormap.h:86
void setEntries(int count, const QRgb *colors, int base=0)
Set an array of cells in this colormap.
void detach()
Detaches this QGLColormap from the shared block.
Definition: qglcolormap.h:95
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
Q_GUI_EXPORT_INLINE int qBlue(QRgb rgb)
Definition: qrgb.h:63
The QGLColormap class is used for installing custom colormaps into a QGLWidget.
Definition: qglcolormap.h:54
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QRgb entryRgb(int idx) const
Returns the QRgb value in the colorcell with index idx.
int findNearest(QRgb color) const
Returns the index of the color that is the closest match to color color.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QGLColormap & operator=(const QGLColormap &)
Assign a shallow copy of map to this QGLColormap.
Q_GUI_EXPORT_INLINE int qGreen(QRgb rgb)
Definition: qrgb.h:60
int find(QRgb color) const
Returns the index of the color color.
~QGLColormap()
Dereferences the QGLColormap and deletes it if this was the last reference to it. ...
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QRgb rgb() const
Returns the RGB value of the color.
Definition: qcolor.cpp:1051
static void cleanup(QGLColormapData *x)