summaryrefslogtreecommitdiffstats
path: root/src/opengl/qglcolormap.cpp
blob: 048b7dc29f6f936bc4667491c77226a5aad2e59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/****************************************************************************
**
** Implementation of TQGLColormap class
**
** Created : 20010326
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the opengl module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

/*!
    \class TQGLColormap ntqglcolormap.h
    \brief The TQGLColormap class is used for installing custom colormaps into
    TQGLWidgets.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \module OpenGL
    \ingroup graphics
    \ingroup images

    TQGLColormap provides a platform independent way of specifying and
    installing indexed colormaps into TQGLWidgets. TQGLColormap is
    especially useful when using the \link opengl.html OpenGL\endlink
    color-index mode.

    Under X11 you must use an X server that supports either a \c
    PseudoColor or \c DirectColor visual class. If your X server
    currently only provides a \c GrayScale, \c TrueColor, \c
    StaticColor or \c 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 \c PseudoColor visual. Note that you may experience
    colormap flashing if your X server is running in 8 bit mode.

    Under Windows 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 explicit sharing (see \link shclass.html Shared
    Classes\endlink).

    Example of use:
    \code
    #include <ntqapplication.h>
    #include <ntqglcolormap.h>

    int main()
    {
	TQApplication a( argc, argv );

	MySuperGLWidget widget( 0 ); // A TQGLWidget in color-index mode
	TQGLColormap colormap;

	// This will fill the colormap with colors ranging from
	// black to white.
	for ( int i = 0; i < colormap.size(); i++ )
	    colormap.setEntry( i, qRgb( i, i, i ) );

	widget.setColormap( colormap );
	widget.show();
	return a.exec();
    }
    \endcode

    \sa TQGLWidget::setColormap(), TQGLWidget::colormap()
*/

#include "ntqglcolormap.h"
#include "ntqmemarray.h"


/*!
    Construct a TQGLColormap.
*/
TQGLColormap::TQGLColormap()
{
    d = 0;
}


/*!
    Construct a shallow copy of \a map.
*/
TQGLColormap::TQGLColormap( const TQGLColormap & map )
{
    d = map.d;
    if ( d )
	d->ref();
}

/*!
    Dereferences the TQGLColormap and deletes it if this was the last
    reference to it.
*/
TQGLColormap::~TQGLColormap()
{
    if ( d && d->deref() ) {
	delete d;
	d = 0;
    }
}

/*!
    Assign a shallow copy of \a map to this TQGLColormap.
*/
TQGLColormap & TQGLColormap::operator=( const TQGLColormap & map )
{
    if ( map.d != 0 )
	map.d->ref();

    if ( d && d->deref() )
	delete d;
    d = map.d;

    return *this;
}

/*!
    Detaches this TQGLColormap from the shared block.
*/
void TQGLColormap::detach()
{
    if ( d && d->count != 1 ) {
	// ### What about the actual colormap handle?
	Private * newd = new Private();
	newd->cells = d->cells;
	newd->cells.detach();
	if ( d->deref() )
	    delete d;
	d = newd;
    }
}

/*!
    Set cell at index \a idx in the colormap to color \a color.
*/
void TQGLColormap::setEntry( int idx, TQRgb color )
{
    if ( !d )
	d = new Private();

#if defined(QT_CHECK_RANGE)
    if ( idx < 0 || idx > (int) d->cells.size() ) {
	qWarning( "TQGLColormap::setRgb: Index out of range." );
	return;
    }
#endif
    d->cells[ idx ] = color;
}

/*!
    Set an array of cells in this colormap. \a count is the number of
    colors that should be set, \a colors is the array of colors, and
    \a base is the starting index.
*/
void TQGLColormap::setEntries( int count, const TQRgb * colors, int base )
{
    if ( !d )
	d = new Private();

    if ( !colors || base < 0 || base >= (int) d->cells.size() )
	return;

    for( int i = base; i < base + count; i++ ) {
	if ( i < (int) d->cells.size() )
	    setEntry( i, colors[i] );
	else
	    break;
    }
}

/*!
    Returns the TQRgb value in the colorcell with index \a idx.
*/
TQRgb TQGLColormap::entryRgb( int idx ) const
{
    if ( !d || idx < 0 || idx > (int) d->cells.size() )
	return 0;
    else
	return d->cells[ idx ];
}

/*!
    \overload

    Set the cell with index \a idx in the colormap to color \a color.
*/
void TQGLColormap::setEntry( int idx, const TQColor & color )
{
    setEntry( idx, color.rgb() );
}

/*!
    Returns the TQRgb value in the colorcell with index \a idx.
*/
TQColor TQGLColormap::entryColor( int idx ) const
{
    if ( !d || idx < 0 || idx > (int) d->cells.size() )
	return TQColor();
    else
	return TQColor( d->cells[ idx ] );
}

/*!
    Returns TRUE if the colormap is empty; otherwise returns FALSE. A
    colormap with no color values set is considered to be empty.
*/
bool TQGLColormap::isEmpty() const
{
    return (d == 0) || (d->cells.size() == 0)  || (d->cmapHandle == 0);
}


/*!
    Returns the number of colorcells in the colormap.
*/
int TQGLColormap::size() const
{
    return d != 0 ? d->cells.size() : 0;
}

/*!
    Returns the index of the color \a color. If \a color is not in the
    map, -1 is returned.
*/
int TQGLColormap::find( TQRgb color ) const
{
    if ( d )
	return d->cells.find( color );
    return -1;
}

/*!
    Returns the index of the color that is the closest match to color
    \a color.
*/
int TQGLColormap::findNearest( TQRgb color ) const
{
    int idx = find( color );
    if ( idx >= 0 )
	return idx;
    int mapSize = size();
    int mindist = 200000;
    int r = qRed( color );
    int g = qGreen( color );
    int b = qBlue( color );
    int rx, gx, bx, dist;
    for ( int i=0; i < mapSize; i++ ) {
	TQRgb ci = d->cells[i];
	rx = r - qRed( ci );
	gx = g - qGreen( ci );
	bx = b - qBlue( ci );
	dist = rx*rx + gx*gx + bx*bx;	// calculate distance
	if ( dist < mindist ) {		// minimal?
	    mindist = dist;
	    idx = i;
	}
    }
    return idx;
}