Rev 4430 | Rev 4608 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
2886 | fschmid | 7 | /*************************************************************************** |
8 | sccolor.cpp - description |
||
9 | ------------------- |
||
10 | begin : Sun Sep 9 2001 |
||
11 | copyright : (C) 2001 by Franz Schmid |
||
12 | email : Franz.Schmid@altmuehlnet.de |
||
13 | ***************************************************************************/ |
||
14 | |||
15 | /*************************************************************************** |
||
16 | * * |
||
17 | * This program is free software; you can redistribute it and/or modify * |
||
18 | * it under the terms of the GNU General Public License as published by * |
||
19 | * the Free Software Foundation; either version 2 of the License, or * |
||
20 | * (at your option) any later version. * |
||
21 | * * |
||
22 | ***************************************************************************/ |
||
23 | |||
24 | #include <qcolor.h> |
||
25 | #include <qstring.h> |
||
26 | |||
27 | #include "sccolor.h" |
||
28 | |||
29 | #include "scconfig.h" |
||
30 | |||
31 | #ifdef HAVE_CMS |
||
32 | #include CMS_INC |
||
33 | extern cmsHTRANSFORM stdTransG; |
||
34 | extern cmsHTRANSFORM stdProofG; |
||
35 | extern cmsHTRANSFORM stdTransCMYKG; |
||
36 | extern cmsHTRANSFORM stdProofCMYKG; |
||
37 | extern cmsHTRANSFORM stdTransRGBG; |
||
3046 | fschmid | 38 | extern cmsHTRANSFORM stdProofGCG; |
39 | extern cmsHTRANSFORM stdProofCMYKGCG; |
||
2886 | fschmid | 40 | extern bool SoftProofing; |
41 | extern bool CMSuse; |
||
42 | #endif |
||
43 | extern bool CMSavail; |
||
44 | bool ScColor::UseProf = true; |
||
45 | |||
46 | ScColor::ScColor() |
||
47 | { |
||
3490 | avox | 48 | // setColor(0, 0, 0, 0); |
49 | Model = colorModelCMYK; |
||
50 | C = M = Y = K = 0; |
||
51 | R = G = B = 255; |
||
52 | RGB = Qt::white; |
||
53 | |||
2886 | fschmid | 54 | Spot = false; |
3041 | fschmid | 55 | outOfGamutFlag = false; |
3060 | fschmid | 56 | Regist = false; |
2886 | fschmid | 57 | } |
58 | |||
59 | ScColor::ScColor(int c, int m, int y, int k) |
||
60 | { |
||
61 | setColor(c, m, y, k); |
||
62 | Spot = false; |
||
3041 | fschmid | 63 | outOfGamutFlag = false; |
3060 | fschmid | 64 | Regist = false; |
2886 | fschmid | 65 | } |
66 | |||
67 | ScColor::ScColor(int r, int g, int b) |
||
68 | { |
||
69 | setColorRGB(r, g, b); |
||
70 | Spot = false; |
||
3041 | fschmid | 71 | outOfGamutFlag = false; |
3060 | fschmid | 72 | Regist = false; |
2886 | fschmid | 73 | } |
74 | |||
75 | void ScColor::setColor(int c, int m, int y, int k) |
||
76 | { |
||
77 | C = c; |
||
78 | M = m; |
||
79 | Y = y; |
||
80 | K = k; |
||
81 | Model = colorModelCMYK; |
||
82 | RecalcRGB(); |
||
83 | } |
||
84 | |||
85 | void ScColor::setColorRGB(int r, int g, int b) |
||
86 | { |
||
87 | R = r; |
||
88 | G = g; |
||
89 | B = b; |
||
90 | Model = colorModelRGB; |
||
91 | RecalcRGB(); |
||
92 | } |
||
93 | |||
94 | void ScColor::setColorModel (colorModel cm) |
||
95 | { |
||
96 | Model = cm; |
||
97 | RecalcRGB(); |
||
98 | } |
||
99 | |||
100 | colorModel ScColor::getColorModel () |
||
101 | { |
||
102 | return Model; |
||
103 | } |
||
104 | |||
105 | void ScColor::fromQColor(QColor color) |
||
106 | { |
||
107 | int r, g, b; |
||
108 | color.rgb(&r, &g, &b); |
||
109 | setColorRGB(r, g, b); |
||
110 | } |
||
111 | |||
112 | QColor ScColor::getRGBColor() |
||
113 | { |
||
114 | return RGB; |
||
115 | } |
||
116 | |||
117 | void ScColor::getShadeColorCMYK(int *c, int *m, int *y, int *k, int level) |
||
118 | { |
||
119 | if (Model == colorModelRGB) |
||
120 | { |
||
121 | int r, g, b; |
||
122 | ScColor tmpR; |
||
123 | getShadeColorRGB(&r, &g, &b, level); |
||
124 | tmpR.setColorRGB(r, g, b); |
||
125 | tmpR.getCMYK(c, m, y, k); |
||
126 | } |
||
127 | else |
||
128 | { |
||
129 | *c = C * level / 100; |
||
130 | *m = M * level / 100; |
||
131 | *y = Y * level / 100; |
||
132 | *k = K * level / 100; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | void ScColor::getShadeColorRGB(int *r, int *g, int *b, int level) |
||
137 | { |
||
138 | int h, s, v, snew; |
||
139 | |||
140 | if (Model == colorModelCMYK) |
||
141 | { |
||
142 | int c, m, y, k; |
||
143 | getShadeColorCMYK(&c, &m, &y, &k, level); |
||
144 | ScColor tmpC(c, m, y, k); |
||
145 | tmpC.getRGB(r, g, b); |
||
146 | } |
||
147 | else |
||
148 | { |
||
149 | QColor tmpR(R, G, B); |
||
150 | tmpR.hsv(&h, &s, &v); |
||
151 | |||
152 | if (R == G && G == B) |
||
153 | { |
||
154 | snew = 255 - ((255 - v) * level / 100); |
||
155 | tmpR.setHsv(h, s, snew); |
||
156 | } |
||
157 | else |
||
158 | { |
||
159 | snew = s * level / 100; |
||
160 | tmpR.setHsv(h, snew, v); |
||
161 | } |
||
162 | |||
163 | tmpR.getRgb(r, g, b); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | QColor ScColor::getShadeColorProof(int level) |
||
168 | { |
||
169 | QColor tmp; |
||
170 | ScColor tmp2; |
||
171 | int r, g, b, c, m ,y, k; |
||
172 | |||
173 | if (Model == colorModelRGB) |
||
174 | { |
||
175 | getShadeColorRGB(&r, &g, &b, level); |
||
176 | tmp2.setColorRGB(r, g, b); |
||
177 | } |
||
178 | else |
||
179 | { |
||
180 | getShadeColorCMYK(&c, &m, &y, &k, level); |
||
181 | tmp2.setColor(c, m, y, k); |
||
182 | } |
||
183 | |||
184 | return tmp2.getRGBColor(); |
||
185 | } |
||
186 | |||
187 | void ScColor::getRawRGBColor(int *r, int *g, int *b) |
||
188 | { |
||
189 | *r = 255-QMIN(255, C+K); |
||
190 | *g = 255-QMIN(255, M+K); |
||
191 | *b = 255-QMIN(255, Y+K); |
||
192 | } |
||
193 | |||
3046 | fschmid | 194 | QColor ScColor::getRawRGBColor() |
195 | { |
||
196 | return QColor(255-QMIN(255, C+K), 255-QMIN(255, M+K), 255-QMIN(255, Y+K)); |
||
197 | } |
||
198 | |||
2886 | fschmid | 199 | void ScColor::getRGB(int *r, int *g, int *b) |
200 | { |
||
201 | *r = R; |
||
202 | *g = G; |
||
203 | *b = B; |
||
204 | } |
||
205 | |||
206 | void ScColor::getCMYK(int *c, int *m, int *y, int *k) |
||
207 | { |
||
208 | *c = C; |
||
209 | *m = M; |
||
210 | *y = Y; |
||
211 | *k = K; |
||
212 | } |
||
213 | |||
214 | void ScColor::applyGCR() |
||
215 | { |
||
216 | #ifdef HAVE_CMS |
||
217 | if (!(CMSuse && CMSavail)) |
||
218 | { |
||
219 | #endif |
||
220 | int k = QMIN(QMIN(C, M), Y); |
||
221 | C = C - k; |
||
222 | M = M - k; |
||
223 | Y = Y - k; |
||
224 | K = QMIN((K + k), 255); |
||
225 | #ifdef HAVE_CMS |
||
226 | } |
||
227 | #endif |
||
228 | } |
||
229 | |||
230 | QString ScColor::name() |
||
231 | { |
||
232 | QString tmp, name="#"; |
||
233 | switch (Model) |
||
234 | { |
||
235 | case colorModelCMYK: |
||
236 | tmp.setNum(C, 16); |
||
237 | if (tmp.length() < 2) |
||
238 | tmp.insert(0, "0"); |
||
239 | name += tmp; |
||
240 | tmp.setNum(M, 16); |
||
241 | if (tmp.length() < 2) |
||
242 | tmp.insert(0, "0"); |
||
243 | name += tmp; |
||
244 | tmp.setNum(Y, 16); |
||
245 | if (tmp.length() < 2) |
||
246 | tmp.insert(0, "0"); |
||
247 | name += tmp; |
||
248 | tmp.setNum(K, 16); |
||
249 | if (tmp.length() < 2) |
||
250 | tmp.insert(0, "0"); |
||
251 | name += tmp; |
||
252 | return name; |
||
253 | break; |
||
254 | case colorModelRGB: |
||
255 | tmp.setNum(R, 16); |
||
256 | if (tmp.length() < 2) |
||
257 | tmp.insert(0, "0"); |
||
258 | name += tmp; |
||
259 | tmp.setNum(G, 16); |
||
260 | if (tmp.length() < 2) |
||
261 | tmp.insert(0, "0"); |
||
262 | name += tmp; |
||
263 | tmp.setNum(B, 16); |
||
264 | if (tmp.length() < 2) |
||
265 | tmp.insert(0, "0"); |
||
266 | name += tmp; |
||
267 | return name; |
||
268 | } |
||
269 | return ""; |
||
270 | } |
||
271 | |||
4535 | fschmid | 272 | QString ScColor::nameCMYK() |
273 | { |
||
274 | QString tmp, name="#"; |
||
275 | tmp.setNum(C, 16); |
||
276 | if (tmp.length() < 2) |
||
277 | tmp.insert(0, "0"); |
||
278 | name += tmp; |
||
279 | tmp.setNum(M, 16); |
||
280 | if (tmp.length() < 2) |
||
281 | tmp.insert(0, "0"); |
||
282 | name += tmp; |
||
283 | tmp.setNum(Y, 16); |
||
284 | if (tmp.length() < 2) |
||
285 | tmp.insert(0, "0"); |
||
286 | name += tmp; |
||
287 | tmp.setNum(K, 16); |
||
288 | if (tmp.length() < 2) |
||
289 | tmp.insert(0, "0"); |
||
290 | name += tmp; |
||
291 | return name; |
||
292 | } |
||
293 | |||
294 | QString ScColor::nameRGB() |
||
295 | { |
||
296 | QString tmp, name="#"; |
||
297 | tmp.setNum(R, 16); |
||
298 | if (tmp.length() < 2) |
||
299 | tmp.insert(0, "0"); |
||
300 | name += tmp; |
||
301 | tmp.setNum(G, 16); |
||
302 | if (tmp.length() < 2) |
||
303 | tmp.insert(0, "0"); |
||
304 | name += tmp; |
||
305 | tmp.setNum(B, 16); |
||
306 | if (tmp.length() < 2) |
||
307 | tmp.insert(0, "0"); |
||
308 | name += tmp; |
||
309 | return name; |
||
310 | } |
||
311 | |||
2886 | fschmid | 312 | void ScColor::setNamedColor(QString name) |
313 | { |
||
314 | bool ok; |
||
315 | if (name.length () == 9) |
||
316 | { |
||
317 | int c = name.mid(1,2).toInt(&ok, 16); |
||
318 | int m = name.mid(3,2).toInt(&ok, 16); |
||
319 | int y = name.mid(5,2).toInt(&ok, 16); |
||
320 | int k = name.mid(7,2).toInt(&ok, 16); |
||
321 | setColor(c, m, y, k); |
||
322 | } |
||
323 | else if (name.length () == 7) |
||
324 | { |
||
325 | int r = name.mid(1,2).toInt(&ok, 16); |
||
326 | int g = name.mid(3,2).toInt(&ok, 16); |
||
327 | int b = name.mid(5,2).toInt(&ok, 16); |
||
328 | setColorRGB(r, g, b); |
||
329 | } |
||
330 | } |
||
331 | |||
3041 | fschmid | 332 | bool ScColor::isOutOfGamut() |
333 | { |
||
334 | return outOfGamutFlag; |
||
335 | } |
||
336 | |||
3046 | fschmid | 337 | void ScColor::checkGamut() |
338 | { |
||
339 | outOfGamutFlag = false; |
||
3057 | fschmid | 340 | if (Spot) |
341 | return; |
||
3046 | fschmid | 342 | #ifdef HAVE_CMS |
343 | WORD inC[4]; |
||
344 | WORD outC[4]; |
||
345 | if (CMSuse && CMSavail) |
||
346 | { |
||
347 | bool alert = true; |
||
348 | cmsHTRANSFORM xformProof; |
||
349 | if (Model == colorModelRGB) |
||
350 | { |
||
351 | inC[0] = R*257; |
||
352 | inC[1] = G*257; |
||
353 | inC[2] = B*257; |
||
354 | xformProof = stdProofGCG; |
||
355 | if ((R == 0) && (B == 0) && (G == 255)) |
||
356 | alert = false; |
||
3052 | fschmid | 357 | if ((R == G && G == B)) |
358 | alert = false; |
||
3046 | fschmid | 359 | } |
360 | else |
||
361 | { |
||
362 | inC[0] = C*257; |
||
363 | inC[1] = M*257; |
||
364 | inC[2] = Y*257; |
||
365 | inC[3] = K*257; |
||
366 | xformProof = stdProofCMYKGCG; |
||
3051 | fschmid | 367 | if ((M == 0) && (K == 0) && (C == 255) && (Y == 255)) |
3046 | fschmid | 368 | alert = false; |
3052 | fschmid | 369 | if ((M == 0) && (C == 0) && (Y == 0)) |
370 | alert = false; |
||
371 | if ((M == C) && (C == Y) && (Y == K)) |
||
372 | alert = false; |
||
3046 | fschmid | 373 | } |
3052 | fschmid | 374 | if (alert) |
375 | { |
||
376 | cmsDoTransform(xformProof, inC, outC, 1); |
||
377 | if ((alert) && ((outC[0]/257 == 0) && (outC[1]/257 == 255) & (outC[2]/257 == 0))) |
||
378 | outOfGamutFlag = true; |
||
379 | } |
||
3046 | fschmid | 380 | } |
381 | #endif |
||
382 | } |
||
383 | |||
2886 | fschmid | 384 | void ScColor::RecalcRGB() |
385 | { |
||
3041 | fschmid | 386 | outOfGamutFlag = false; |
3163 | fschmid | 387 | bool alert = true; |
2886 | fschmid | 388 | #ifdef HAVE_CMS |
389 | WORD inC[4]; |
||
390 | WORD outC[4]; |
||
3163 | fschmid | 391 | if ((CMSuse && CMSavail) && (!Spot)) |
2886 | fschmid | 392 | { |
3163 | fschmid | 393 | if (Model == colorModelRGB) |
2886 | fschmid | 394 | { |
3163 | fschmid | 395 | // allow RGB greys to go got to CMYK greys without transform |
396 | if (R == G && G == B) |
||
2886 | fschmid | 397 | { |
3163 | fschmid | 398 | C = M = Y = 0; |
399 | K = 255 - R; |
||
400 | } |
||
401 | else |
||
402 | { |
||
2886 | fschmid | 403 | inC[0] = R * 257; |
404 | inC[1] = G * 257; |
||
405 | inC[2] = B * 257; |
||
3163 | fschmid | 406 | if (SoftProofing) |
407 | { |
||
408 | if ((R == 0) && (B == 0) && (G == 255)) |
||
409 | alert = false; |
||
410 | cmsDoTransform(stdProofG, inC, outC, 1); |
||
411 | R = outC[0] / 257; |
||
412 | G = outC[1] / 257; |
||
413 | B = outC[2] / 257; |
||
414 | if ((alert) && (R == 0) && (B == 0) && (G == 255)) |
||
415 | outOfGamutFlag = true; |
||
416 | K = QMIN(QMIN(255 - R, 255 - G), 255 - B); |
||
417 | C = 255 - R - K; |
||
418 | M = 255 - G - K; |
||
419 | Y = 255 - B - K; |
||
420 | } |
||
421 | else |
||
422 | { |
||
423 | cmsDoTransform(stdTransCMYKG, inC, outC, 1); |
||
424 | C = outC[0] / 257; |
||
425 | M = outC[1] / 257; |
||
426 | Y = outC[2] / 257; |
||
427 | K = outC[3] / 257; |
||
428 | } |
||
2886 | fschmid | 429 | } |
430 | } |
||
3163 | fschmid | 431 | else |
2886 | fschmid | 432 | { |
433 | inC[0] = C * 257; |
||
434 | inC[1] = M * 257; |
||
435 | inC[2] = Y * 257; |
||
436 | inC[3] = K * 257; |
||
3163 | fschmid | 437 | if (SoftProofing) |
438 | { |
||
439 | if ((M == 0) && (K == 0) && (C == 255) && (Y == 255)) |
||
440 | alert = false; |
||
441 | if ((M == 0) && (C == 0) && (Y == 0)) |
||
442 | alert = false; |
||
443 | if ((M == C) && (C == Y) && (Y == K)) |
||
444 | alert = false; |
||
445 | cmsDoTransform(stdProofCMYKG, inC, outC, 1); |
||
446 | if ((alert) && (R == 0) && (B == 0) && (G == 255)) |
||
447 | outOfGamutFlag = true; |
||
448 | } |
||
449 | else |
||
450 | cmsDoTransform(stdTransRGBG, inC, outC, 1); |
||
2886 | fschmid | 451 | R = outC[0] / 257; |
452 | G = outC[1] / 257; |
||
453 | B = outC[2] / 257; |
||
454 | } |
||
455 | } |
||
3163 | fschmid | 456 | else |
2886 | fschmid | 457 | { |
3163 | fschmid | 458 | #endif |
2886 | fschmid | 459 | if (Model == colorModelRGB) |
460 | { |
||
3163 | fschmid | 461 | K = QMIN(QMIN(255 - R, 255 - G), 255 - B); |
462 | C = 255 - R - K; |
||
463 | M = 255 - G - K; |
||
464 | Y = 255 - B - K; |
||
2886 | fschmid | 465 | } |
466 | else |
||
467 | { |
||
3163 | fschmid | 468 | R = 255 - QMIN(255, C + K); |
469 | G = 255 - QMIN(255, M + K); |
||
470 | B = 255 - QMIN(255, Y + K); |
||
2886 | fschmid | 471 | } |
3163 | fschmid | 472 | #ifdef HAVE_CMS |
2886 | fschmid | 473 | } |
474 | #endif |
||
3163 | fschmid | 475 | RGB = QColor(R, G, B); |
2886 | fschmid | 476 | } |
3051 | fschmid | 477 | |
3060 | fschmid | 478 | bool ScColor::isRegistrationColor() |
479 | { |
||
480 | return Regist; |
||
481 | } |
||
482 | |||
483 | void ScColor::setRegistrationColor(bool s) |
||
484 | { |
||
485 | Regist = s; |
||
486 | } |
||
487 | |||
3051 | fschmid | 488 | bool ScColor::isSpotColor() |
489 | { |
||
490 | return Spot; |
||
491 | } |
||
492 | |||
493 | void ScColor::setSpotColor(bool s) |
||
494 | { |
||
495 | Spot = s; |
||
496 | } |