00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <QColor>
00021 #include <QPainter>
00022
00023 #include "KoColorSlider.h"
00024
00025 KoColorSlider::KoColorSlider(QWidget* parent)
00026 : KSelector(parent)
00027 {
00028 setMaximum(255);
00029 }
00030
00031 KoColorSlider::KoColorSlider(Qt::Orientation o, QWidget *parent)
00032 : KSelector(o, parent)
00033 {
00034 setMaximum(255);
00035 }
00036
00037 KoColorSlider::~KoColorSlider()
00038 {
00039 }
00040
00041 void KoColorSlider::setColors(const KoColor& mincolor, const KoColor& maxcolor)
00042 {
00043 m_minColor = mincolor;
00044 m_maxColor = maxcolor;
00045
00046 update();
00047 }
00048
00049 void KoColorSlider::drawContents( QPainter *painter )
00050 {
00051 QPixmap checker(8, 8);
00052 QPainter p(&checker);
00053 p.fillRect(0, 0, 4, 4, Qt::lightGray);
00054 p.fillRect(4, 0, 4, 4, Qt::darkGray);
00055 p.fillRect(0, 4, 4, 4, Qt::darkGray);
00056 p.fillRect(4, 4, 4, 4, Qt::lightGray);
00057 p.end();
00058 painter->fillRect(contentsRect(), QBrush(checker));
00059
00060 KoColor c = m_minColor;
00061 QColor color;
00062 quint8 opacity;
00063
00064 const quint8 *colors[2];
00065 colors[0] = m_minColor.data();
00066 colors[1] = m_maxColor.data();
00067
00068 QImage image(contentsRect().width(), contentsRect().height(), QImage::Format_ARGB32 );
00069
00070 if( orientation() == Qt::Horizontal ) {
00071 for (int x = 0; x < contentsRect().width(); x++) {
00072
00073 double t = static_cast<double>(x) / (contentsRect().width() - 1);
00074
00075 quint8 colorWeights[2];
00076 colorWeights[0] = static_cast<quint8>((1.0 - t) * 255 + 0.5);
00077 colorWeights[1] = 255 - colorWeights[0];
00078
00079 c.colorSpace()->mixColors(colors, colorWeights, 2, c.data());
00080
00081 c.toQColor(&color, &opacity);
00082 color.setAlpha(opacity);
00083
00084 for (int y = 0; y < contentsRect().height(); y++)
00085 image.setPixel(x, y, color.rgba());
00086 }
00087 }
00088 else {
00089 for (int y = 0; y < contentsRect().height(); y++) {
00090
00091 double t = static_cast<double>(y) / (contentsRect().height() - 1);
00092
00093 quint8 colorWeights[2];
00094 colorWeights[0] = static_cast<quint8>((t) * 255 + 0.5);
00095 colorWeights[1] = 255 - colorWeights[0];
00096
00097 c.colorSpace()->mixColors(colors, colorWeights, 2, c.data());
00098
00099 c.toQColor(&color, &opacity);
00100 color.setAlpha(opacity);
00101
00102 for (int x = 0; x < contentsRect().width(); x++)
00103 image.setPixel(x, y, color.rgba());
00104 }
00105 }
00106 painter->drawImage( contentsRect(), image, QRect( 0, 0, image.width(), image.height()) );
00107 }
00108
00109 #include "KoColorSlider.moc"