Changeset View
Changeset View
Standalone View
Standalone View
imagelib/kpColor.cpp
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Line(s) | |||||
45 | } | 45 | } | ||
46 | 46 | | |||
47 | //--------------------------------------------------------------------- | 47 | //--------------------------------------------------------------------- | ||
48 | 48 | | |||
49 | kpColor::kpColor (int red, int green, int blue, bool isTransparent) | 49 | kpColor::kpColor (int red, int green, int blue, bool isTransparent) | ||
50 | : m_rgba(0), m_colorCacheIsValid(false) | 50 | : m_rgba(0), m_colorCacheIsValid(false) | ||
51 | { | 51 | { | ||
52 | qCDebug(kpLogImagelib) << "kpColor::<ctor>(r=" << red << ",g=" << green << ",b=" << blue | 52 | qCDebug(kpLogImagelib) << "kpColor::<ctor>(r=" << red << ",g=" << green << ",b=" << blue | ||
53 | << ",isTrans=" << isTransparent << ")" << endl; | 53 | << ",isTrans=" << isTransparent << ")"; | ||
54 | if (red < 0 || red > 255 || | 54 | if (red < 0 || red > 255 || | ||
55 | green < 0 || green > 255 || | 55 | green < 0 || green > 255 || | ||
56 | blue < 0 || blue > 255) | 56 | blue < 0 || blue > 255) | ||
57 | { | 57 | { | ||
58 | qCCritical(kpLogImagelib) << "kpColor::<ctor>(r=" << red | 58 | qCCritical(kpLogImagelib) << "kpColor::<ctor>(r=" << red | ||
59 | << ",g=" << green | 59 | << ",g=" << green | ||
60 | << ",b=" << blue | 60 | << ",b=" << blue | ||
61 | << ",t=" << isTransparent | 61 | << ",t=" << isTransparent | ||
62 | << ") passed out of range values" << endl; | 62 | << ") passed out of range values"; | ||
63 | m_rgbaIsValid = false; | 63 | m_rgbaIsValid = false; | ||
64 | return; | 64 | return; | ||
65 | } | 65 | } | ||
66 | 66 | | |||
67 | m_rgba = qRgba (red, green, blue, isTransparent ? 0 : 255/*opaque*/); | 67 | m_rgba = qRgba (red, green, blue, isTransparent ? 0 : 255/*opaque*/); | ||
68 | m_rgbaIsValid = true; | 68 | m_rgbaIsValid = true; | ||
69 | } | 69 | } | ||
70 | 70 | | |||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
116 | 116 | | |||
117 | //--------------------------------------------------------------------- | 117 | //--------------------------------------------------------------------- | ||
118 | 118 | | |||
119 | kpColor &kpColor::operator= (const kpColor &rhs) | 119 | kpColor &kpColor::operator= (const kpColor &rhs) | ||
120 | { | 120 | { | ||
121 | // (as soon as you add a ptr, you won't be complaining to me that this | 121 | // (as soon as you add a ptr, you won't be complaining to me that this | ||
122 | // method was unnecessary :)) | 122 | // method was unnecessary :)) | ||
123 | 123 | | |||
124 | if (this == &rhs) | 124 | if (this == &rhs) { | ||
125 | return *this; | 125 | return *this; | ||
126 | } | ||||
126 | 127 | | |||
127 | m_rgbaIsValid = rhs.m_rgbaIsValid; | 128 | m_rgbaIsValid = rhs.m_rgbaIsValid; | ||
128 | m_rgba = rhs.m_rgba; | 129 | m_rgba = rhs.m_rgba; | ||
129 | m_colorCacheIsValid = rhs.m_colorCacheIsValid; | 130 | m_colorCacheIsValid = rhs.m_colorCacheIsValid; | ||
130 | m_colorCache = rhs.m_colorCache; | 131 | m_colorCache = rhs.m_colorCache; | ||
131 | 132 | | |||
132 | return *this; | 133 | return *this; | ||
133 | } | 134 | } | ||
Show All 28 Lines | 159 | { | |||
162 | return int (square (colorSimilarity) * (square (255) * 3)); | 163 | return int (square (colorSimilarity) * (square (255) * 3)); | ||
163 | } | 164 | } | ||
164 | 165 | | |||
165 | //--------------------------------------------------------------------- | 166 | //--------------------------------------------------------------------- | ||
166 | 167 | | |||
167 | bool kpColor::isSimilarTo (const kpColor &rhs, int processedSimilarity) const | 168 | bool kpColor::isSimilarTo (const kpColor &rhs, int processedSimilarity) const | ||
168 | { | 169 | { | ||
169 | // Are we the same? | 170 | // Are we the same? | ||
170 | if (this == &rhs) | 171 | if (this == &rhs) { | ||
171 | return true; | 172 | return true; | ||
173 | } | ||||
172 | 174 | | |||
173 | 175 | | |||
174 | // Do we dither in terms of validity? | 176 | // Do we dither in terms of validity? | ||
175 | if (isValid () != rhs.isValid ()) | 177 | if (isValid () != rhs.isValid ()) { | ||
176 | return false; | 178 | return false; | ||
179 | } | ||||
177 | 180 | | |||
178 | // Are both of us invalid? | 181 | // Are both of us invalid? | ||
179 | if (!isValid ()) | 182 | if (!isValid ()) { | ||
180 | return true; | 183 | return true; | ||
184 | } | ||||
181 | 185 | | |||
182 | // --- both are now valid --- | 186 | // --- both are now valid --- | ||
183 | 187 | | |||
184 | if (m_rgba == rhs.m_rgba) | 188 | if (m_rgba == rhs.m_rgba) { | ||
185 | return true; | 189 | return true; | ||
190 | } | ||||
186 | 191 | | |||
187 | if (processedSimilarity == kpColor::Exact) | 192 | if (processedSimilarity == kpColor::Exact) { | ||
188 | return false; | 193 | return false; | ||
189 | else | 194 | } | ||
190 | { | 195 | | ||
196 | | ||||
191 | return (square (qRed (m_rgba) - qRed (rhs.m_rgba)) + | 197 | return (square (qRed (m_rgba) - qRed (rhs.m_rgba)) + | ||
192 | square (qGreen (m_rgba) - qGreen (rhs.m_rgba)) + | 198 | square (qGreen (m_rgba) - qGreen (rhs.m_rgba)) + | ||
193 | square (qBlue (m_rgba) - qBlue (rhs.m_rgba)) | 199 | square (qBlue (m_rgba) - qBlue (rhs.m_rgba)) | ||
194 | <= processedSimilarity); | 200 | <= processedSimilarity); | ||
195 | } | 201 | | ||
196 | } | 202 | } | ||
197 | 203 | | |||
198 | //--------------------------------------------------------------------- | 204 | //--------------------------------------------------------------------- | ||
199 | 205 | | |||
200 | // public | 206 | // public | ||
201 | bool kpColor::isValid () const | 207 | bool kpColor::isValid () const | ||
202 | { | 208 | { | ||
203 | return m_rgbaIsValid; | 209 | return m_rgbaIsValid; | ||
204 | } | 210 | } | ||
205 | 211 | | |||
206 | //--------------------------------------------------------------------- | 212 | //--------------------------------------------------------------------- | ||
207 | 213 | | |||
208 | // public | 214 | // public | ||
209 | int kpColor::red () const | 215 | int kpColor::red () const | ||
210 | { | 216 | { | ||
211 | if (!m_rgbaIsValid) | 217 | if (!m_rgbaIsValid) | ||
212 | { | 218 | { | ||
213 | qCCritical(kpLogImagelib) << "kpColor::red() called with invalid kpColor" << endl; | 219 | qCCritical(kpLogImagelib) << "kpColor::red() called with invalid kpColor"; | ||
214 | return 0; | 220 | return 0; | ||
215 | } | 221 | } | ||
216 | 222 | | |||
217 | return qRed (m_rgba); | 223 | return qRed (m_rgba); | ||
218 | } | 224 | } | ||
219 | 225 | | |||
220 | //--------------------------------------------------------------------- | 226 | //--------------------------------------------------------------------- | ||
221 | 227 | | |||
222 | // public | 228 | // public | ||
223 | int kpColor::green () const | 229 | int kpColor::green () const | ||
224 | { | 230 | { | ||
225 | if (!m_rgbaIsValid) | 231 | if (!m_rgbaIsValid) | ||
226 | { | 232 | { | ||
227 | qCCritical(kpLogImagelib) << "kpColor::green() called with invalid kpColor" << endl; | 233 | qCCritical(kpLogImagelib) << "kpColor::green() called with invalid kpColor"; | ||
228 | return 0; | 234 | return 0; | ||
229 | } | 235 | } | ||
230 | 236 | | |||
231 | return qGreen (m_rgba); | 237 | return qGreen (m_rgba); | ||
232 | } | 238 | } | ||
233 | 239 | | |||
234 | //--------------------------------------------------------------------- | 240 | //--------------------------------------------------------------------- | ||
235 | 241 | | |||
236 | // public | 242 | // public | ||
237 | int kpColor::blue () const | 243 | int kpColor::blue () const | ||
238 | { | 244 | { | ||
239 | if (!m_rgbaIsValid) | 245 | if (!m_rgbaIsValid) | ||
240 | { | 246 | { | ||
241 | qCCritical(kpLogImagelib) << "kpColor::blue() called with invalid kpColor" << endl; | 247 | qCCritical(kpLogImagelib) << "kpColor::blue() called with invalid kpColor"; | ||
242 | return 0; | 248 | return 0; | ||
243 | } | 249 | } | ||
244 | 250 | | |||
245 | return qBlue (m_rgba); | 251 | return qBlue (m_rgba); | ||
246 | } | 252 | } | ||
247 | 253 | | |||
248 | //--------------------------------------------------------------------- | 254 | //--------------------------------------------------------------------- | ||
249 | 255 | | |||
250 | // public | 256 | // public | ||
251 | int kpColor::alpha () const | 257 | int kpColor::alpha () const | ||
252 | { | 258 | { | ||
253 | if (!m_rgbaIsValid) | 259 | if (!m_rgbaIsValid) | ||
254 | { | 260 | { | ||
255 | qCCritical(kpLogImagelib) << "kpColor::alpha() called with invalid kpColor" << endl; | 261 | qCCritical(kpLogImagelib) << "kpColor::alpha() called with invalid kpColor"; | ||
256 | return 0; | 262 | return 0; | ||
257 | } | 263 | } | ||
258 | 264 | | |||
259 | return qAlpha (m_rgba); | 265 | return qAlpha (m_rgba); | ||
260 | } | 266 | } | ||
261 | 267 | | |||
262 | //--------------------------------------------------------------------- | 268 | //--------------------------------------------------------------------- | ||
263 | 269 | | |||
264 | // public | 270 | // public | ||
265 | bool kpColor::isTransparent () const | 271 | bool kpColor::isTransparent () const | ||
266 | { | 272 | { | ||
267 | return (alpha () == 0); | 273 | return (alpha () == 0); | ||
268 | } | 274 | } | ||
269 | 275 | | |||
270 | //--------------------------------------------------------------------- | 276 | //--------------------------------------------------------------------- | ||
271 | 277 | | |||
272 | // public | 278 | // public | ||
273 | QRgb kpColor::toQRgb () const | 279 | QRgb kpColor::toQRgb () const | ||
274 | { | 280 | { | ||
275 | if (!m_rgbaIsValid) | 281 | if (!m_rgbaIsValid) | ||
276 | { | 282 | { | ||
277 | qCCritical(kpLogImagelib) << "kpColor::toQRgb() called with invalid kpColor" << endl; | 283 | qCCritical(kpLogImagelib) << "kpColor::toQRgb() called with invalid kpColor"; | ||
278 | return 0; | 284 | return 0; | ||
279 | } | 285 | } | ||
280 | 286 | | |||
281 | return m_rgba; | 287 | return m_rgba; | ||
282 | } | 288 | } | ||
283 | 289 | | |||
284 | //--------------------------------------------------------------------- | 290 | //--------------------------------------------------------------------- | ||
285 | 291 | | |||
286 | // public | 292 | // public | ||
287 | QColor kpColor::toQColor () const | 293 | QColor kpColor::toQColor () const | ||
288 | { | 294 | { | ||
289 | if (!m_rgbaIsValid) | 295 | if (!m_rgbaIsValid) | ||
290 | { | 296 | { | ||
291 | qCCritical(kpLogImagelib) << "kpColor::toQColor() called with invalid kpColor" << endl; | 297 | qCCritical(kpLogImagelib) << "kpColor::toQColor() called with invalid kpColor"; | ||
292 | return Qt::black; | 298 | return Qt::black; | ||
293 | } | 299 | } | ||
294 | 300 | | |||
295 | if (m_colorCacheIsValid) | 301 | if (m_colorCacheIsValid) { | ||
296 | return m_colorCache; | 302 | return m_colorCache; | ||
303 | } | ||||
297 | 304 | | |||
298 | m_colorCache = QColor(qRed(m_rgba), qGreen(m_rgba), qBlue(m_rgba), qAlpha(m_rgba)); | 305 | m_colorCache = QColor(qRed(m_rgba), qGreen(m_rgba), qBlue(m_rgba), qAlpha(m_rgba)); | ||
299 | m_colorCacheIsValid = true; | 306 | m_colorCacheIsValid = true; | ||
300 | 307 | | |||
301 | return m_colorCache; | 308 | return m_colorCache; | ||
302 | } | 309 | } | ||
303 | 310 | | |||
304 | //--------------------------------------------------------------------- | 311 | //--------------------------------------------------------------------- |