lib

ko_color_wheel.cc

00001 /*
00002  * Copyright (c) 2004 Sven Langkamp <longamp@reallygood.de>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program 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
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <math.h>
00020 
00021 #include <qpoint.h>
00022 #include <qpainter.h>
00023 #include <qimage.h>
00024 
00025 #include <kdebug.h>
00026 #include "ko_color_wheel.h"
00027 
00028 #define pi 3.14159265
00029 
00030 KoColorWheel::KoColorWheel( QWidget *parent, const char *name ): KXYSelector( parent, name )
00031 {
00032 
00033 }
00034 
00035 void KoColorWheel::resizeEvent( QResizeEvent * )
00036 {
00037     drawWheel(&m_pixmap);
00038     setRange( 0, 0, contentsRect().width(), contentsRect().height() );
00039 }
00040 
00041 void KoColorWheel::drawContents( QPainter *painter )
00042 {
00043     painter->drawPixmap( contentsRect().x(), contentsRect().y(), m_pixmap );
00044 }
00045 
00046 void KoColorWheel::drawWheel( QPixmap *pixmap )
00047 {
00048     int size = QMIN(contentsRect().width(), contentsRect().height());
00049     QPoint center(size/2, size/2);
00050 
00051     QImage image( size, size, 32 );
00052     image.fill(colorGroup ().background().pixel());
00053 
00054     QColor col;
00055     int a, b, h, s;
00056     uint *p;
00057 
00058     for ( a = size-1; a >= 0; a-- )
00059     {
00060         p = (uint*) image.scanLine( size - a - 1 );
00061         for( b = 0; b < size; b++ )
00062         {
00063             s = (int)(sqrt(pow(a-center.y(), 2) + pow(b-center.x(), 2))/(size/2)*255);
00064             if(s<=255)
00065             {
00066                 h = (int)(atan2( b-center.x(), a-center.y())* 180.0 / pi);
00067                 if(h<0) h += 360;
00068                 if(h>360) h -= 360;
00069 
00070                 col.setHsv( h, s, 210 );
00071                 *p = col.rgb();
00072             }
00073             p++;
00074         }
00075     }
00076     pixmap->convertFromImage( image );
00077 }
00078 
00079 void KoColorWheel::mousePressEvent( QMouseEvent *e )
00080 {
00081     int size = QMIN(contentsRect().width(), contentsRect().height());
00082     QPoint center(size/2, size/2);
00083 
00084     int xVal, yVal;
00085     valuesFromPosition( e->pos().x() - 2, e->pos().y() - 2, xVal, yVal );
00086     setValues( xVal, yVal );
00087 
00088     int h, s;
00089 
00090     s = (int)(sqrt(pow(yVal-center.y(), 2) + pow(xVal-center.x(), 2))/(size/2)*255);
00091     if(s>255) s = 255;
00092 
00093     h = (int)(atan2( xVal-center.x(), yVal-center.y())* 180.0 / pi);
00094     if(h<0) h += 360;
00095     if(h>360) h -= 360;
00096 
00097     m_color.setHSV( h, s, 255);
00098     emit valueChanged(m_color);
00099 }
00100 
00101 void KoColorWheel::mouseMoveEvent( QMouseEvent *e )
00102 {
00103     mousePressEvent( e );
00104 }
00105 
00106 void KoColorWheel::slotSetValue(const KoColor& c)
00107 {
00108     int size = QMIN(contentsRect().width(), contentsRect().height());
00109     QPoint center(size/2, size/2);
00110 
00111     int xVal, yVal;
00112     xVal = (int)(sin(c.H() * pi /180) * c.S() / 255 * (size/2) + center.x());
00113     yVal = (int)(cos(c.H() * pi /180) * c.S() / 255 * (size/2) + center.y());
00114     setValues( xVal, yVal );
00115 }
00116 
00117 #include "ko_color_wheel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys