summaryrefslogtreecommitdiffstats
path: root/lib/kopainter/ko_color_wheel.cpp
blob: f309125ca9e83dd5844048e08073a3638f176b5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2004 Sven Langkamp <longamp@reallygood.de>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <math.h>

#include <tqpoint.h>
#include <tqpainter.h>
#include <tqimage.h>

#include <kdebug.h>
#include "ko_color_wheel.h"

#define pi 3.14159265

KoColorWheel::KoColorWheel( TQWidget *parent, const char *name ): KXYSelector( parent, name )
{

}

void KoColorWheel::resizeEvent( TQResizeEvent * )
{
    drawWheel(&m_pixmap);
    setRange( 0, 0, contentsRect().width(), contentsRect().height() );
}

void KoColorWheel::drawContents( TQPainter *painter )
{
    painter->drawPixmap( contentsRect().x(), contentsRect().y(), m_pixmap );
}

void KoColorWheel::drawWheel( TQPixmap *pixmap )
{
    int size = TQMIN(contentsRect().width(), contentsRect().height());
    TQPoint center(size/2, size/2);

    TQImage image( size, size, 32 );
    image.fill(colorGroup ().background().pixel());

    TQColor col;
    int a, b, h, s;
    uint *p;

    for ( a = size-1; a >= 0; a-- )
    {
        p = (uint*) image.scanLine( size - a - 1 );
        for( b = 0; b < size; b++ )
        {
            s = (int)(sqrt(pow(a-center.y(), 2) + pow(b-center.x(), 2))/(size/2)*255);
            if(s<=255)
            {
                h = (int)(atan2( b-center.x(), a-center.y())* 180.0 / pi);
                if(h<0) h += 360;
                if(h>360) h -= 360;

                col.setHsv( h, s, 210 );
                *p = col.rgb();
            }
            p++;
        }
    }
    pixmap->convertFromImage( image );
}

void KoColorWheel::mousePressEvent( TQMouseEvent *e )
{
    int size = TQMIN(contentsRect().width(), contentsRect().height());
    TQPoint center(size/2, size/2);

    int xVal, yVal;
    valuesFromPosition( e->pos().x() - 2, e->pos().y() - 2, xVal, yVal );
    setValues( xVal, yVal );

    int h, s;

    s = (int)(sqrt(pow(yVal-center.y(), 2) + pow(xVal-center.x(), 2))/(size/2)*255);
    if(s>255) s = 255;

    h = (int)(atan2( xVal-center.x(), yVal-center.y())* 180.0 / pi);
    if(h<0) h += 360;
    if(h>360) h -= 360;

    m_color.setHSV( h, s, 255);
    emit valueChanged(m_color);
}

void KoColorWheel::mouseMoveEvent( TQMouseEvent *e )
{
    mousePressEvent( e );
}

void KoColorWheel::slotSetValue(const KoColor& c)
{
    int size = TQMIN(contentsRect().width(), contentsRect().height());
    TQPoint center(size/2, size/2);

    int xVal, yVal;
    xVal = (int)(sin(c.H() * pi /180) * c.S() / 255 * (size/2) + center.x());
    yVal = (int)(cos(c.H() * pi /180) * c.S() / 255 * (size/2) + center.y());
    setValues( xVal, yVal );
}

#include "ko_color_wheel.moc"