1 /****************************************************************************
3 ** Copyright (C) 2011 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 QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "qcolormap.h"
45 #include "qt_windows.h"
47 #if defined(Q_WS_WINCE)
48 #include "qguifunctions_wince.h"
53 class QColormapPrivate
56 inline QColormapPrivate()
57 : ref(1), mode(QColormap::Direct), depth(0), hpal(0)
67 QVector<QColor> palette;
70 static QColormapPrivate *screenMap = 0;
72 void QColormap::initialize()
74 HDC dc = qt_win_display_dc();
76 screenMap = new QColormapPrivate;
77 screenMap->depth = GetDeviceCaps(dc, BITSPIXEL);
79 screenMap->numcolors = -1;
80 if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)
81 screenMap->numcolors = GetDeviceCaps(dc, SIZEPALETTE);
83 if (screenMap->numcolors <= 16 || screenMap->numcolors > 256) // no need to create palette
87 int numPalEntries = 6*6*6; // color cube
89 pal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + numPalEntries * sizeof(PALETTEENTRY));
90 // Make 6x6x6 color cube
92 for(int ir = 0x0; ir <= 0xff; ir+=0x33) {
93 for(int ig = 0x0; ig <= 0xff; ig+=0x33) {
94 for(int ib = 0x0; ib <= 0xff; ib+=0x33) {
95 pal->palPalEntry[idx].peRed = ir;
96 pal->palPalEntry[idx].peGreen = ig;
97 pal->palPalEntry[idx].peBlue = ib;
98 pal->palPalEntry[idx].peFlags = 0;
104 pal->palVersion = 0x300;
105 pal->palNumEntries = numPalEntries;
107 screenMap->hpal = CreatePalette(pal);
108 if (!screenMap->hpal)
109 qErrnoWarning("QColor::initialize: Failed to create logical palette");
112 SelectPalette(dc, screenMap->hpal, FALSE);
115 PALETTEENTRY paletteEntries[256];
116 screenMap->numcolors = GetPaletteEntries(screenMap->hpal, 0, 255, paletteEntries);
118 screenMap->palette.resize(screenMap->numcolors);
119 for (int i = 0; i < screenMap->numcolors; i++) {
120 screenMap->palette[i] = qRgb(paletteEntries[i].peRed,
121 paletteEntries[i].peGreen,
122 paletteEntries[i].peBlue);
126 void QColormap::cleanup()
131 if (screenMap->hpal) { // delete application global
132 DeleteObject(screenMap->hpal); // palette
140 QColormap QColormap::instance(int)
142 Q_ASSERT_X(screenMap, "QColormap",
143 "A QApplication object needs to be constructed before QColormap is used.");
147 QColormap::QColormap()
151 QColormap::QColormap(const QColormap &colormap)
155 QColormap::~QColormap()
161 QColormap::Mode QColormap::mode() const
164 int QColormap::depth() const
167 int QColormap::size() const
168 { return d->numcolors; }
170 uint QColormap::pixel(const QColor &color) const
172 const QColor c = color.toRgb();
173 COLORREF rgb = RGB(c.red(), c.green(), c.blue());
175 return PALETTEINDEX(GetNearestPaletteIndex(d->hpal, rgb));
179 const QColor QColormap::colorAt(uint pixel) const
182 if (pixel < uint(d->numcolors))
183 return d->palette.at(pixel);
186 return QColor(GetRValue(pixel), GetGValue(pixel), GetBValue(pixel));
190 HPALETTE QColormap::hPal()
191 { return screenMap ? screenMap->hpal : 0; }
194 const QVector<QColor> QColormap::colormap() const
195 { return d->palette; }
197 QColormap &QColormap::operator=(const QColormap &colormap)
198 { qAtomicAssign(d, colormap.d); return *this; }