summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/viewplugins/colorrange/dlg_colorrange.cc
blob: 453bbec27cc2c84dc23519b079b49171a9fb1da5 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 *  dlg_colorrange.cc - part of KimageShop^WKrayon^WChalk
 *
 *  Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
 *
 *  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 <tqapplication.h>
#include <tqpushbutton.h>
#include <tqcheckbox.h>
#include <tqslider.h>
#include <tqcombobox.h>
#include <tqpixmap.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqcolor.h>
#include <tqradiobutton.h>

#include <knuminput.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeaction.h>

#include <kis_canvas_subject.h>
#include <kis_iterators_pixel.h>
#include <kis_layer.h>
#include <kis_paint_device.h>
#include <kis_selection.h>
#include <kis_selection_manager.h>
#include <kis_types.h>
#include <kis_undo_adapter.h>
#include <kis_view.h>
#include <kis_colorspace.h>
#include <kis_profile.h>
#include <kis_color_conversions.h>
#include <kis_selected_transaction.h>
#include <kis_cursor.h>

#include "dlg_colorrange.h"
#include "wdg_colorrange.h"

namespace {

// XXX: Poynton says: hsv/hls is not what one ought to use for colour calculations.
//      Unfortunately, I don't know enough to be able to use anything else.

    bool isReddish(int h)
    {
        return ((h > 330 && h < 360) || ( h > 0 && h < 40));
    }

    bool isYellowish(int h)
    {
        return (h> 40 && h < 65);
    }

    bool isGreenish(int h)
    {
        return (h > 70 && h < 155);
    }

    bool isCyanish(int h)
    {
        return (h > 150 && h < 190);
    }

    bool isBlueish(int h)
    {
        return (h > 185 && h < 270);
    }

    bool isMagentaish(int h)
    {
        return (h > 265 && h < 330);
    }

    bool isHighlight(int v)
    {
        return (v > 200);
    }

    bool isMidTone(int v)
    {
        return (v > 100 && v < 200);
    }

    bool isShadow(int v)
    {
        return (v < 100);
    }

}

TQ_UINT32 matchColors(const TQColor & c, enumAction action)
{
    int r = c.red();
    int g = c.green();
    int b = c.blue();

    int h, s, v;
    rgb_to_hsv(r, g, b, &h, &s, &v);



    // XXX: Map the degree in which the colors conform to the requirement
    //      to a range of selectedness between 0 and 255

    // XXX: Implement out-of-gamut using lcms

    switch(action) {

        case REDS:
            if (isReddish(h))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case YELLOWS:
            if (isYellowish(h)) {
                return MAX_SELECTED;
            }
            else
                return MIN_SELECTED;
        case GREENS:
            if (isGreenish(h))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case CYANS:
            if (isCyanish(h))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case BLUES:
            if (isBlueish(h))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case MAGENTAS:
            if (isMagentaish(h))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case HIGHLIGHTS:
            if (isHighlight(v))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case MIDTONES:
            if (isMidTone(v))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
        case SHADOWS:
            if (isShadow(v))
                return MAX_SELECTED;
            else
                return MIN_SELECTED;
    };

    return MIN_SELECTED;
}



DlgColorRange::DlgColorRange( KisView * view, KisPaintDeviceSP dev, TQWidget *  parent, const char * name)
    : super (parent, name, true, i18n("Color Range"), Ok | Cancel, Ok)
{
    m_dev = dev;
    m_view = view;

    m_subject = view->canvasSubject();

    m_page = new WdgColorRange(this, "color_range");
    TQ_CHECK_PTR(m_page);

    setCaption(i18n("Color Range"));
    setMainWidget(m_page);
    resize(m_page->sizeHint());

    if (m_dev->image()->undo()) m_transaction = new KisSelectedTransaction(i18n("Select by Color Range"), m_dev);

    if(! m_dev->hasSelection())
        m_dev->selection()->clear();
    m_selection = m_dev->selection();

    updatePreview();

    m_invert = false;
    m_mode = SELECTION_ADD;
    m_currentAction = REDS;

    connect(this, TQT_SIGNAL(okClicked()),
        this, TQT_SLOT(okClicked()));

    connect(this, TQT_SIGNAL(cancelClicked()),
        this, TQT_SLOT(cancelClicked()));

    connect(m_page->chkInvert, TQT_SIGNAL(clicked()),
        this, TQT_SLOT(slotInvertClicked()));

    connect(m_page->cmbSelect, TQT_SIGNAL(activated(int)),
        this, TQT_SLOT(slotSelectionTypeChanged(int)));

    connect (m_page->radioAdd, TQT_SIGNAL(toggled(bool)),
         this, TQT_SLOT(slotAdd(bool)));

    connect (m_page->radioSubtract, TQT_SIGNAL(toggled(bool)),
         this, TQT_SLOT(slotSubtract(bool)));

    connect (m_page->bnSelect, TQT_SIGNAL(clicked()),
        this, TQT_SLOT(slotSelectClicked()));

    connect (m_page->bnDeselect, TQT_SIGNAL(clicked()),
        this, TQT_SLOT(slotDeselectClicked()));

}

DlgColorRange::~DlgColorRange()
{
    delete m_page;
}


void DlgColorRange::updatePreview()
{
    if (!m_selection) return;

    TQ_INT32 x, y, w, h;
    m_dev->exactBounds(x, y, w, h);
    TQPixmap pix = TQPixmap(m_selection->maskImage().smoothScale(350, 350, TQ_ScaleMin));
    m_subject->canvasController()->updateCanvas();
    m_page->pixSelection->setPixmap(pix);
}

void DlgColorRange::okClicked()
{
    m_dev->setDirty();
    m_dev->emitSelectionChanged();

    if (m_dev->image()->undo()) m_subject->undoAdapter()->addCommand(m_transaction);
    accept();
}

void DlgColorRange::cancelClicked()
{
    if (m_dev->image()->undo()) m_transaction->unexecute();

    m_subject->canvasController()->updateCanvas();
    reject();
}

void DlgColorRange::slotInvertClicked()
{
    m_invert = m_page->chkInvert->isChecked();
}

void DlgColorRange::slotSelectionTypeChanged(int index)
{
    m_currentAction = (enumAction)index;
}

void DlgColorRange::slotSubtract(bool on)
{
    if (on)
        m_mode = SELECTION_SUBTRACT;
}
void DlgColorRange::slotAdd(bool on)
{
    if (on)
        m_mode = SELECTION_ADD;
}

void DlgColorRange::slotSelectClicked()
{
    TQApplication::setOverrideCursor(KisCursor::waitCursor());
    // XXX: Multithread this!
    TQ_INT32 x, y, w, h;
    m_dev->exactBounds(x, y, w, h);
    KisColorSpace * cs = m_dev->colorSpace();
    TQ_UINT8 opacity;
    for (int y2 = y; y2 < h - y; ++y2) {
        KisHLineIterator hiter = m_dev->createHLineIterator(x, y2, w, false);
        KisHLineIterator selIter = m_selection ->createHLineIterator(x, y2, w, true);
        while (!hiter.isDone()) {
            TQColor c;

            cs->toTQColor(hiter.rawData(), &c, &opacity);
            // Don't try to select transparent pixels.
            if (opacity > OPACITY_TRANSPARENT) {
                TQ_UINT8 match = matchColors(c, m_currentAction);

                if (match) {
                    // Personally, I think the invert option a bit silly. But it's possible I don't quite understand it. BSAR.
                    if (!m_invert) {
                        if (m_mode == SELECTION_ADD) {
                            *(selIter.rawData()) =  match;
                        }
                        else if (m_mode == SELECTION_SUBTRACT) {
                            TQ_UINT8 selectedness = *(selIter.rawData());
                            if (match < selectedness) {
                                *(selIter.rawData()) = selectedness - match;
                            }
                            else {
                                *(selIter.rawData()) = 0;
                            }
                        }
                    }
                    else {
                        if (m_mode == SELECTION_ADD) {
                            TQ_UINT8 selectedness = *(selIter.rawData());
                            if (match < selectedness) {
                                *(selIter.rawData()) = selectedness - match;
                            }
                            else {
                                *(selIter.rawData()) = 0;
                            }
                        }
                        else if (m_mode == SELECTION_SUBTRACT) {
                            *(selIter.rawData()) =  match;
                        }
                    }
                }
            }
            ++hiter;
            ++selIter;
        }
    }
    updatePreview();
    TQApplication::restoreOverrideCursor();
}

void DlgColorRange::slotDeselectClicked()
{
    m_dev->selection()->clear();
    updatePreview();
}


#include "dlg_colorrange.moc"