summaryrefslogtreecommitdiffstats
path: root/kolourpaint/widgets/kpcolorsimilaritycube.cpp
blob: b2f3441aee29caaa9e228e1889f87ca7b5e48552 (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

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define DEBUG_KP_COLOR_SIMILARITY_CUBE 0


#include <kpcolorsimilaritycube.h>

#include <math.h>

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqwhatsthis.h>

#include <kdebug.h>
#include <klocale.h>

#include <kpcolor.h>
#include <kpcolorsimilaritydialog.h>
#include <kpdefs.h>


const double kpColorSimilarityCube::colorCubeDiagonalDistance =
    sqrt (255 * 255 * 3);

kpColorSimilarityCube::kpColorSimilarityCube (int look,
                                              kpMainWindow *mainWindow,
                                              TQWidget *parent,
                                              const char *name)
    : TQFrame (parent, name, TQt::WNoAutoErase/*no flicker*/),
      m_mainWindow (mainWindow),
      m_colorSimilarity (-1)
{
    if (look & Depressed)
        setFrameStyle (TQFrame::Panel | TQFrame::Sunken);

    setColorSimilarity (0);


    // Don't cause the translators grief by appending strings
    // - duplicate text with 2 cases

    if (look & DoubleClickInstructions)
    {
        TQWhatsThis::add (this,
            i18n ("<qt><p><b>Color Similarity</b> is how close "
                  "colors must be in the RGB Color Cube "
                  "to be considered the same.</p>"

                  "<p>If you set it to something "
                  "other than <b>Exact</b>, "
                  "you can work more effectively with dithered "
                  "images and photos.</p>"

                  "<p>This feature applies to transparent selections, as well as "
                  "the Flood Fill, Color Eraser and Autocrop "
                  "tools.</p>"

                  // sync: different to else case
                  "<p>To configure it, double click on the cube.</p>"

                  "</qt>"));
    }
    else
    {
        TQWhatsThis::add (this,
            i18n ("<qt><p><b>Color Similarity</b> is how close "
                  "colors must be in the RGB Color Cube "
                  "to be considered the same.</p>"

                  "<p>If you set it to something "
                  "other than <b>Exact</b>, "
                  "you can work more effectively with dithered "
                  "images and photos.</p>"

                  "<p>This feature applies to transparent selections, as well as "
                  "the Flood Fill, Color Eraser and Autocrop "
                  "tools.</p>"

                  "</qt>"));
    }
}

kpColorSimilarityCube::~kpColorSimilarityCube ()
{
}


// public
double kpColorSimilarityCube::colorSimilarity () const
{
    return m_colorSimilarity;
}

// public
void kpColorSimilarityCube::setColorSimilarity (double similarity)
{
#if DEBUG_KP_COLOR_SIMILARITY_CUBE
    kdDebug () << "kpColorSimilarityCube::setColorSimilarity(" << similarity << ")" << endl;
#endif

    if (m_colorSimilarity == similarity)
        return;

    if (similarity < 0)
        similarity = 0;
    else if (similarity > kpColorSimilarityDialog::maximumColorSimilarity)
        similarity = kpColorSimilarityDialog::maximumColorSimilarity;

    m_colorSimilarity = similarity;

    tqrepaint (false/*no erase*/);
}


// protected virtual [base TQWidget]
TQSize kpColorSimilarityCube::tqsizeHint () const
{
    return TQSize (52, 52);
}


// protected
TQColor kpColorSimilarityCube::color (int redOrGreenOrBlue,
                                     int baseBrightness,
                                     int similarityDirection) const
{
    int brightness = int (baseBrightness +
                          similarityDirection *
                              .5 * m_colorSimilarity * kpColorSimilarityCube::colorCubeDiagonalDistance);

    if (brightness < 0)
        brightness = 0;
    else if (brightness > 255)
        brightness = 255;

    switch (redOrGreenOrBlue)
    {
    default:
    case 0: return TQColor (brightness, 0, 0);
    case 1: return TQColor (0, brightness, 0);
    case 2: return TQColor (0, 0, brightness);
    }
}

static TQPoint pointBetween (const TQPoint &p, const TQPoint &q)
{
    return TQPoint ((p.x () + q.x ()) / 2, (p.y () + q.y ()) / 2);
}

static void drawQuadrant (TQPainter *p,
                          const TQColor &col,
                          const TQPoint &p1, const TQPoint &p2, const TQPoint &p3,
                          const TQPoint pointNotOnOutline)
{
    p->save ();


    TQPointArray points (4);
    points [0] = p1;
    points [1] = p2;
    points [2] = p3;
    points [3] = pointNotOnOutline;

    p->setPen (col);
    p->setBrush (col);
    p->drawPolygon (points);


    points.resize (3);

    p->setPen (TQt::black);
    p->setBrush (TQt::NoBrush);
    p->drawPolyline (points);


    p->restore ();
}

// protected
void kpColorSimilarityCube::drawFace (TQPainter *p,
                                      int redOrGreenOrBlue,
                                      const TQPoint &tl, const TQPoint &tr,
                                      const TQPoint &bl, const TQPoint &br)
{
#if DEBUG_KP_COLOR_SIMILARITY_CUBE
    kdDebug () << "kpColorSimilarityCube(RorGorB=" << redOrGreenOrBlue
               << ",tl=" << tl
               << ",tr=" << tr
               << ",bl=" << bl
               << ",br=" << br
               << ")"
               << endl;
#endif

    //  tl --- tm --- tr
    //  |      |       |
    //  |      |       |
    //  ml --- mm --- mr
    //  |      |       |
    //  |      |       |
    //  bl --- bm --- br

    const TQPoint tm (::pointBetween (tl, tr));
    const TQPoint bm (::pointBetween (bl, br));

    const TQPoint ml (::pointBetween (tl, bl));
    const TQPoint mr (::pointBetween (tr, br));
    const TQPoint mm (::pointBetween (ml, mr));


    const int baseBrightness = TQMAX (127,
                                     255 - int (kpColorSimilarityDialog::maximumColorSimilarity *
                                                kpColorSimilarityCube::colorCubeDiagonalDistance / 2));
    TQColor colors [2] =
    {
        color (redOrGreenOrBlue, baseBrightness, -1),
        color (redOrGreenOrBlue, baseBrightness, +1)
    };

    if (!isEnabled ())
    {
    #if DEBUG_KP_COLOR_SIMILARITY_CUBE
        kdDebug () << "\tnot enabled - making us grey" << endl;
    #endif
        colors [0] = tqcolorGroup ().background ();
        colors [1] = tqcolorGroup ().background ();
    }

#if DEBUG_KP_COLOR_SIMILARITY_CUBE
    kdDebug () << "\tmaxColorSimilarity=" << kpColorSimilarityDialog::maximumColorSimilarity
               << " colorCubeDiagDist=" << kpColorSimilarityCube::colorCubeDiagonalDistance
               << endl
               << "\tbaseBrightness=" << baseBrightness
               << " color[0]=" << ((colors [0].rgb () & TQRGB_MASK) >> ((2 - redOrGreenOrBlue) * 8))
               << " color[1]=" << ((colors [1].rgb () & TQRGB_MASK) >> ((2 - redOrGreenOrBlue) * 8))
               << endl;
#endif


    ::drawQuadrant (p, colors [0], tm, tl, ml, mm);
    ::drawQuadrant (p, colors [1], tm, tr, mr, mm);
    ::drawQuadrant (p, colors [1], ml, bl, bm, mm);
    ::drawQuadrant (p, colors [0], bm, br, mr, mm);
}

// protected virtual [base TQFrame]
void kpColorSimilarityCube::drawContents (TQPainter *p)
{
    TQRect cr (contentsRect ());

    TQPixmap backBuffer (cr.width (), cr.height ());
    backBuffer.fill (tqcolorGroup ().background ());

    TQPainter backBufferPainter (&backBuffer);

    int cubeRectSize = TQMIN (cr.width () * 6 / 8, cr.height () * 6 / 8);
    int dx = (cr.width () - cubeRectSize) / 2,
        dy = (cr.height () - cubeRectSize) / 2;
    backBufferPainter.translate (dx, dy);

    //
    //      P------- Q  ---  ---
    //     /       / |   |    |
    //    /A      /  |  side  |
    //   R-------S   T  --- cubeRectSize
    //   |       |  /   /     |
    // S |       | /  side    |
    //   U-------V   ---     ---
    //   |-------|
    //      side
    //   |-----------|
    //    cubeRectSize
    //
    //

    const double angle = KP_DEGREES_TO_RADIANS (45);
    // S + S sin A = cubeRectSize
    // (1 + sin A) x S = cubeRectSize
    const double side = double (cubeRectSize) / (1 + sin (angle));


    const TQPoint pointP ((int) (side * cos (angle)), 0);
    const TQPoint pointQ ((int) (side * cos (angle) + side), 0);
    const TQPoint pointR (0, (int) (side * sin (angle)));
    const TQPoint pointS ((int) (side), (int) (side * sin (angle)));
    const TQPoint pointU (0, (int) (side * sin (angle) + side));
    const TQPoint pointT ((int) (side + side * cos (angle)), (int) (side));
    const TQPoint pointV ((int) (side), (int) (side * sin (angle) + side));


    // Top Face
    drawFace (&backBufferPainter,
              0/*red*/,
              pointP, pointQ,
              pointR, pointS);


    // Bottom Face
    drawFace (&backBufferPainter,
              1/*green*/,
              pointR, pointS,
              pointU, pointV);


    // Right Face
    drawFace (&backBufferPainter,
              2/*blue*/,
              pointS, pointQ,
              pointV, pointT);


#if 0
    backBufferPainter.save ();
    backBufferPainter.setPen (TQt::cyan);
    backBufferPainter.drawRect (0, 0, cubeRectSize, cubeRectSize);
    backBufferPainter.restore ();
#endif


    backBufferPainter.end ();

    p->drawPixmap (cr, backBuffer);
}