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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
|
/****************************************************************************
**
** Implementation of TQColor class for X11
**
** Created : 940112
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "tqcolor.h"
#include "tqcolor_p.h"
#include "string.h"
#include "tqpaintdevice.h"
#include "tqapplication.h"
#include "tqapplication_p.h"
#include "tqt_x11_p.h"
#ifdef USE_QT4
#include "tqintdict.h"
static const uint col_std_dict = 419;
static const uint col_large_dict = 18397;
struct TQColorData {
uint pix; // allocated pixel value
int context; // allocation context
};
typedef TQIntDict<TQColorData> TQColorDict;
typedef TQIntDictIterator<TQColorData> TQColorDictIt;
static int current_alloc_context = 0; // current color alloc context
class TQColorScreenData {
public:
TQColorScreenData()
{
colorDict = 0;
colors_avail = TRUE;
g_vis = 0;
g_carr = 0;
g_carr_fetch = TRUE;
g_cells = 0;
g_our_alloc = 0;
color_reduce = FALSE;
}
TQColorDict *colorDict; // dict of allocated colors
bool colors_avail; // X colors available
bool g_truecolor; // truecolor visual
Visual *g_vis; // visual
XColor *g_carr; // color array
bool g_carr_fetch; // perform XQueryColors?
int g_cells; // number of entries in g_carr
bool *g_our_alloc; // our allocated colors
uint red_mask , green_mask , blue_mask;
int red_shift, green_shift, blue_shift;
bool color_reduce;
int col_div_r;
int col_div_g;
int col_div_b;
};
static int screencount = 0;
static TQColorScreenData **screendata = 0; // array of screendata pointers
#define MAX_CONTEXTS 16
static int context_stack[MAX_CONTEXTS];
static int context_ptr = 0;
static void init_context_stack()
{
static bool did_init = FALSE;
if ( !did_init ) {
did_init = TRUE;
context_stack[0] = current_alloc_context = 0;
}
}
/*
This function is called from the event loop. It resets the colors_avail
flag so that the application can retry to allocate read-only colors
that other applications may have deallocated lately.
The g_our_alloc and g_carr are global arrays that optimize color
approximation when there are no more colors left to allocate.
*/
void qt_reset_color_avail()
{
int i;
for ( i = 0; i < screencount; i++ ) {
screendata[i]->colors_avail = TRUE;
screendata[i]->g_carr_fetch = TRUE; // do XQueryColors if !colors_avail
}
}
/*!
Enters a color allocation context and returns a non-zero unique
identifier.
Color allocation contexts are useful for programs that need to
allocate many colors and throw them away later, like image
viewers. The allocation context functions work for true color
displays as well as for colormap displays, except that
TQColor::destroyAllocContext() does nothing for true color.
Example:
\code
TQPixmap loadPixmap( TQString fileName )
{
static int alloc_context = 0;
if ( alloc_context )
TQColor::destroyAllocContext( alloc_context );
alloc_context = TQColor::enterAllocContext();
TQPixmap pm( fileName );
TQColor::leaveAllocContext();
return pm;
}
\endcode
The example code loads a pixmap from file. It frees up all colors
that were allocated the last time loadPixmap() was called.
The initial/default context is 0. TQt keeps a list of colors
associated with their allocation contexts. You can call
destroyAllocContext() to get rid of all colors that were allocated
in a specific context.
Calling enterAllocContext() enters an allocation context. The
allocation context lasts until you call leaveAllocContext().
TQColor has an internal stack of allocation contexts. Each call to
enterAllocContex() must have a corresponding leaveAllocContext().
\code
// context 0 active
int c1 = TQColor::enterAllocContext(); // enter context c1
// context c1 active
int c2 = TQColor::enterAllocContext(); // enter context c2
// context c2 active
TQColor::leaveAllocContext(); // leave context c2
// context c1 active
TQColor::leaveAllocContext(); // leave context c1
// context 0 active
// Now, free all colors that were allocated in context c2
TQColor::destroyAllocContext( c2 );
\endcode
You may also want to set the application's color specification.
See TQApplication::setColorSpec() for more information.
\sa leaveAllocContext(), currentAllocContext(), destroyAllocContext(),
TQApplication::setColorSpec()
*/
int TQColor::enterAllocContext()
{
static int context_seq_no = 0;
init_context_stack();
if ( context_ptr+1 == MAX_CONTEXTS ) {
#if defined(TQT_CHECK_STATE)
qWarning( "TQColor::enterAllocContext: Context stack overflow" );
#endif
return 0;
}
current_alloc_context = context_stack[++context_ptr] = ++context_seq_no;
return current_alloc_context;
}
/*!
Leaves a color allocation context.
See enterAllocContext() for a detailed explanation.
\sa enterAllocContext(), currentAllocContext()
*/
void TQColor::leaveAllocContext()
{
init_context_stack();
if ( context_ptr == 0 ) {
#if defined(TQT_CHECK_STATE)
qWarning( "TQColor::leaveAllocContext: Context stack underflow" );
#endif
return;
}
current_alloc_context = context_stack[--context_ptr];
}
/*!
Returns the current color allocation context.
The default context is 0.
\sa enterAllocContext(), leaveAllocContext()
*/
int TQColor::currentAllocContext()
{
return current_alloc_context;
}
/*!
Destroys a color allocation context, \e context.
This function deallocates all colors that were allocated in the
specified \a context. If \a context == -1, it frees up all colors
that the application has allocated. If \a context == -2, it frees
up all colors that the application has allocated, except those in
the default context.
The function does nothing for true color displays.
\sa enterAllocContext(), alloc()
*/
void TQColor::destroyAllocContext( int context )
{
init_context_stack();
if ( !color_init )
return;
int screen;
for ( screen = 0; screen < screencount; ++screen ) {
if ( screendata[screen]->g_truecolor )
continue;
ulong pixels[256];
bool freeing[256];
memset( freeing, FALSE, screendata[screen]->g_cells*sizeof(bool) );
TQColorData *d;
TQColorDictIt it( *screendata[screen]->colorDict );
int i = 0;
uint rgbv;
while ( (d=it.current()) ) {
rgbv = (uint)it.currentKey();
if ( (d->context || context==-1) &&
(d->context == context || context < 0) ) {
if ( !screendata[screen]->g_our_alloc[d->pix] && !freeing[d->pix] ) {
// will free this color
pixels[i++] = d->pix;
freeing[d->pix] = TRUE;
}
// remove from dict
screendata[screen]->colorDict->remove( (long)rgbv );
}
++it;
}
if ( i )
XFreeColors( TQPaintDevice::x11AppDisplay(),
TQPaintDevice::x11AppColormap( screen ),
pixels, i, 0 );
}
}
/*!
Returns the number of color bit planes for the underlying window
system.
The returned value is equal to the default pixmap depth.
\sa TQPixmap::defaultDepth()
*/
int TQColor::numBitPlanes()
{
return TQPaintDevice::x11AppDepth();
}
/*
Finds the nearest color.
*/
static int tqfind_nearest_color( int r, int g, int b, int* mindist_out,
TQColorScreenData *sd )
{
int mincol = -1;
int mindist = 200000;
int rx, gx, bx, dist;
XColor *xc = &sd->g_carr[0];
for ( int i=0; i<sd->g_cells; i++ ) {
rx = r - (xc->red >> 8);
gx = g - (xc->green >> 8);
bx = b - (xc->blue>> 8);
dist = rx*rx + gx*gx + bx*bx; // calculate distance
if ( dist < mindist ) { // minimal?
mindist = dist;
mincol = i;
}
xc++;
}
*mindist_out = mindist;
return mincol;
}
/*!
\internal
Allocates the color on screen \a screen. Only used in X11.
\sa alloc(), pixel()
*/
uint TQColor::alloc( int screen )
{
Display *dpy = TQPaintDevice::x11AppDisplay();
if ( screen < 0 )
screen = TQPaintDevice::x11AppScreen();
if ( !color_init )
return dpy ? (uint)BlackPixel(dpy, screen) : 0;
int r = tqRed(d.argb);
int g = tqGreen(d.argb);
int b = tqBlue(d.argb);
uint pix = 0;
TQColorScreenData *sd = screendata[screen];
if ( sd->g_truecolor ) { // truecolor: map to pixel
r = sd->red_shift > 0 ? r << sd->red_shift : r >> -sd->red_shift;
g = sd->green_shift > 0 ? g << sd->green_shift : g >> -sd->green_shift;
b = sd->blue_shift > 0 ? b << sd->blue_shift : b >> -sd->blue_shift;
pix = (b & sd->blue_mask) | (g & sd->green_mask) | (r & sd->red_mask)
| ~(sd->blue_mask | sd->green_mask | sd->red_mask);
if ( screen == TQPaintDevice::x11AppScreen() )
d.d32.pix = pix;
return pix;
}
TQColorData *c = sd->colorDict->tqfind( (long)(d.argb) );
if ( c ) { // found color in dictionary
pix = c->pix;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE; // color ok
d.d8.dirty = FALSE;
d.d8.pix = pix; // use same pixel value
if ( c->context != current_alloc_context ) {
c->context = 0; // convert to default context
sd->g_our_alloc[pix] = TRUE; // reuse without XAllocColor
}
}
return pix;
}
XColor col;
col.red = r << 8;
col.green = g << 8;
col.blue = b << 8;
bool try_again = FALSE;
bool try_alloc = !sd->color_reduce;
int try_count = 0;
do {
// This loop is run until we manage to either allocate or
// tqfind an approximate color, it stops after a few iterations.
try_again = FALSE;
if ( try_alloc && sd->colors_avail &&
XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ),&col) ) {
// We could allocate the color
pix = (uint) col.pixel;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.pix = pix;
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
sd->g_carr[d.d8.pix] = col; // update color array
if ( current_alloc_context == 0 )
sd->g_our_alloc[d.d8.pix] = TRUE; // reuse without XAllocColor
}
} else {
// No available colors, or we did not want to allocate one
int i;
sd->colors_avail = FALSE; // no more available colors
if ( sd->g_carr_fetch ) { // refetch color array
sd->g_carr_fetch = FALSE;
XQueryColors( dpy, TQPaintDevice::x11AppColormap( screen ), sd->g_carr,
sd->g_cells );
}
int mindist;
i = tqfind_nearest_color( r, g, b, &mindist, sd );
if ( mindist != 0 && !try_alloc ) {
// Not an exact match with an existing color
int rr = ((r+sd->col_div_r/2)/sd->col_div_r)*sd->col_div_r;
int rg = ((g+sd->col_div_g/2)/sd->col_div_g)*sd->col_div_g;
int rb = ((b+sd->col_div_b/2)/sd->col_div_b)*sd->col_div_b;
int rx = rr - r;
int gx = rg - g;
int bx = rb - b;
int dist = rx*rx + gx*gx + bx*bx; // calculate distance
if ( dist < mindist ) {
// reduced color is closer - try to alloc it
r = rr;
g = rg;
b = rb;
col.red = r << 8;
col.green = g << 8;
col.blue = b << 8;
try_alloc = TRUE;
try_again = TRUE;
sd->colors_avail = TRUE;
continue; // Try alloc reduced color
}
}
if ( i == -1 ) { // no nearest color?!
int unused, value;
hsv(&unused, &unused, &value);
if (value < 128) { // dark, use black
d.argb = tqRgb(0,0,0);
pix = (uint)BlackPixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
} else { // light, use white
d.argb = tqRgb(0xff,0xff,0xff);
pix = (uint)WhitePixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
}
return pix;
}
if ( sd->g_our_alloc[i] ) { // we've already allocated it
; // i == g_carr[i].pixel
} else {
// Try to allocate existing color
col = sd->g_carr[i];
if ( XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ), &col) ) {
i = (uint)col.pixel;
sd->g_carr[i] = col; // update color array
if ( screen == TQPaintDevice::x11AppScreen() ) {
if ( current_alloc_context == 0 )
sd->g_our_alloc[i] = TRUE; // only in the default context
}
} else {
// Oops, it's gone again
try_count++;
try_again = TRUE;
sd->colors_avail = TRUE;
sd->g_carr_fetch = TRUE;
}
}
if ( !try_again ) { // got it
pix = (uint)sd->g_carr[i].pixel;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix; // allocated X11 color
}
}
}
} while ( try_again && try_count < 2 );
if ( try_again ) { // no hope of allocating color
int unused, value;
hsv(&unused, &unused, &value);
if (value < 128) { // dark, use black
d.argb = tqRgb(0,0,0);
pix = (uint)BlackPixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
} else { // light, use white
d.argb = tqRgb(0xff,0xff,0xff);
pix = (uint)WhitePixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
}
return pix;
}
// All colors outside context 0 must go into the dictionary
bool many = sd->colorDict->count() >= sd->colorDict->size() * 8;
if ( many && sd->colorDict->size() == col_std_dict ) {
sd->colorDict->resize( col_large_dict );
}
if ( !many || current_alloc_context != 0 ) {
c = new TQColorData; // insert into color dict
TQ_CHECK_PTR( c );
c->pix = pix;
c->context = current_alloc_context;
sd->colorDict->insert( (long)d.argb, c ); // store color in dict
}
return pix;
}
/*!
Allocates the RGB color and returns the pixel value.
Allocating a color means to obtain a pixel value from the RGB
specification. The pixel value is an index into the global color
table, but should be considered an arbitrary platform-dependent value.
The pixel() function calls alloc() if necessary, so in general you
don't need to call this function.
\sa enterAllocContext()
*/
// ### 4.0 - remove me?
uint TQColor::alloc()
{
return alloc( -1 );
}
#else // USE_QT4
// NOT REVISED
/*****************************************************************************
The color dictionary speeds up color allocation significantly for X11.
When there are no more colors, TQColor::alloc() will set the colors_avail
flag to FALSE and try to tqfind the nearest color.
NOTE: From deep within the event loop, the colors_avail flag is reset to
TRUE (calls the function qt_reset_color_avail()), because some other
application might free its colors, thereby making them available for
this TQt application.
*****************************************************************************/
#include "tqintdict.h"
struct TQColorData {
uint pix; // allocated pixel value
int context; // allocation context
};
typedef TQIntDict<TQColorData> TQColorDict;
typedef TQIntDictIterator<TQColorData> TQColorDictIt;
static int current_alloc_context = 0; // current color alloc context
static const uint col_std_dict = 419;
static const uint col_large_dict = 18397;
class TQColorScreenData {
public:
TQColorScreenData()
{
colorDict = 0;
colors_avail = TRUE;
g_vis = 0;
g_carr = 0;
g_carr_fetch = TRUE;
g_cells = 0;
g_our_alloc = 0;
color_reduce = FALSE;
}
TQColorDict *colorDict; // dict of allocated colors
bool colors_avail; // X colors available
bool g_truecolor; // truecolor visual
Visual *g_vis; // visual
XColor *g_carr; // color array
bool g_carr_fetch; // perform XQueryColors?
int g_cells; // number of entries in g_carr
bool *g_our_alloc; // our allocated colors
uint red_mask , green_mask , blue_mask;
int red_shift, green_shift, blue_shift;
bool color_reduce;
int col_div_r;
int col_div_g;
int col_div_b;
};
static int screencount = 0;
static TQColorScreenData **screendata = 0; // array of screendata pointers
/*
This function is called from the event loop. It resets the colors_avail
flag so that the application can retry to allocate read-only colors
that other applications may have deallocated lately.
The g_our_alloc and g_carr are global arrays that optimize color
approximation when there are no more colors left to allocate.
*/
void qt_reset_color_avail()
{
int i;
for ( i = 0; i < screencount; i++ ) {
screendata[i]->colors_avail = TRUE;
screendata[i]->g_carr_fetch = TRUE; // do XQueryColors if !colors_avail
}
}
/*
Finds the nearest color.
*/
static int tqfind_nearest_color( int r, int g, int b, int* mindist_out,
TQColorScreenData *sd )
{
int mincol = -1;
int mindist = 200000;
int rx, gx, bx, dist;
XColor *xc = &sd->g_carr[0];
for ( int i=0; i<sd->g_cells; i++ ) {
rx = r - (xc->red >> 8);
gx = g - (xc->green >> 8);
bx = b - (xc->blue>> 8);
dist = rx*rx + gx*gx + bx*bx; // calculate distance
if ( dist < mindist ) { // minimal?
mindist = dist;
mincol = i;
}
xc++;
}
*mindist_out = mindist;
return mincol;
}
/*****************************************************************************
TQColor misc internal functions
*****************************************************************************/
static int highest_bit( uint v )
{
int i;
uint b = (uint)1 << 31; // get pos of highest bit in v
for ( i=31; ((b & v) == 0) && i>=0; i-- )
b >>= 1;
return i;
}
/*****************************************************************************
TQColor static member functions
*****************************************************************************/
/*!
Returns the maximum number of colors supported by the underlying
window system if the window system uses a palette.
Otherwise returns -1. Use numBitPlanes() to calculate the available
colors in that case.
*/
int TQColor::maxColors()
{
Visual *visual = (Visual *) TQPaintDevice::x11AppVisual();
if (visual->c_class & 1)
return TQPaintDevice::x11AppCells();
return -1;
}
/*!
Returns the number of color bit planes for the underlying window
system.
The returned value is equal to the default pixmap depth.
\sa TQPixmap::defaultDepth()
*/
int TQColor::numBitPlanes()
{
return TQPaintDevice::x11AppDepth();
}
/*!
Internal initialization required for TQColor.
This function is called from the TQApplication constructor.
\sa cleanup()
*/
void TQColor::initialize()
{
static const int blackIdx = 2;
static const int whiteIdx = 3;
if ( color_init ) // already initialized
return;
color_init = TRUE;
Display *dpy = TQPaintDevice::x11AppDisplay();
int spec = TQApplication::colorSpec();
screencount = ScreenCount( dpy );
screendata = new TQColorScreenData*[ screencount ];
int scr;
for ( scr = 0; scr < screencount; ++scr ) {
screendata[scr] = new TQColorScreenData;
screendata[scr]->g_vis = (Visual *) TQPaintDevice::x11AppVisual( scr );
screendata[scr]->g_truecolor = screendata[scr]->g_vis->c_class == TrueColor
|| screendata[scr]->g_vis->c_class == DirectColor;
int ncols = TQPaintDevice::x11AppCells( scr );
if ( screendata[scr]->g_truecolor ) {
if (scr == DefaultScreen(dpy))
colormodel = d32;
} else {
if (scr == DefaultScreen(dpy))
colormodel = d8;
// Create the g_our_alloc array, which remembers which color pixels
// we allocated.
screendata[scr]->g_cells = TQMIN(ncols,256);
screendata[scr]->g_carr = new XColor[screendata[scr]->g_cells];
TQ_CHECK_PTR( screendata[scr]->g_carr );
memset( screendata[scr]->g_carr, 0,
screendata[scr]->g_cells*sizeof(XColor) );
screendata[scr]->g_carr_fetch = TRUE; // run XQueryColors on demand
screendata[scr]->g_our_alloc = new bool[screendata[scr]->g_cells];
TQ_CHECK_PTR( screendata[scr]->g_our_alloc );
memset( screendata[scr]->g_our_alloc, FALSE,
screendata[scr]->g_cells*sizeof(bool) );
XColor *xc = &screendata[scr]->g_carr[0];
for ( int i=0; i<screendata[scr]->g_cells; i++ ) {
xc->pixel = i; // g_carr[i] = color i
xc++;
}
}
int dictsize;
if ( screendata[scr]->g_truecolor ) { // truecolor
dictsize = 1; // will not need color dict
screendata[scr]->red_mask = (uint)screendata[scr]->g_vis->red_mask;
screendata[scr]->green_mask = (uint)screendata[scr]->g_vis->green_mask;
screendata[scr]->blue_mask = (uint)screendata[scr]->g_vis->blue_mask;
screendata[scr]->red_shift =
highest_bit( screendata[scr]->red_mask ) - 7;
screendata[scr]->green_shift =
highest_bit( screendata[scr]->green_mask ) - 7;
screendata[scr]->blue_shift =
highest_bit( screendata[scr]->blue_mask ) - 7;
} else {
dictsize = col_std_dict;
}
screendata[scr]->colorDict = new TQColorDict(dictsize); // create dictionary
TQ_CHECK_PTR( screendata[scr]->colorDict );
if ( spec == (int)TQApplication::ManyColor ) {
screendata[scr]->color_reduce = TRUE;
switch ( qt_ncols_option ) {
case 216:
// 6:6:6
screendata[scr]->col_div_r = screendata[scr]->col_div_g =
screendata[scr]->col_div_b = (255/(6-1));
break;
default: {
// 2:3:1 proportions, solved numerically
if ( qt_ncols_option > 255 ) qt_ncols_option = 255;
if ( qt_ncols_option < 1 ) qt_ncols_option = 1;
int nr = 2;
int ng = 2;
int nb = 2;
for (;;) {
if ( nb*2 < nr && (nb+1)*nr*ng < qt_ncols_option )
nb++;
else if ( nr*3 < ng*2 && nb*(nr+1)*ng < qt_ncols_option )
nr++;
else if ( nb*nr*(ng+1) < qt_ncols_option )
ng++;
else break;
}
qt_ncols_option = nr*ng*nb;
screendata[scr]->col_div_r = (255/(nr-1));
screendata[scr]->col_div_g = (255/(ng-1));
screendata[scr]->col_div_b = (255/(nb-1));
}
}
}
}
scr = TQPaintDevice::x11AppScreen();
// Initialize global color objects
if ( TQPaintDevice::x11AppDefaultVisual(scr) &&
TQPaintDevice::x11AppDefaultColormap(scr) ) {
globalColors()[blackIdx].setPixel((uint) BlackPixel(dpy, scr));
globalColors()[whiteIdx].setPixel((uint) WhitePixel(dpy, scr));
} else {
globalColors()[blackIdx].alloc(scr);
globalColors()[whiteIdx].alloc(scr);
}
#if 0 /* 0 == allocate colors on demand */
setLazyAlloc( FALSE ); // allocate global colors
((TQColor*)(&darkGray))-> alloc();
((TQColor*)(&gray))-> alloc();
((TQColor*)(&lightGray))-> alloc();
((TQColor*)(&::red))-> alloc();
((TQColor*)(&::green))-> alloc();
((TQColor*)(&::blue))-> alloc();
((TQColor*)(&cyan))-> alloc();
((TQColor*)(&magenta))-> alloc();
((TQColor*)(&yellow))-> alloc();
((TQColor*)(&darkRed))-> alloc();
((TQColor*)(&darkGreen))-> alloc();
((TQColor*)(&darkBlue))-> alloc();
((TQColor*)(&darkCyan))-> alloc();
((TQColor*)(&darkMagenta))-> alloc();
((TQColor*)(&darkYellow))-> alloc();
setLazyAlloc( TRUE );
#endif
}
/*!
Internal clean up required for TQColor.
This function is called from the TQApplication destructor.
\sa initialize()
*/
void TQColor::cleanup()
{
if ( !color_init )
return;
color_init = FALSE;
int scr;
for ( scr = 0; scr < screencount; scr++ ) {
if ( screendata[scr]->g_carr ) {
delete [] screendata[scr]->g_carr;
screendata[scr]->g_carr = 0;
}
if ( screendata[scr]->g_our_alloc ) {
delete [] screendata[scr]->g_our_alloc;
screendata[scr]->g_our_alloc = 0;
}
if ( screendata[scr]->colorDict ) {
screendata[scr]->colorDict->setAutoDelete( TRUE );
screendata[scr]->colorDict->clear();
delete screendata[scr]->colorDict;
screendata[scr]->colorDict = 0;
}
delete screendata[scr];
screendata[scr] = 0;
}
delete [] screendata;
screendata = 0;
screencount = 0;
}
/*****************************************************************************
TQColor member functions
*****************************************************************************/
/*!
\internal
Allocates the color on screen \a screen. Only used in X11.
\sa alloc(), pixel()
*/
uint TQColor::alloc( int screen )
{
Display *dpy = TQPaintDevice::x11AppDisplay();
if ( screen < 0 )
screen = TQPaintDevice::x11AppScreen();
if ( !color_init )
return dpy ? (uint)BlackPixel(dpy, screen) : 0;
int r = tqRed(d.argb);
int g = tqGreen(d.argb);
int b = tqBlue(d.argb);
uint pix = 0;
TQColorScreenData *sd = screendata[screen];
if ( sd->g_truecolor ) { // truecolor: map to pixel
r = sd->red_shift > 0 ? r << sd->red_shift : r >> -sd->red_shift;
g = sd->green_shift > 0 ? g << sd->green_shift : g >> -sd->green_shift;
b = sd->blue_shift > 0 ? b << sd->blue_shift : b >> -sd->blue_shift;
pix = (b & sd->blue_mask) | (g & sd->green_mask) | (r & sd->red_mask)
| ~(sd->blue_mask | sd->green_mask | sd->red_mask);
if ( screen == TQPaintDevice::x11AppScreen() )
d.d32.pix = pix;
return pix;
}
TQColorData *c = sd->colorDict->tqfind( (long)(d.argb) );
if ( c ) { // found color in dictionary
pix = c->pix;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE; // color ok
d.d8.dirty = FALSE;
d.d8.pix = pix; // use same pixel value
if ( c->context != current_alloc_context ) {
c->context = 0; // convert to default context
sd->g_our_alloc[pix] = TRUE; // reuse without XAllocColor
}
}
return pix;
}
XColor col;
col.red = r << 8;
col.green = g << 8;
col.blue = b << 8;
bool try_again = FALSE;
bool try_alloc = !sd->color_reduce;
int try_count = 0;
do {
// This loop is run until we manage to either allocate or
// tqfind an approximate color, it stops after a few iterations.
try_again = FALSE;
if ( try_alloc && sd->colors_avail &&
XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ),&col) ) {
// We could allocate the color
pix = (uint) col.pixel;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.pix = pix;
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
sd->g_carr[d.d8.pix] = col; // update color array
if ( current_alloc_context == 0 )
sd->g_our_alloc[d.d8.pix] = TRUE; // reuse without XAllocColor
}
} else {
// No available colors, or we did not want to allocate one
int i;
sd->colors_avail = FALSE; // no more available colors
if ( sd->g_carr_fetch ) { // refetch color array
sd->g_carr_fetch = FALSE;
XQueryColors( dpy, TQPaintDevice::x11AppColormap( screen ), sd->g_carr,
sd->g_cells );
}
int mindist;
i = tqfind_nearest_color( r, g, b, &mindist, sd );
if ( mindist != 0 && !try_alloc ) {
// Not an exact match with an existing color
int rr = ((r+sd->col_div_r/2)/sd->col_div_r)*sd->col_div_r;
int rg = ((g+sd->col_div_g/2)/sd->col_div_g)*sd->col_div_g;
int rb = ((b+sd->col_div_b/2)/sd->col_div_b)*sd->col_div_b;
int rx = rr - r;
int gx = rg - g;
int bx = rb - b;
int dist = rx*rx + gx*gx + bx*bx; // calculate distance
if ( dist < mindist ) {
// reduced color is closer - try to alloc it
r = rr;
g = rg;
b = rb;
col.red = r << 8;
col.green = g << 8;
col.blue = b << 8;
try_alloc = TRUE;
try_again = TRUE;
sd->colors_avail = TRUE;
continue; // Try alloc reduced color
}
}
if ( i == -1 ) { // no nearest color?!
int unused, value;
hsv(&unused, &unused, &value);
if (value < 128) { // dark, use black
d.argb = tqRgb(0,0,0);
pix = (uint)BlackPixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
} else { // light, use white
d.argb = tqRgb(0xff,0xff,0xff);
pix = (uint)WhitePixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
}
return pix;
}
if ( sd->g_our_alloc[i] ) { // we've already allocated it
; // i == g_carr[i].pixel
} else {
// Try to allocate existing color
col = sd->g_carr[i];
if ( XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ), &col) ) {
i = (uint)col.pixel;
sd->g_carr[i] = col; // update color array
if ( screen == TQPaintDevice::x11AppScreen() ) {
if ( current_alloc_context == 0 )
sd->g_our_alloc[i] = TRUE; // only in the default context
}
} else {
// Oops, it's gone again
try_count++;
try_again = TRUE;
sd->colors_avail = TRUE;
sd->g_carr_fetch = TRUE;
}
}
if ( !try_again ) { // got it
pix = (uint)sd->g_carr[i].pixel;
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix; // allocated X11 color
}
}
}
} while ( try_again && try_count < 2 );
if ( try_again ) { // no hope of allocating color
int unused, value;
hsv(&unused, &unused, &value);
if (value < 128) { // dark, use black
d.argb = tqRgb(0,0,0);
pix = (uint)BlackPixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
} else { // light, use white
d.argb = tqRgb(0xff,0xff,0xff);
pix = (uint)WhitePixel( dpy, screen );
if ( screen == TQPaintDevice::x11AppScreen() ) {
d.d8.invalid = FALSE;
d.d8.dirty = FALSE;
d.d8.pix = pix;
}
}
return pix;
}
// All colors outside context 0 must go into the dictionary
bool many = sd->colorDict->count() >= sd->colorDict->size() * 8;
if ( many && sd->colorDict->size() == col_std_dict ) {
sd->colorDict->resize( col_large_dict );
}
if ( !many || current_alloc_context != 0 ) {
c = new TQColorData; // insert into color dict
TQ_CHECK_PTR( c );
c->pix = pix;
c->context = current_alloc_context;
sd->colorDict->insert( (long)d.argb, c ); // store color in dict
}
return pix;
}
/*!
Allocates the RGB color and returns the pixel value.
Allocating a color means to obtain a pixel value from the RGB
specification. The pixel value is an index into the global color
table, but should be considered an arbitrary platform-dependent value.
The pixel() function calls alloc() if necessary, so in general you
don't need to call this function.
\sa enterAllocContext()
*/
// ### 4.0 - remove me?
uint TQColor::alloc()
{
return alloc( -1 );
}
/*!
\overload
Returns the pixel value for screen \a screen.
This value is used by the underlying window system to refer to a color.
It can be thought of as an index into the display hardware's color table,
but the value is an arbitrary 32-bit value.
\sa alloc()
*/
uint TQColor::pixel( int screen ) const
{
if (screen != TQPaintDevice::x11AppScreen() &&
// don't allocate color0 or color1, they have fixed pixel
// values for all screens
d.argb != tqRgba(255, 255, 255, 1) && d.argb != tqRgba(0, 0, 0, 1))
return ((TQColor*)this)->alloc( screen );
return pixel();
}
void TQColor::setSystemNamedColor( const TQString& name )
{
// setSystemNamedColor should look up rgb values from the built in
// color tables first (see qcolor_p.cpp), and failing that, use
// the window system's interface for translating names to rgb values...
// we do this so that things like uic can load an XPM file with named colors
// and convert it to a png without having to use window system functions...
d.argb = qt_get_rgb_val( name.latin1() );
TQRgb rgb;
if ( qt_get_named_rgb( name.latin1(), &rgb ) ) {
setRgb( tqRed(rgb), tqGreen(rgb), tqBlue(rgb) );
if ( colormodel == d8 ) {
d.d8.invalid = FALSE;
d.d8.dirty = TRUE;
d.d8.pix = 0;
} else {
alloc();
}
} else if ( !color_init ) {
#if defined(TQT_CHECK_STATE)
qWarning( "TQColor::setSystemNamedColor: Cannot perform this operation "
"because TQApplication does not exist" );
#endif
// set color to invalid
*this = TQColor();
} else {
XColor col, hw_col;
if ( XLookupColor(TQPaintDevice::x11AppDisplay(),
TQPaintDevice::x11AppColormap(), name.latin1(),
&col, &hw_col) ) {
setRgb( col.red>>8, col.green>>8, col.blue>>8 );
} else {
// set color to invalid
*this = TQColor();
}
}
}
#define MAX_CONTEXTS 16
static int context_stack[MAX_CONTEXTS];
static int context_ptr = 0;
static void init_context_stack()
{
static bool did_init = FALSE;
if ( !did_init ) {
did_init = TRUE;
context_stack[0] = current_alloc_context = 0;
}
}
/*!
Enters a color allocation context and returns a non-zero unique
identifier.
Color allocation contexts are useful for programs that need to
allocate many colors and throw them away later, like image
viewers. The allocation context functions work for true color
displays as well as for colormap displays, except that
TQColor::destroyAllocContext() does nothing for true color.
Example:
\code
TQPixmap loadPixmap( TQString fileName )
{
static int alloc_context = 0;
if ( alloc_context )
TQColor::destroyAllocContext( alloc_context );
alloc_context = TQColor::enterAllocContext();
TQPixmap pm( fileName );
TQColor::leaveAllocContext();
return pm;
}
\endcode
The example code loads a pixmap from file. It frees up all colors
that were allocated the last time loadPixmap() was called.
The initial/default context is 0. TQt keeps a list of colors
associated with their allocation contexts. You can call
destroyAllocContext() to get rid of all colors that were allocated
in a specific context.
Calling enterAllocContext() enters an allocation context. The
allocation context lasts until you call leaveAllocContext().
TQColor has an internal stack of allocation contexts. Each call to
enterAllocContex() must have a corresponding leaveAllocContext().
\code
// context 0 active
int c1 = TQColor::enterAllocContext(); // enter context c1
// context c1 active
int c2 = TQColor::enterAllocContext(); // enter context c2
// context c2 active
TQColor::leaveAllocContext(); // leave context c2
// context c1 active
TQColor::leaveAllocContext(); // leave context c1
// context 0 active
// Now, free all colors that were allocated in context c2
TQColor::destroyAllocContext( c2 );
\endcode
You may also want to set the application's color specification.
See TQApplication::setColorSpec() for more information.
\sa leaveAllocContext(), currentAllocContext(), destroyAllocContext(),
TQApplication::setColorSpec()
*/
int TQColor::enterAllocContext()
{
static int context_seq_no = 0;
init_context_stack();
if ( context_ptr+1 == MAX_CONTEXTS ) {
#if defined(TQT_CHECK_STATE)
qWarning( "TQColor::enterAllocContext: Context stack overflow" );
#endif
return 0;
}
current_alloc_context = context_stack[++context_ptr] = ++context_seq_no;
return current_alloc_context;
}
/*!
Leaves a color allocation context.
See enterAllocContext() for a detailed explanation.
\sa enterAllocContext(), currentAllocContext()
*/
void TQColor::leaveAllocContext()
{
init_context_stack();
if ( context_ptr == 0 ) {
#if defined(TQT_CHECK_STATE)
qWarning( "TQColor::leaveAllocContext: Context stack underflow" );
#endif
return;
}
current_alloc_context = context_stack[--context_ptr];
}
/*!
Returns the current color allocation context.
The default context is 0.
\sa enterAllocContext(), leaveAllocContext()
*/
int TQColor::currentAllocContext()
{
return current_alloc_context;
}
/*!
Destroys a color allocation context, \e context.
This function deallocates all colors that were allocated in the
specified \a context. If \a context == -1, it frees up all colors
that the application has allocated. If \a context == -2, it frees
up all colors that the application has allocated, except those in
the default context.
The function does nothing for true color displays.
\sa enterAllocContext(), alloc()
*/
void TQColor::destroyAllocContext( int context )
{
init_context_stack();
if ( !color_init )
return;
int screen;
for ( screen = 0; screen < screencount; ++screen ) {
if ( screendata[screen]->g_truecolor )
continue;
ulong pixels[256];
bool freeing[256];
memset( freeing, FALSE, screendata[screen]->g_cells*sizeof(bool) );
TQColorData *d;
TQColorDictIt it( *screendata[screen]->colorDict );
int i = 0;
uint rgbv;
while ( (d=it.current()) ) {
rgbv = (uint)it.currentKey();
if ( (d->context || context==-1) &&
(d->context == context || context < 0) ) {
if ( !screendata[screen]->g_our_alloc[d->pix] && !freeing[d->pix] ) {
// will free this color
pixels[i++] = d->pix;
freeing[d->pix] = TRUE;
}
// remove from dict
screendata[screen]->colorDict->remove( (long)rgbv );
}
++it;
}
if ( i )
XFreeColors( TQPaintDevice::x11AppDisplay(),
TQPaintDevice::x11AppColormap( screen ),
pixels, i, 0 );
}
}
#endif // USE_QT4
|