lib

ko_color_wheel.cpp
1/*
2 * Copyright (c) 2004 Sven Langkamp <longamp@reallygood.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
18
19#include <math.h>
20
21#include <tqpoint.h>
22#include <tqpainter.h>
23#include <tqimage.h>
24
25#include <kdebug.h>
26#include "ko_color_wheel.h"
27
28#define pi 3.14159265
29
30KoColorWheel::KoColorWheel( TQWidget *parent, const char *name ): KXYSelector( parent, name )
31{
32
33}
34
35void KoColorWheel::resizeEvent( TQResizeEvent * )
36{
37 drawWheel(&m_pixmap);
38 setRange( 0, 0, contentsRect().width(), contentsRect().height() );
39}
40
41void KoColorWheel::drawContents( TQPainter *painter )
42{
43 painter->drawPixmap( contentsRect().x(), contentsRect().y(), m_pixmap );
44}
45
46void KoColorWheel::drawWheel( TQPixmap *pixmap )
47{
48 int size = TQMIN(contentsRect().width(), contentsRect().height());
49 TQPoint center(size/2, size/2);
50
51 TQImage image( size, size, 32 );
52 image.fill(colorGroup ().background().pixel());
53
54 TQColor col;
55 int a, b, h, s;
56 uint *p;
57
58 for ( a = size-1; a >= 0; a-- )
59 {
60 p = (uint*) image.scanLine( size - a - 1 );
61 for( b = 0; b < size; b++ )
62 {
63 s = (int)(sqrt(pow(a-center.y(), 2) + pow(b-center.x(), 2))/(size/2)*255);
64 if(s<=255)
65 {
66 h = (int)(atan2( b-center.x(), a-center.y())* 180.0 / pi);
67 if(h<0) h += 360;
68 if(h>360) h -= 360;
69
70 col.setHsv( h, s, 210 );
71 *p = col.rgb();
72 }
73 p++;
74 }
75 }
76 pixmap->convertFromImage( image );
77}
78
79void KoColorWheel::mousePressEvent( TQMouseEvent *e )
80{
81 int size = TQMIN(contentsRect().width(), contentsRect().height());
82 TQPoint center(size/2, size/2);
83
84 int xVal, yVal;
85 valuesFromPosition( e->pos().x() - 2, e->pos().y() - 2, xVal, yVal );
86 setValues( xVal, yVal );
87
88 int h, s;
89
90 s = (int)(sqrt(pow(yVal-center.y(), 2) + pow(xVal-center.x(), 2))/(size/2)*255);
91 if(s>255) s = 255;
92
93 h = (int)(atan2( xVal-center.x(), yVal-center.y())* 180.0 / pi);
94 if(h<0) h += 360;
95 if(h>360) h -= 360;
96
97 m_color.setHSV( h, s, 255);
98 emit valueChanged(m_color);
99}
100
101void KoColorWheel::mouseMoveEvent( TQMouseEvent *e )
102{
103 mousePressEvent( e );
104}
105
106void KoColorWheel::slotSetValue(const KoColor& c)
107{
108 int size = TQMIN(contentsRect().width(), contentsRect().height());
109 TQPoint center(size/2, size/2);
110
111 int xVal, yVal;
112 xVal = (int)(sin(c.H() * pi /180) * c.S() / 255 * (size/2) + center.x());
113 yVal = (int)(cos(c.H() * pi /180) * c.S() / 255 * (size/2) + center.y());
114 setValues( xVal, yVal );
115}
116
117#include "ko_color_wheel.moc"
void setRange(int minX, int minY, int maxX, int maxY)
TQRect contentsRect() const
void setValues(int xPos, int yPos)
void valuesFromPosition(int x, int y, int &xVal, int &yVal) const
const char * name(StdAction id)