summaryrefslogtreecommitdiff
path: root/xc/workInProgress/lbx/programs/lbxproxy/di/colormap.c
blob: 802d0e2407154f483590c1875bc0c833edc33278 (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
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
/* $XConsortium: colormap.c,v 1.8 95/05/17 18:26:41 dpw Exp mor $ */
/*
 * Copyright 1994 Network Computing Devices, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name Network Computing Devices, Inc. not be
 * used in advertising or publicity pertaining to distribution of this
 * software without specific, written prior permission.
 *
 * THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
 * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
 * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
 * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $NCDId: @(#)colormap.c,v 1.11 1994/09/07 01:50:28 lemke Exp $
 */
/*
 * XXX
 *
 * known glitches:
 *
 * LookupColor
 *	- only default cmap supported.  others possible, but need some
 *		data structure reorg.  perhaps hang RGBEntry cache off
 *		each cmap instead
 *
 * Alloc{Named}Color
 *	- no support for DirectColor or TrueColor visuals
 *		should just be a matter of adding more code all
 *		over the place
 *
 * FreeColors
 *	- not supporting planemask argument
 *
 * XXX
 */

#include	<stdio.h>
#include	"misc.h"
#include	"lbx.h"
#include	"assert.h"
#include	"colormap.h"
#include	"util.h"
#include	"resource.h"

#define	NBUCKETS	16

typedef struct _RGBEntry {
    struct _RGBEntry *next;
    RGBEntryRec color;
}           RGBCacheEntryRec, *RGBCacheEntryPtr;

static RGBCacheEntryPtr rgb_cache[NBUCKETS];


#define DynamicClass  1

typedef struct _cmap {
    Colormap    id;
    int         class;
    Window      window;
    VisualID    visual;
    int         size;
    Entry      *red;
    Entry      *green;
    Entry      *blue;
    int        *numPixelsRed;
    int        *numPixelsGreen;
    int        *numPixelsBlue;
    Pixel     **clientPixelsRed;
    Pixel     **clientPixelsGreen;
    Pixel     **clientPixelsBlue;
}           ColormapRec, *ColormapPtr;

/*
 * colormap cache code
 *
 * handles RGB database and AllocColor
 *
 */


/*
 * colorname cache
 *
 * assumes that hashing on the name makes the most sense.
 *
 *
 * XXX this gets real messy with colormaps other than the default,
 * since then we need to track the buggers to make sure we flush
 * entries when the cmap goes away.  we may simply want to cheat
 * and ignore non-default cmaps
 */


static int
Hash(name, len)
    char       *name;
    int         len;
{
    int         hash = 0;

    while (len--)
	hash = (hash << 1) ^ *name++;
    if (hash < 0)
	hash = -hash;
    return hash;
}

RGBEntryPtr
FindColorName(name, len, cmap)
    char       *name;
    int         len;
    Colormap    cmap;
{
    RGBCacheEntryPtr ce;
    int         hash;

    hash = Hash(name, len) % NBUCKETS;

    ce = rgb_cache[hash];

    while (ce) {
	if ((ce->color.cmap == cmap) &&
		(ce->color.namelen == len) &&
		!strncmp(ce->color.name, name, len)) {
	    return &ce->color;
	}
	ce = ce->next;
    }
    return (RGBEntryPtr) NULL;
}

Bool
AddColorName(name, len, rgbe)
    char       *name;
    int         len;
    RGBEntryRec *rgbe;
{
    RGBCacheEntryPtr ce,
                new;
    int         hash;

    hash = Hash(name, len) % NBUCKETS;

    ce = rgb_cache[hash];

    new = (RGBCacheEntryPtr) xalloc(sizeof(RGBCacheEntryRec));
    if (!new)
	return FALSE;
    if (!ce) {
	rgb_cache[hash] = new;
    } else {
	while (ce->next) {

#ifdef DEBUG
	    if ((rgbe->cmap == ce->color.cmap) &&
		    (len == ce->color.namelen) &&
		    !strncmp(name, ce->color.name, len)) {
		fprintf(stderr, "resetting color name\n");
	    }
#endif

	    ce = ce->next;
	}
	ce->next = new;
    }
    new->color = *rgbe;
    new->color.name = strnalloc(name, len);
    new->color.namelen = len;
    new->next = NULL;
    return TRUE;
}

/*****************************************************************************/

typedef struct _visual {
    int         class;
    VisualID    id;
    int         depth;
}           LbxVisualRec, *LbxVisualPtr;

static int  num_visuals = 0;

LbxVisualPtr *visuals;

typedef struct {
    Colormap    mid;
    int         client;
}           colorResource;

int
CreateVisual(depth, vis)
    int         depth;
    xVisualType *vis;
{
    LbxVisualPtr pvis;

    pvis = (LbxVisualPtr) xalloc(sizeof(LbxVisualRec));
    if (!pvis)
	return 0;
    pvis->id = vis->visualID;
    pvis->class = vis->class;
    pvis->depth = depth;
    visuals = (LbxVisualPtr *) xrealloc(visuals,
				   (num_visuals + 1) * sizeof(LbxVisualPtr));
    if (!visuals) {
	xfree(pvis);
	return 0;
    }
    visuals[num_visuals++] = pvis;

    return 1;
}

static      LbxVisualPtr
get_visual(vid)
    VisualID    vid;
{
    LbxVisualPtr pvis;
    int         i;

    for (i = 0; i < num_visuals; i++) {
	pvis = visuals[i];
	if (pvis->id == vid)
	    return pvis;
    }
    assert(0);
    return (LbxVisualPtr) 0;
}

static int
find_matching_pixel(pent, num, red, green, blue, pe)
    Entry      *pent;
    int         num;
    int         red,
                green,
                blue;
    Entry     **pe;
{
    int         i;

    if (!pent)
	return 0;

    *pe = (Entry *) 0;
    for (i = 0; i < num; i++) {
	if (pent[i].refcnt == 0)
	    continue;
	if ((pent[i].red == red) &&
		(pent[i].blue == blue) && (pent[i].green == green)) {
	    *pe = &pent[i];
	    return 1;
	}
    }
    return 0;
}

static int
find_named_pixel(pent, num, name, namelen, pe)
    Entry      *pent;
    int         num;
    char       *name;
    int         namelen;
    Entry     **pe;
{
    int         i;

    if (!pent)
	return 0;

    *pe = (Entry *) 0;
    for (i = 0; i < num; i++) {
	if (pent[i].refcnt == 0)
	    continue;
	if (pent[i].len == namelen && !strncmp(pent[i].name, name, namelen)) {
	    *pe = &pent[i];
	    return 1;
	}
    }
    return 0;
}

/* ARGSUSED */
int
FindPixel(client, cmap, red, green, blue, pent)
    ClientPtr   client;
    Colormap    cmap;
    int         red,
                green,
                blue;
    Entry     **pent;
{
    ColormapPtr pmap;
    Entry      *pe;

    *pent = (Entry *) 0;
    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    switch (pmap->class) {
    case StaticGray:
    case StaticColor:
	if (find_matching_pixel(pmap->red, pmap->size,
				red, green, blue, &pe)) {
	    *pent = pe;
	    return 1;
	}
	return 0;
    case PseudoColor:
    case GrayScale:
	if (find_matching_pixel(pmap->red, pmap->size,
				red, green, blue, &pe)) {
	    *pent = pe;
	    return 1;
	}
	return 0;
    case TrueColor:
    case DirectColor:
	/* XXX */
	return 0;
    default:
	assert(0);
	break;
    }
    return 0;
}

/* ARGSUSED */
int
FindNamedPixel(client, cmap, name, namelen, pent)
    ClientPtr   client;
    Colormap    cmap;
    char       *name;
    int         namelen;
    Entry     **pent;
{
    Entry      *pe;
    ColormapPtr pmap;

    *pent = (Entry *) 0;
    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    switch (pmap->class) {
    case StaticGray:
    case StaticColor:
	if (find_named_pixel(pmap->red, pmap->size, name, namelen, &pe)) {
	    *pent = pe;
	    return 1;
	}
	return 0;
    case PseudoColor:
    case GrayScale:
	if (find_named_pixel(pmap->red, pmap->size, name, namelen, &pe)) {
	    *pent = pe;
	    return 1;
	}
	return 0;
    case TrueColor:
    case DirectColor:
	/* XXX */
	return 0;
    default:
	assert(0);
	break;
    }
    return 0;
}

int
StorePixel(client, cmap, red, green, blue, rep_red, rep_green, rep_blue, pixel)
    ClientPtr   client;
    Colormap    cmap;
    int         red,
                green,
                blue;
    int         rep_red,
                rep_green,
                rep_blue;
    Pixel       pixel;
{
    ColormapPtr pmap;
    Entry      *pent;

    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    switch (pmap->class) {
    case StaticGray:
    case StaticColor:
    case PseudoColor:
    case GrayScale:
	pent = pmap->red;
#ifdef DEBUG
	if (pent[pixel].refcnt)
	    fprintf(stderr, "Overwriting existing pixel\n");
#endif

	pent[pixel].red = red;
	pent[pixel].green = green;
	pent[pixel].blue = blue;
	pent[pixel].rep_red = rep_red;
	pent[pixel].rep_green = rep_green;
	pent[pixel].rep_blue = rep_blue;
	pent[pixel].pixel = pixel;

	IncrementPixel(client, cmap, &pent[pixel]);

	return 1;
    case TrueColor:
    case DirectColor:
	/* XXX */
	return 0;
    default:
	assert(0);
	break;
    }
    return 0;
}

int
StoreNamedPixel(client, cmap, name, namelen, xred, xgreen, xblue,
		vred, vgreen, vblue, pixel)
    ClientPtr   client;
    Colormap    cmap;
    char       *name;
    int         namelen;
    int         xred,
                xgreen,
                xblue;
    int         vred,
                vgreen,
                vblue;
    Pixel       pixel;
{
    ColormapPtr pmap;
    Entry      *pent;
    RGBEntryRec rgbe;

    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    switch (pmap->class) {
    case StaticGray:
    case StaticColor:
    case PseudoColor:
    case GrayScale:
	pent = pmap->red;

	if (pent[pixel].refcnt) {

#ifdef DEBUG
	    fprintf(stderr, "Changing existing pixel name from %s to %s\n",
		    pent[pixel].name, name);
#endif

	    xfree(pent[pixel].name);
	}
	pent[pixel].red = xred;
	pent[pixel].green = xgreen;
	pent[pixel].blue = xblue;
	pent[pixel].rep_red = vred;
	pent[pixel].rep_green = vgreen;
	pent[pixel].rep_blue = vblue;
	pent[pixel].pixel = pixel;

	pent[pixel].name = strnalloc(name, namelen);
	pent[pixel].len = namelen;

	IncrementPixel(client, cmap, &pent[pixel]);
	break;
    case TrueColor:
    case DirectColor:
	/* XXX */
	break;
    default:
	assert(0);
	break;
    }
    /* stash RGB value in RGB database as well */
    rgbe.xred = xred;
    rgbe.xblue = xblue;
    rgbe.xgreen = xgreen;
    rgbe.vred = vred;
    rgbe.vblue = vblue;
    rgbe.vgreen = vgreen;
    rgbe.cmap = cmap;
    AddColorName(name, namelen, &rgbe);
    return 1;
}

/* ARGSUSED */
static void
FreeCell(pmap, pixel, channel)
    ColormapPtr pmap;
    Pixel       pixel;
    int         channel;
{
    Entry      *pent;

    /* XXX ingoring all but red/pseudo channel */
    pent = (Entry *) & pmap->red[pixel];
    if (pent->refcnt > 1)
	pent->refcnt--;
    else {
	pent->refcnt = 0;
	xfree(pent->name);
	bzero((char *) pent, sizeof(Entry));
    }
}

static void
FreeAllClientPixels(pmap, client)
    ColormapPtr pmap;
    int		client;
{
    Pixel      *ppix,
               *ppst;
    int         n;

    ppst = pmap->clientPixelsRed[client];
    if (pmap->class & DynamicClass) {
	for (ppix = ppst, n = pmap->numPixelsRed[client]; --n >= 0;) {
	    FreeCell(pmap, *ppix++, 1);
	}
    }
    xfree(ppst);
    pmap->clientPixelsRed[client] = (Pixel *) NULL;
    pmap->numPixelsRed[client] = 0;

    /* XXX add Direct and True color stuff */
}

/* ARGSUSED */
int
FreeClientPixels(value, id)
    pointer	value;
    XID		id;
{
    colorResource *pcr = (colorResource *)value;
    ColormapPtr pmap;

    pmap = (ColormapPtr) LookupIDByType(pcr->mid, RT_COLORMAP);
    if (pmap) {
	FreeAllClientPixels(pmap, pcr->client);
    }
    xfree(pcr);
    return Success;
}

int
IncrementPixel(pclient, cmap, pent)
    ClientPtr   pclient;
    Colormap    cmap;
    Entry      *pent;
{
    colorResource *pcr;
    ColormapPtr pmap;
    int         npix;
    Pixel      *ppix;
    int         client = pclient->index;

    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    npix = pmap->numPixelsRed[client];
    ppix = (Pixel *) xrealloc(pmap->clientPixelsRed[client],
			      (npix + 1) * sizeof(Pixel));
    if (!ppix)
	return 0;
    ppix[npix] = pent->pixel;
    pmap->clientPixelsRed[client] = ppix;
    pmap->numPixelsRed[client]++;

    pent->refcnt++;

    if ((LbxClientIndex(cmap) != client) && (pmap->numPixelsRed[client]==1)) {
	pcr = (colorResource *) xalloc(sizeof(colorResource));
	if (!pcr)
	    return 0;
	pcr->mid = cmap;
	pcr->client = client;

	AddResource(FakeClientID(client), RT_CMAPENTRY, (pointer) pcr);
    }
    return 1;
}

int
FreePixels(client, cmap, num, pixels)
    ClientPtr   client;
    Colormap    cmap;
    int         num;
    Pixel       pixels[];
{
    ColormapPtr pmap;
    Pixel       pix;
    int         i,
                zapped = 0,
                npix,
                npixnew;
    Pixel      *cptr,
               *pptr,
               *ppixClient;
    int         idx = client->index;

/* XXX doesn't handle plane-mask properly */
    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    switch (pmap->class) {
    case StaticGray:
    case StaticColor:
    case PseudoColor:
    case GrayScale:
	for (i = 0, pix = pixels[i]; i < num; i++) {
	    for (cptr = pmap->clientPixelsRed[idx],
		    npix = pmap->numPixelsRed[idx];
		    --npix >= 0 && *cptr != pix;
		    cptr++);
	    if (npix >= 0) {
		if (pmap->class & DynamicClass)
		    FreeCell(pmap, pix, 1);
		*cptr = ~((Pixel) 0);
		zapped++;
	    }
	}
	if (zapped) {
	    npixnew = pmap->numPixelsRed[idx] - zapped;
	    if (npixnew) {
		pptr = cptr = pmap->clientPixelsRed[idx];
		for (npix = 0; npix < npixnew; cptr++) {
		    if (*cptr != ~((Pixel) 0)) {
			*pptr++ = *cptr;
			npix++;
		    }
		}
		ppixClient = pmap->clientPixelsRed[idx];
		pptr = (Pixel *) xrealloc(ppixClient, npixnew * sizeof(Pixel));
		if (pptr)
		    ppixClient = pptr;
		pmap->numPixelsRed[idx] = npixnew;
		pmap->clientPixelsRed[idx] = ppixClient;
	    } else {
		pmap->numPixelsRed[idx] = 0;
		xfree(pmap->clientPixelsRed[idx]);
		pmap->clientPixelsRed[idx] = (Pixel *) NULL;
	    }
	}
	return 1;
    case TrueColor:
    case DirectColor:
	/* XXX */
	break;
    default:
	assert(0);
	break;
    }
    return 0;
}

static      ColormapPtr
create_colormap(cmap, win, visual)
    Colormap    cmap;
    Window      win;
    VisualID    visual;
{
    ColormapPtr pmap;
    LbxVisualPtr pvis;
    int         tsize,
                size;
    Pixel     **pptr;

    pvis = get_visual(visual);
    size = (1 << pvis->depth);
    tsize = sizeof(ColormapRec) + (size * sizeof(Entry)) +
	(MAXCLIENTS * sizeof(Pixel *)) + (MAXCLIENTS * sizeof(int));

    pmap = (ColormapPtr) xalloc(tsize);
    if (!pmap)
	return pmap;
    bzero((char *) pmap, tsize);
    pmap->size = size;
    pmap->id = cmap;
    pmap->window = win;
    pmap->visual = visual;
    pmap->class = pvis->class;
    pmap->red = (Entry *) ((char *) pmap + sizeof(ColormapRec));

    pmap->clientPixelsRed = (Pixel **)
	((char *) pmap->red + (pmap->size * sizeof(Entry)));
    pmap->numPixelsRed = (int *) ((char *) pmap->clientPixelsRed +
				  (MAXCLIENTS * sizeof(Pixel *)));

    bzero((char *) pmap->red, (pmap->size * sizeof(Entry)));
    bzero((char *) pmap->numPixelsRed, (MAXCLIENTS * sizeof(int)));
    for (pptr = &pmap->clientPixelsRed[MAXCLIENTS]; --pptr >= pmap->clientPixelsRed;)
	*pptr = (Pixel *) NULL;

    /* XXX still need to handle Direct and True */

    return pmap;
}

/* ARGSUSED */
int
CreateColormap(client, cmap, win, visual)
    ClientPtr   client;
    Colormap    cmap;
    Window      win;
    VisualID    visual;
{
    ColormapPtr pmap;

    pmap = create_colormap(cmap, win, visual);
    if (!pmap)
	return 0;
    if (!AddResource(cmap, RT_COLORMAP, (pointer) pmap)) {
	return 0;
    }
    return 1;
}

/* ARGSUSED */
int
DestroyColormap(value, id)
    pointer	value;
    XID		id;
{
    ColormapPtr pmap = (ColormapPtr)value;
    xfree(pmap->blue);
    xfree(pmap->green);
    xfree(pmap);
    return Success;
}

/* ARGSUSED */
int
FreeColormap(client, cmap)
    ClientPtr   client;
    Colormap    cmap;
{
    ColormapPtr pmap;

    pmap = (ColormapPtr) LookupIDByType(cmap, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    FreeResource(cmap, RT_NONE);
    return 1;
}

/*
 * this one is real ugly.  just nuke everything for now
 */
/* ARGSUSED */
int
CopyAndFreeColormap(client, new, old)
    ClientPtr   client;
    Colormap    new,
                old;
{
    ColormapPtr pmap,
                newmap;

    pmap = (ColormapPtr) LookupIDByType(old, RT_COLORMAP);
    if (!pmap) {
	return 0;
    }
    /* trash existing cache values */
    xfree(pmap->red);
    xfree(pmap->blue);
    xfree(pmap->green);
    pmap->red = pmap->blue = pmap->green = (Entry *) 0;

    /* create a new cmap of the same type */
    newmap = create_colormap(new, pmap->window, pmap->visual);
    if (!AddResource(new, RT_COLORMAP, (pointer) newmap)) {
	return 0;
    }
    return 1;
}

void
FreeColors()
{
    RGBCacheEntryPtr ce,
                nce;
    int         i;

    for (i = 0; i < NBUCKETS; i++) {
	ce = rgb_cache[i];
	while (ce) {
	    nce = ce->next;
	    xfree(ce->color.name);
	    xfree(ce);
	    ce = nce;
	}
	rgb_cache[i] = 0;
    }
}