summaryrefslogtreecommitdiffstats
path: root/kolourpaint/kpcolor.cpp
blob: 2efa3c9e36b3b360b58dca6fa1b3bdf0e08d2672 (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
352
353
354
355
356
357
358
359
360

/*
   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 0


#include <kpcolor.h>

#include <tqdatastream.h>

#include <kdebug.h>


// public static
const int kpColor::Exact = 0;

// public static
const kpColor kpColor::invalid;  // TODO: what's wrong with explicitly specifying () constructor?
const kpColor kpColor::transparent (0, 0, 0, true/*isTransparent*/);


kpColor::kpColor ()
    : m_rgbaIsValid (false),
      m_colorCacheIsValid (false)
{
}

kpColor::kpColor (int red, int green, int blue, bool isTransparent)
    : m_colorCacheIsValid (false)
{
    if (red < 0 || red > 255 ||
        green < 0 || green > 255 ||
        blue < 0 || blue > 255)
    {
        kdError () << "kpColor::<ctor>(r=" << red
                   << ",g=" << green
                   << ",b=" << blue
                   << ",t=" << isTransparent
                   << ") passed out of range values" << endl;
        m_rgbaIsValid = false;
        return;
    }

    m_rgba = tqRgba (red, green, blue, isTransparent ? 0 : 255/*opaque*/);
    m_rgbaIsValid = true;
}

kpColor::kpColor (const TQRgb &rgba)
    : m_colorCacheIsValid (false)
{
    if (tqAlpha (rgba) > 0 && tqAlpha (rgba) < 255)
    {
        kdError () << "kpColor::<ctor>(TQRgb) passed translucent alpha "
                   << tqAlpha (rgba)
                   << " - trying to recover"
                   << endl;

        // Forget the alpha channel - make it opaque
        m_rgba = tqRgb (tqRed (m_rgba), tqGreen (m_rgba), tqBlue (m_rgba));
        m_rgbaIsValid = true;
    }
    else
    {
        m_rgba = rgba;
        m_rgbaIsValid = true;
    }
}

kpColor::kpColor (const kpColor &rhs)
    :  m_rgbaIsValid (rhs.m_rgbaIsValid),
       m_rgba (rhs.m_rgba),
       m_colorCacheIsValid (rhs.m_colorCacheIsValid),
       m_colorCache (rhs.m_colorCache)
{
}

// friend
TQDataStream &operator<< (TQDataStream &stream, const kpColor &color)
{
    stream << int (color.m_rgbaIsValid) << int (color.m_rgba);

    return stream;
}

// friend
TQDataStream &operator>> (TQDataStream &stream, kpColor &color)
{
    int a, b;
    stream >> a >> b;
    color.m_rgbaIsValid = a;
    color.m_rgba = b;

    color.m_colorCacheIsValid = false;

    return stream;
}

kpColor &kpColor::operator= (const kpColor &rhs)
{
    // (as soon as you add a ptr, you won't be complaining to me that this
    //  method was unnecessary :))

    if (this == &rhs)
        return *this;

    m_rgbaIsValid = rhs.m_rgbaIsValid;
    m_rgba = rhs.m_rgba;
    m_colorCacheIsValid = rhs.m_colorCacheIsValid;
    m_colorCache = rhs.m_colorCache;

    return *this;
}

bool kpColor::operator== (const kpColor &rhs) const
{
    return isSimilarTo (rhs, kpColor::Exact);
}

bool kpColor::operator!= (const kpColor &rhs) const
{
    return !(*this == rhs);
}


template <class dtype>
inline dtype square (dtype val)
{
    return val * val;
}

// public static
int kpColor::processSimilarity (double colorSimilarity)
{
    // sqrt (dr ^ 2 + dg ^ 2 + db ^ 2) <= colorSimilarity       * sqrt (255 ^ 2 * 3)
    //       dr ^ 2 + dg ^ 2 + db ^ 2  <= (colorSimilarity ^ 2) * (255 ^ 2 * 3)

    return int (square (colorSimilarity) * (square (255) * 3));
}

bool kpColor::isSimilarTo (const kpColor &rhs, int processedSimilarity) const
{
    // Are we the same?
    if (this == &rhs)
        return true;


    // Do we dither in terms of validity?
    if (isValid () != rhs.isValid ())
        return false;

    // Are both of us invalid?
    if (!isValid ())
        return true;

    // --- both are now valid ---


    if (isTransparent () != rhs.isTransparent ())
        return false;

    // Are both of us transparent?
    if (isTransparent ())
        return true;

    // --- both are now valid and opaque ---


    if (m_rgba == rhs.m_rgba)
        return true;


    if (processedSimilarity == kpColor::Exact)
        return false;
    else
    {
        return (square (tqRed (m_rgba) - tqRed (rhs.m_rgba)) +
                square (tqGreen (m_rgba) - tqGreen (rhs.m_rgba)) +
                square (tqBlue (m_rgba) - tqBlue (rhs.m_rgba))
                <= processedSimilarity);
    }
}

kpColor::~kpColor ()
{
}


// public
bool kpColor::isValid () const
{
    return m_rgbaIsValid;
}


// public
int kpColor::red () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::red() called with invalid kpColor" << endl;
        return 0;
    }

    if (isTransparent ())
    {
        kdError () << "kpColor::red() called with transparent kpColor" << endl;
        return 0;
    }

    return tqRed (m_rgba);
}

// public
int kpColor::green () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::green() called with invalid kpColor" << endl;
        return 0;
    }

    if (isTransparent ())
    {
        kdError () << "kpColor::green() called with transparent kpColor" << endl;
        return 0;
    }

    return tqGreen (m_rgba);
}

// public
int kpColor::blue () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::blue() called with invalid kpColor" << endl;
        return 0;
    }

    if (isTransparent ())
    {
        kdError () << "kpColor::blue() called with transparent kpColor" << endl;
        return 0;
    }

    return tqBlue (m_rgba);
}

// public
int kpColor::alpha () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::alpha() called with invalid kpColor" << endl;
        return 0;
    }

    const int alpha = tqAlpha (m_rgba);

    if (alpha > 0 && alpha < 255)
    {
        kdError () << "kpColor::alpha() called with translucent kpColor alpha=" << alpha << endl;

        // no translucency
        return alpha ? 255 : 0;
    }
    else
    {
        return alpha;
    }
}

// public
bool kpColor::isTransparent () const
{
    return (alpha () == 0);
}

// public
bool kpColor::isOpaque () const
{
    return (alpha () == 255);
}


// public
TQRgb kpColor::toTQRgb () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::toTQRgb() called with invalid kpColor" << endl;
        return 0;
    }

    return m_rgba;
}

// public
const TQColor &kpColor::toTQColor () const
{
    if (!m_rgbaIsValid)
    {
        kdError () << "kpColor::toTQColor() called with invalid kpColor" << endl;
        return TQt::black;
    }

    if (m_colorCacheIsValid)
        return m_colorCache;

    if (tqAlpha (m_rgba) < 255)
    {
        kdError () << "kpColor::toTQColor() called with not fully opaque kpColor alpha="
                   << tqAlpha (m_rgba)
                   << endl;
        return TQt::black;
    }

    m_colorCache = TQColor (m_rgba);
    if (!m_colorCache.isValid ())
    {
        kdError () << "kpColor::toTQColor () internal error - could not return valid TQColor"
                   << endl;
        return TQt::black;
    }

    m_colorCacheIsValid = true;

    return m_colorCache;
}

// public
TQColor kpColor::tqmaskColor () const
{
    return isTransparent () ? TQt::color0 : TQt::color1;
}