hitcounter
dpointer
Sunday, December 24, 2006
 
Which color role?

Are you sometimes confused which one of QPalette::Background or QPalette::Base you need to use? Look no more, using this small utility shown below, you can find the exact RGB of each color role of the default widget. This is how it looks like (left: Linux, right: Windows):

And here is the code. You can compile it with:

g++ -o qpal qpal.cpp -I/usr/include/QtGui -L/usr/lib -lQtGui

or create a simple pro file (you know how to this, don't you?

/* QPAL - show colors for the palette

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

 */

#include <QtGui>

int main(int argc, char** argv)
{
 QApplication app(argc, argv);

 QWidget w;
 w.setWindowTitle("QPAL - Palette Viewer");
 QGridLayout layout;
 w.setLayout(&layout);

 QPalette palette;
 palette = w.palette();

 const char* roleNames[] =
 {
  "Window",
  "Background",
  "WindowText",
  "Foreground",
  "Base",
  "AlternateBase",
  "Text",
  "Button",
  "ButtonText",
  "BrightText",
  "",
  "Light",
  "Midlight",
  "Dark",
  "Mid",
  "Shadow",
  "",
  "Highlight",
  "HighlightedText",
  "",
  "Link",
  "LinkVisited"
 };

 const char* groupNames[] = 
 {
  "Active",
  "Inactive",
  "Disabled"
 };

 QPalette::ColorRole roles[] = 
 {
  QPalette::Window,
  QPalette::Background,
  QPalette::WindowText,
  QPalette::Foreground,
  QPalette::Base,
  QPalette::AlternateBase,
  QPalette::Text,
  QPalette::Button,
  QPalette::ButtonText,
  QPalette::BrightText,
  QPalette::Window, // separator
  QPalette::Light,
  QPalette::Midlight,
  QPalette::Dark,
  QPalette::Mid,
  QPalette::Shadow,
  QPalette::Window, // separator
  QPalette::Highlight,
  QPalette::HighlightedText,
  QPalette::Window, // separator
  QPalette::Link,
  QPalette::LinkVisited
 };

 QPalette::ColorGroup groups[] = 
 {
  QPalette::Active,
  QPalette::Inactive,
  QPalette::Disabled
 };

 QLabel* label;
 QLabel* sample;
 QLabel* red;
 QLabel* green;
 QLabel* blue;
 QColor color;

 label = new QLabel(&w);
 label->setText("<b>ROLE</b>");
 layout.addWidget(label, 0, 0);
 for(int k = 0; k < 3; k++)
 {
  layout.addItem(new QSpacerItem(50,10), 0, 1 + k*5);
  label = new QLabel(&w);
  label->setText(QLatin1String(groupNames[k]));
  layout.addWidget(label, 0, 2 + k*5, 1, 4);
 }

 for(int i = 0; i < 19+3; i++)
 {
  QString name = QLatin1String(roleNames[i]);
  label = new QLabel(&w);
  label->setText(name);
  layout.addWidget(label, i+1, 0);
  if(!name.isEmpty())
  for(int j = 0; j < 3; j++)
  {
   color = palette.brush(groups[j], roles[i]).color();
   sample = new QLabel(&w);
   sample->setAutoFillBackground(true);
   QPalette special = sample->palette();
   special.setColor(QPalette::Background, color);
   sample->setPalette(special);
   sample->setFrameShape(QFrame::Box);
   sample->setFixedSize(12, 12);
   red = new QLabel(&w);
   red->setText(QString::number(color.red()));
   red->setAlignment(Qt::AlignRight);
   green = new QLabel(&w);
   green->setText(QString::number(color.green()));
   green->setAlignment(Qt::AlignRight);
   blue = new QLabel(&w);
   blue->setText(QString::number(color.blue()));
   blue->setAlignment(Qt::AlignRight);
   layout.addWidget(sample, i+1, 2 + j*5);
   layout.addWidget(red, i+1, 3 + j*5);
   layout.addWidget(green, i+1, 4 + j*5);
   layout.addWidget(blue, i+1, 5 + j*5);
  }
 }

 w.show();
 return app.exec();
}


Powered by Blogger