~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/extensions/dockers/colorselectorng/kis_color_selector_simple.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2010 Adam Celarek <kdedev at xibo dot at>
 
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
 */
 
17
 
 
18
#include "kis_color_selector_simple.h"
 
19
 
 
20
#include <QImage>
 
21
#include <QPainter>
 
22
#include <QColor>
 
23
#include <cmath>
 
24
 
 
25
#include <KDebug>
 
26
 
 
27
KisColorSelectorSimple::KisColorSelectorSimple(KisColorSelector *parent) :
 
28
    KisColorSelectorComponent(parent),
 
29
    m_lastClickPos(-1,-1)
 
30
{
 
31
}
 
32
 
 
33
void KisColorSelectorSimple::setColor(const QColor &c)
 
34
{
 
35
    switch (m_parameter) {
 
36
    case KisColorSelector::SL:
 
37
        m_lastClickPos.setX(c.hslSaturationF());
 
38
        m_lastClickPos.setY(1.-c.lightnessF());
 
39
        emit paramChanged(-1, -1, -1, c.hslSaturationF(), c.lightnessF());
 
40
        break;
 
41
    case KisColorSelector::LH:
 
42
        m_lastClickPos.setX(qBound(0., c.hueF(), 1.));
 
43
        m_lastClickPos.setY(1.-c.lightnessF());
 
44
        emit paramChanged(c.hueF(), -1, -1, -1, c.lightnessF());
 
45
        break;
 
46
    case KisColorSelector::SV:
 
47
        m_lastClickPos.setX(c.saturationF());
 
48
        m_lastClickPos.setY(1-c.valueF());
 
49
        emit paramChanged(-1, c.saturationF(), c.valueF(), -1, -1);
 
50
        break;
 
51
    case KisColorSelector::VH:
 
52
        m_lastClickPos.setX(qBound(0., c.hueF(), 1.));
 
53
        m_lastClickPos.setY(c.valueF());
 
54
        emit paramChanged(c.hueF(), -1, c.valueF(), -1, -1);
 
55
        break;
 
56
    case KisColorSelector::hsvSH:
 
57
        m_lastClickPos.setX(qBound(0., c.hueF(), 1.));
 
58
        m_lastClickPos.setY(1-c.saturationF());
 
59
        emit paramChanged(c.hueF(), c.saturationF(), -1, -1, -1);
 
60
        break;
 
61
    case KisColorSelector::hslSH:
 
62
        m_lastClickPos.setX(qBound(0., c.hueF(), 1.));
 
63
        m_lastClickPos.setY(1-c.hslSaturationF());
 
64
        emit paramChanged(c.hueF(), -1, -1, c.hslSaturationF(), -1);
 
65
        break;
 
66
    case KisColorSelector::L:
 
67
        m_lastClickPos.setX(1-c.lightnessF());
 
68
        emit paramChanged(-1, -1, -1, -1, c.lightnessF());
 
69
        break;
 
70
    case KisColorSelector::V:
 
71
        m_lastClickPos.setX(c.valueF());
 
72
        emit paramChanged(-1, -1, c.valueF(), -1, -1);
 
73
        break;
 
74
    case KisColorSelector::hsvS:
 
75
        m_lastClickPos.setX(c.saturationF());
 
76
        emit paramChanged(-1, c.saturationF(), -1, -1, -1);
 
77
        break;
 
78
    case KisColorSelector::hslS:
 
79
        m_lastClickPos.setX(c.hslSaturationF());
 
80
        emit paramChanged(-1, -1, -1, c.hslSaturationF(), -1);
 
81
        break;
 
82
    case KisColorSelector::H:
 
83
        m_lastClickPos.setX(1-qBound(0., c.hueF(), 1.));
 
84
        emit paramChanged(c.hueF(), -1, -1, -1, -1);
 
85
        break;
 
86
    default:
 
87
        Q_ASSERT(false);
 
88
        break;
 
89
    }
 
90
    emit update();
 
91
}
 
92
 
 
93
QColor KisColorSelectorSimple::selectColor(int x, int y)
 
94
{
 
95
    m_kocolor.convertTo(colorSpace());
 
96
 
 
97
    m_lastClickPos.setX(x/qreal(width()));
 
98
    m_lastClickPos.setY(y/qreal(height()));
 
99
 
 
100
    qreal xRel = x/qreal(width());
 
101
    qreal yRel = 1.-y/qreal(height());
 
102
    qreal relPos;
 
103
    if(height()>width())
 
104
        relPos = 1.-y/qreal(height());
 
105
    else
 
106
        relPos = 1.-x/qreal(width());
 
107
 
 
108
    switch (m_parameter) {
 
109
    case KisColorSelector::H:
 
110
        emit paramChanged(relPos, -1, -1, -1, -1);
 
111
        break;
 
112
    case KisColorSelector::hsvS:
 
113
        emit paramChanged(-1, relPos, -1, -1, -1);
 
114
        break;
 
115
    case KisColorSelector::hslS:
 
116
        emit paramChanged(-1, -1, -1, relPos, -1);
 
117
        break;
 
118
    case KisColorSelector::V:
 
119
        emit paramChanged(-1, -1, relPos, -1, -1);
 
120
        break;
 
121
    case KisColorSelector::L:
 
122
        emit paramChanged(-1, -1, -1, -1, relPos);
 
123
        break;
 
124
    case KisColorSelector::SL:
 
125
        emit paramChanged(-1, -1, -1, xRel, yRel);
 
126
        break;
 
127
    case KisColorSelector::SV:
 
128
        emit paramChanged(-1, xRel, yRel, -1, -1);
 
129
        break;
 
130
    case KisColorSelector::hsvSH:
 
131
        emit paramChanged(xRel, yRel, -1, -1, -1);
 
132
        break;
 
133
    case KisColorSelector::hslSH:
 
134
        emit paramChanged(xRel, -1, -1, yRel, -1);
 
135
        break;
 
136
    case KisColorSelector::VH:
 
137
        emit paramChanged(xRel, -1, yRel, -1, -1);
 
138
        break;
 
139
    case KisColorSelector::LH:
 
140
        emit paramChanged(xRel, -1, -1, -1, yRel);
 
141
        break;
 
142
    }
 
143
 
 
144
    emit update();
 
145
 
 
146
//    kDebug()<<"selectColor(x/y) y rel="<<yRel<<"  value="<<QColor::fromRgb(colorAt(x, y)).valueF();
 
147
    return colorAt(x, y);
 
148
}
 
149
 
 
150
void KisColorSelectorSimple::paint(QPainter* painter)
 
151
{
 
152
    if(isDirty()) {
 
153
        m_kocolor.convertTo(colorSpace());
 
154
 
 
155
        m_pixelCache=QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
 
156
 
 
157
        for(int x=0; x<width(); x++) {
 
158
            for(int y=0; y<height(); y++) {
 
159
                m_kocolor.fromQColor(colorAt(x, y));
 
160
                m_kocolor.toQColor(&m_qcolor);
 
161
                m_pixelCache.setPixel(x, y, m_qcolor.rgb());
 
162
            }
 
163
        }
 
164
    }
 
165
 
 
166
    painter->drawImage(0,0, m_pixelCache);
 
167
 
 
168
    // draw blip
 
169
    if(m_lastClickPos!=QPointF(-1,-1) && m_parent->displayBlip()) {
 
170
        switch (m_parameter) {
 
171
        case KisColorSelector::H:
 
172
        case KisColorSelector::hsvS:
 
173
        case KisColorSelector::hslS:
 
174
        case KisColorSelector::V:
 
175
        case KisColorSelector::L:
 
176
            if(width()>height()) {
 
177
                painter->setPen(QColor(0,0,0));
 
178
                painter->drawLine(m_lastClickPos.x()*width()-1, 0, m_lastClickPos.x()*width()-1, height());
 
179
                painter->setPen(QColor(255,255,255));
 
180
                painter->drawLine(m_lastClickPos.x()*width()+1, 0, m_lastClickPos.x()*width()+1, height());
 
181
            }
 
182
            else {
 
183
                painter->setPen(QColor(0,0,0));
 
184
                painter->drawLine(0, m_lastClickPos.x()*height()-1, width(), m_lastClickPos.x()*height()-1);
 
185
                painter->setPen(QColor(255,255,255));
 
186
                painter->drawLine(0, m_lastClickPos.x()*height()+1, width(), m_lastClickPos.x()*height()+1);
 
187
            }
 
188
            break;
 
189
        case KisColorSelector::SL:
 
190
        case KisColorSelector::SV:
 
191
        case KisColorSelector::hslSH:
 
192
        case KisColorSelector::hsvSH:
 
193
        case KisColorSelector::VH:
 
194
        case KisColorSelector::LH:
 
195
            painter->setPen(QColor(0,0,0));
 
196
            painter->drawEllipse(m_lastClickPos.x()*width()-5, m_lastClickPos.y()*height()-5, 10, 10);
 
197
            painter->setPen(QColor(255,255,255));
 
198
            painter->drawEllipse(m_lastClickPos.x()*width()-4, m_lastClickPos.y()*height()-4, 8, 8);
 
199
            break;
 
200
        }
 
201
 
 
202
    }
 
203
}
 
204
 
 
205
const QColor& KisColorSelectorSimple::colorAt(int x, int y)
 
206
{
 
207
    if (x < 0) x = 0;
 
208
    if (x > width()) x = width();
 
209
    if (y < 0) y = 0;
 
210
    if (y > width()) y = height();
 
211
 
 
212
    qreal xRel = x/qreal(width());
 
213
    qreal yRel = 1.-y/qreal(height());
 
214
    qreal relPos;
 
215
    if(height()>width())
 
216
        relPos = 1.-y/qreal(height());
 
217
    else
 
218
        relPos = 1.-x/qreal(width());
 
219
 
 
220
    switch(m_parameter) {
 
221
    case KisColorSelector::SL:
 
222
        m_qcolor.setHslF(m_hue, xRel, yRel);
 
223
        break;
 
224
    case KisColorSelector::SV:
 
225
        m_qcolor.setHsvF(m_hue, xRel, yRel);
 
226
        break;
 
227
    case KisColorSelector::hsvSH:
 
228
        m_qcolor.setHsvF(xRel, yRel, m_value);
 
229
        break;
 
230
    case KisColorSelector::hslSH:
 
231
        m_qcolor.setHslF(xRel, yRel, m_lightness);
 
232
        break;
 
233
    case KisColorSelector::VH:
 
234
        m_qcolor.setHsvF(xRel, m_hsvSaturation, yRel);
 
235
        break;
 
236
    case KisColorSelector::LH:
 
237
        m_qcolor.setHslF(xRel, m_hslSaturation, yRel);
 
238
        break;
 
239
    case KisColorSelector::H:
 
240
        m_qcolor.setHsvF(relPos, 1, 1);
 
241
        break;
 
242
    case KisColorSelector::hsvS:
 
243
        m_qcolor.setHsvF(m_hue, relPos, m_value);
 
244
        break;
 
245
    case KisColorSelector::hslS:
 
246
        m_qcolor.setHslF(m_hue, relPos, m_lightness);
 
247
        break;
 
248
    case KisColorSelector::V:
 
249
        m_qcolor.setHsvF(m_hue, m_hsvSaturation, relPos);
 
250
        break;
 
251
    case KisColorSelector::L:
 
252
        m_qcolor.setHslF(m_hue, m_hslSaturation, relPos);
 
253
        break;
 
254
    default:
 
255
        Q_ASSERT(false);
 
256
        m_qcolor = QColor();
 
257
        return m_qcolor;
 
258
    }
 
259
 
 
260
    return m_qcolor;
 
261
}