11#include <QtCore/qmutex.h>
15#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 11
)
18QT_WARNING_DISABLE_DEPRECATED
23 inline QColormapPrivate()
24 : ref(1), mode(QColormap::Direct), depth(0), numcolors(0)
34static QColormapPrivate *screenMap =
nullptr;
36void QColormap::initialize()
38 screenMap =
new QColormapPrivate;
39 if (Q_UNLIKELY(!QGuiApplication::primaryScreen())) {
40 qWarning(
"no screens available, assuming 24-bit color");
41 screenMap->depth = 24;
42 screenMap->mode = QColormap::Direct;
45 screenMap->depth = QGuiApplication::primaryScreen()->depth();
46 if (screenMap->depth < 8) {
47 screenMap->mode = QColormap::Indexed;
48 screenMap->numcolors = 256;
50 screenMap->mode = QColormap::Direct;
51 screenMap->numcolors = -1;
54 qAddPostRoutine(QColormap::cleanup);
57void QColormap::cleanup()
60 if (!screenMap->ref.deref())
66QColormap QColormap::instance(
int )
69 QMutexLocker locker(&mutex);
80QColormap::QColormap(
const QColormap &colormap)
84QColormap::~QColormap()
90QColormap::Mode QColormap::mode()
const
94int QColormap::depth()
const
98int QColormap::size()
const
103#ifndef QT_QWS_DEPTH16_RGB
104#define QT_QWS_DEPTH16_RGB 565
106static const int qt_rbits = (QT_QWS_DEPTH16_RGB/100);
107static const int qt_gbits = (QT_QWS_DEPTH16_RGB/10%10);
108static const int qt_bbits = (QT_QWS_DEPTH16_RGB%10);
109static const int qt_red_shift = qt_bbits+qt_gbits-(8-qt_rbits);
110static const int qt_green_shift = qt_bbits-(8-qt_gbits);
111static const int qt_neg_blue_shift = 8-qt_bbits;
112static const int qt_blue_mask = (1<<qt_bbits)-1;
113static const int qt_green_mask = (1<<(qt_gbits+qt_bbits))-(1<<qt_bbits);
114static const int qt_red_mask = (1<<(qt_rbits+qt_gbits+qt_bbits))-(1<<(qt_gbits+qt_bbits));
116static const int qt_red_rounding_shift = qt_red_shift + qt_rbits;
117static const int qt_green_rounding_shift = qt_green_shift + qt_gbits;
118static const int qt_blue_rounding_shift = qt_bbits - qt_neg_blue_shift;
120inline ushort qt_convRgbTo16(QRgb c)
122 const int tr = qRed(c) << qt_red_shift;
123 const int tg = qGreen(c) << qt_green_shift;
124 const int tb = qBlue(c) >> qt_neg_blue_shift;
126 return (tb & qt_blue_mask) | (tg & qt_green_mask) | (tr & qt_red_mask);
129inline QRgb qt_conv16ToRgb(ushort c)
131 const int r=(c & qt_red_mask);
132 const int g=(c & qt_green_mask);
133 const int b=(c & qt_blue_mask);
134 const int tr = r >> qt_red_shift | r >> qt_red_rounding_shift;
135 const int tg = g >> qt_green_shift | g >> qt_green_rounding_shift;
136 const int tb = b << qt_neg_blue_shift | b >> qt_blue_rounding_shift;
138 return qRgb(tr,tg,tb);
141uint QColormap::pixel(
const QColor &color)
const
143 QRgb rgb = color.rgba();
144 if (d->mode == QColormap::Direct) {
147 return qt_convRgbTo16(rgb);
151 const int r = qRed(rgb);
152 const int g = qGreen(rgb);
153 const int b = qBlue(rgb);
154 const int red_shift = 16;
155 const int green_shift = 8;
156 const int red_mask = 0xff0000;
157 const int green_mask = 0x00ff00;
158 const int blue_mask = 0x0000ff;
159 const int tg = g << green_shift;
160 const int tr = r << red_shift;
161 return 0xff000000 | (b & blue_mask) | (tg & green_mask) | (tr & red_mask);
170const QColor QColormap::colorAt(uint pixel)
const
172 if (d->mode == Direct) {
173 if (d->depth == 16) {
174 pixel = qt_conv16ToRgb(pixel);
176 const int red_shift = 16;
177 const int green_shift = 8;
178 const int red_mask = 0xff0000;
179 const int green_mask = 0x00ff00;
180 const int blue_mask = 0x0000ff;
181 return QColor((pixel & red_mask) >> red_shift,
182 (pixel & green_mask) >> green_shift,
183 (pixel & blue_mask));
186 Q_ASSERT_X(
int(pixel) < qt_screen->numCols(),
"QColormap::colorAt",
"pixel out of bounds of palette");
187 return QColor(qt_screen->clut()[pixel]);
192const QList<QColor> QColormap::colormap()
const
194 return QList<QColor>();
197QColormap &QColormap::operator=(
const QColormap &colormap)
198{ qAtomicAssign(d, colormap.d);
return *
this; }