• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kcolorvalueselector.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1997 Martin Jones (mjones@kde.org)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kcolorvalueselector.h"
00021 #include <QPainter>
00022 #include <iostream>
00023 
00024 class KColorValueSelector::Private
00025 {
00026 public:
00027   Private(KColorValueSelector *q): q(q), _hue(0), _sat(0), _colorValue(0), _mode(ChooserClassic) {}
00028 
00029     KColorValueSelector *q;
00030     int _hue;
00031     int _sat;
00032     int _colorValue;
00033     KColorChooserMode _mode;
00034   QPixmap pixmap;
00035 };
00036 
00037 KColorValueSelector::KColorValueSelector( QWidget *parent )
00038         : KSelector( Qt::Vertical, parent ), d( new Private( this ) )
00039 {
00040     setRange( 0, 255 );
00041 }
00042 
00043 KColorValueSelector::KColorValueSelector( Qt::Orientation o, QWidget *parent )
00044         : KSelector( o, parent ), d( new Private( this ) )
00045 {
00046     setRange( 0, 255 );
00047 }
00048 
00049 KColorValueSelector::~KColorValueSelector()
00050 {
00051     delete d;
00052 }
00053 
00054 int KColorValueSelector::hue() const
00055 {
00056     return d->_hue;
00057 }
00058 
00059 void KColorValueSelector::setHue( int hue )
00060 {
00061     d->_hue = hue;
00062 }
00063 
00064 int KColorValueSelector::saturation() const
00065 {
00066     return d->_sat;
00067 }
00068 
00069 void KColorValueSelector::setSaturation( int saturation )
00070 {
00071     d->_sat = saturation;
00072 }
00073 
00074 int KColorValueSelector::colorValue () const
00075 {
00076     return d->_colorValue;
00077 }
00078 
00079 void KColorValueSelector::setColorValue ( int colorValue )
00080 {
00081     d->_colorValue = colorValue;
00082 }
00083 
00084 
00085 
00086 void KColorValueSelector::updateContents()
00087 {
00088     drawPalette( &d->pixmap );
00089 }
00090 
00091 void KColorValueSelector::resizeEvent( QResizeEvent * )
00092 {
00093     updateContents();
00094 }
00095 
00096 void KColorValueSelector::drawContents( QPainter *painter )
00097 {
00098     painter->drawPixmap( contentsRect().x(), contentsRect().y(), d->pixmap );
00099 }
00100 
00101 void KColorValueSelector::setChooserMode( KColorChooserMode c )
00102 {
00103     if ( c == ChooserHue ) {
00104         setRange( 0, 360 );
00105     } else {
00106         setRange( 0, 255 );
00107     }
00108     d->_mode = c;
00109 
00110     //really needed?
00111     //emit modeChanged();
00112 }
00113 
00114 KColorChooserMode KColorValueSelector::chooserMode () const
00115 {
00116     return d->_mode;
00117 }
00118 
00119 void KColorValueSelector::drawPalette( QPixmap *pixmap )
00120 {
00121     int xSize = contentsRect().width(), ySize = contentsRect().height();
00122     QImage image( QSize( xSize, ySize ), QImage::Format_RGB32 );
00123     QColor col;
00124     uint *p;
00125     QRgb rgb;
00126     int _r, _g, _b;
00127 
00128     col.setHsv( hue(), saturation(), colorValue() );
00129     col.getRgb( &_r, &_g, &_b );
00130 
00131     if ( orientation() == Qt::Horizontal )
00132     {
00133         for ( int v = 0; v < ySize; v++ )
00134         {
00135             p = ( uint * ) image.scanLine( ySize - v - 1 );
00136 
00137             for( int x = 0; x < xSize; x++ )
00138             {
00139 
00140                 switch ( chooserMode() ) {
00141                 case ChooserClassic:
00142                 default:
00143                     col.setHsv( hue(), saturation(), 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ) );
00144                     break;
00145                 case ChooserRed:
00146                     col.setRgb( 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ), _g, _b );
00147                     break;
00148                 case ChooserGreen:
00149                     col.setRgb( _r, 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ), _b );
00150                     break;
00151                 case ChooserBlue:
00152                     col.setRgb( _r, _g, 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ) );
00153                     break;
00154                 case ChooserHue:
00155                     col.setHsv( 360 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ), 255, 255 );
00156                     break;
00157                 case ChooserSaturation:
00158                     col.setHsv( hue(), 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ), colorValue() );
00159                     break;
00160                 case ChooserValue:
00161                     col.setHsv( hue(), saturation(), 255 * x / ( ( xSize == 1 ) ? 1 : xSize - 1 ) );
00162                     break;
00163                 }
00164 
00165                 rgb = col.rgb();
00166                 *p++ = rgb;
00167             }
00168         }
00169     }
00170 
00171     if( orientation() == Qt::Vertical )
00172     {
00173         for ( int v = 0; v < ySize; v++ )
00174         {
00175             p = ( uint * ) image.scanLine( ySize - v - 1 );
00176 
00177             switch ( chooserMode() ) {
00178             case ChooserClassic:
00179             default:
00180                 col.setHsv( hue(), saturation(), 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ) );
00181                 break;
00182             case ChooserRed:
00183                 col.setRgb( 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ), _g, _b );
00184                 break;
00185             case ChooserGreen:
00186                 col.setRgb( _r, 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ), _b );
00187                 break;
00188             case ChooserBlue:
00189                 col.setRgb( _r, _g, 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ) );
00190                 break;
00191             case ChooserHue:
00192                 col.setHsv( 360 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ), 255, 255 );
00193                 break;
00194             case ChooserSaturation:
00195                 col.setHsv( hue(), 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ), colorValue() );
00196                 break;
00197             case ChooserValue:
00198                 col.setHsv( hue(), saturation(), 255 * v / ( ( ySize == 1 ) ? 1 : ySize - 1 ) );
00199                 break;
00200             }
00201 
00202             rgb = col.rgb();
00203             for ( int i = 0; i < xSize; i++ )
00204                 *p++ = rgb;
00205         }
00206     }
00207 
00208     /*
00209     if ( pixmap->depth() <= 8 )
00210     {
00211         extern QVector<QColor> kdeui_standardPalette();
00212         const QVector<QColor> standardPalette = kdeui_standardPalette();
00213         KImageEffect::dither( image, standardPalette.data(), standardPalette.size() );
00214     }
00215     */
00216     *pixmap = QPixmap::fromImage( image );
00217 }
00218 
00219 
00220 #include "kcolorvalueselector.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal