gdkcolor.c 18.5 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
15
16
17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
19
20
21
 */
#include <X11/Xlib.h>
#include "gdk.h"
#include "gdkprivate.h"
Tim Janik's avatar
Tim Janik committed
22
#include "gdkx.h"
Elliot Lee's avatar
Elliot Lee committed
23
24
25
26
27
28
29
30
31
32


static gint  gdk_colormap_match_color (GdkColormap *cmap,
				       GdkColor    *color,
				       const gchar *available);
static void  gdk_colormap_add         (GdkColormap *cmap);
static void  gdk_colormap_remove      (GdkColormap *cmap);
static guint gdk_colormap_hash        (Colormap    *cmap);
static gint  gdk_colormap_cmp         (Colormap    *a,
				       Colormap    *b);
33
static void gdk_colormap_real_destroy (GdkColormap *colormap);
Elliot Lee's avatar
Elliot Lee committed
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

static GHashTable *colormap_hash = NULL;


GdkColormap*
gdk_colormap_new (GdkVisual *visual,
		  gint       private_cmap)
{
  GdkColormap *colormap;
  GdkColormapPrivate *private;
  Visual *xvisual;
  int size;
  int i;

  g_return_val_if_fail (visual != NULL, NULL);

  private = g_new (GdkColormapPrivate, 1);
  colormap = (GdkColormap*) private;

  private->xdisplay = gdk_display;
  private->visual = visual;
  private->next_color = 0;
  private->ref_count = 1;
  xvisual = ((GdkVisualPrivate*) visual)->xvisual;

59
60
61
  colormap->size = visual->colormap_size;
  colormap->colors = g_new (GdkColor, colormap->size);

Elliot Lee's avatar
Elliot Lee committed
62
63
64
65
66
67
68
69
70
71
  switch (visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
      private->private_val = private_cmap;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, (private_cmap) ? (AllocAll) : (AllocNone));

      if (private_cmap)
	{
72
73
74
75
76
	  XColor *default_colors;

	  default_colors = g_new (XColor, colormap->size);

	  for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
77
78
79
80
	    default_colors[i].pixel = i;

	  XQueryColors (private->xdisplay,
			DefaultColormap (private->xdisplay, gdk_screen),
81
			default_colors, colormap->size);
Elliot Lee's avatar
Elliot Lee committed
82

83
	  for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
84
85
86
87
88
89
90
	    {
	      colormap->colors[i].pixel = default_colors[i].pixel;
	      colormap->colors[i].red = default_colors[i].red;
	      colormap->colors[i].green = default_colors[i].green;
	      colormap->colors[i].blue = default_colors[i].blue;
	    }

91
92
93
	  gdk_colormap_change (colormap, colormap->size);
	  
	  g_free (default_colors);
Elliot Lee's avatar
Elliot Lee committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	}
      break;

    case GDK_VISUAL_DIRECT_COLOR:
      private->private_val = TRUE;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, AllocAll);

      size = 1 << visual->red_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].red = i * 65535 / (size - 1);

      size = 1 << visual->green_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].green = i * 65535 / (size - 1);

      size = 1 << visual->blue_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].blue = i * 65535 / (size - 1);

114
      gdk_colormap_change (colormap, colormap->size);
Elliot Lee's avatar
Elliot Lee committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
      break;

    case GDK_VISUAL_STATIC_GRAY:
    case GDK_VISUAL_STATIC_COLOR:
    case GDK_VISUAL_TRUE_COLOR:
      private->private_val = FALSE;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, AllocNone);
      break;
    }

  gdk_colormap_add (colormap);

  return colormap;
}

131
static void
Elliot Lee's avatar
Elliot Lee committed
132
133
134
135
136
137
138
139
140
141
142
gdk_colormap_real_destroy (GdkColormap *colormap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;

  g_return_if_fail (colormap != NULL);

  if (private->ref_count > 0)
    return;

  gdk_colormap_remove (colormap);
  XFreeColormap (private->xdisplay, private->xcolormap);
143
  g_free (colormap->colors);
Elliot Lee's avatar
Elliot Lee committed
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
  g_free (colormap);
}

GdkColormap*
gdk_colormap_ref (GdkColormap *cmap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
  g_return_val_if_fail (cmap != NULL, NULL);

  private->ref_count += 1;
  return cmap;
}

void
gdk_colormap_unref (GdkColormap *cmap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
  g_return_if_fail (cmap != NULL);

  private->ref_count -= 1;
  if (private->ref_count == 0)
    gdk_colormap_real_destroy (cmap);
}

GdkColormap*
gdk_colormap_get_system (void)
{
  static GdkColormap *colormap = NULL;
  GdkColormapPrivate *private;
173
  XColor *xpalette;
Elliot Lee's avatar
Elliot Lee committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  gint i;

  if (!colormap)
    {
      private = g_new (GdkColormapPrivate, 1);
      colormap = (GdkColormap*) private;

      private->xdisplay = gdk_display;
      private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
      private->visual = gdk_visual_get_system ();
      private->private_val = FALSE;
      private->next_color = 0;
      private->ref_count = 1;

188
189
190
      colormap->size = private->visual->colormap_size;
      colormap->colors = g_new (GdkColor, colormap->size);

191
192
      if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
	  (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
Elliot Lee's avatar
Elliot Lee committed
193
	{
194
195
196
	  xpalette = g_new (XColor, colormap->size);
	  
	  for (i = 0; i < colormap->size; i++)
197
198
199
200
201
202
203
204
	    {
	      xpalette[i].pixel = i;
	      xpalette[i].red = 0;
	      xpalette[i].green = 0;
	      xpalette[i].blue = 0;
	    }
	  
	  XQueryColors (gdk_display, private->xcolormap, xpalette, 
205
			colormap->size);
206
	  
207
	  for (i = 0; i < colormap->size; i++)
208
209
210
211
212
213
	    {
	      colormap->colors[i].pixel = xpalette[i].pixel;
	      colormap->colors[i].red = xpalette[i].red;
	      colormap->colors[i].green = xpalette[i].green;
	      colormap->colors[i].blue = xpalette[i].blue;
	    }
214
215

	  g_free (xpalette);
Elliot Lee's avatar
Elliot Lee committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	}

      gdk_colormap_add (colormap);
    }

  return colormap;
}

gint
gdk_colormap_get_system_size (void)
{
  return DisplayCells (gdk_display, gdk_screen);
}

void
gdk_colormap_change (GdkColormap *colormap,
		     gint         ncolors)
{
  GdkColormapPrivate *private;
  GdkVisual *visual;
236
  XColor *palette;
Elliot Lee's avatar
Elliot Lee committed
237
238
239
240
241
242
243
  gint shift;
  int max_colors;
  int size;
  int i;

  g_return_if_fail (colormap != NULL);

244
245
  palette = g_new (XColor, ncolors);

Elliot Lee's avatar
Elliot Lee committed
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
  private = (GdkColormapPrivate*) colormap;
  switch (private->visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
      for (i = 0; i < ncolors; i++)
	{
	  palette[i].pixel = colormap->colors[i].pixel;
	  palette[i].red = colormap->colors[i].red;
	  palette[i].green = colormap->colors[i].green;
	  palette[i].blue = colormap->colors[i].blue;
	  palette[i].flags = DoRed | DoGreen | DoBlue;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
      private->next_color = MAX (private->next_color, ncolors);
      break;

    case GDK_VISUAL_DIRECT_COLOR:
      visual = private->visual;

      shift = visual->red_shift;
      max_colors = 1 << visual->red_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].red = colormap->colors[i].red;
	  palette[i].flags = DoRed;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);

      shift = visual->green_shift;
      max_colors = 1 << visual->green_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].green = colormap->colors[i].green;
	  palette[i].flags = DoGreen;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);

      shift = visual->blue_shift;
      max_colors = 1 << visual->blue_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].blue = colormap->colors[i].blue;
	  palette[i].flags = DoBlue;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);
      break;

    default:
      break;
    }
310
311

  g_free (palette);
Elliot Lee's avatar
Elliot Lee committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
}

void
gdk_colors_store (GdkColormap   *colormap,
		  GdkColor      *colors,
		  gint           ncolors)
{
  gint i;

  for (i = 0; i < ncolors; i++)
    {
      colormap->colors[i].pixel = colors[i].pixel;
      colormap->colors[i].red = colors[i].red;
      colormap->colors[i].green = colors[i].green;
      colormap->colors[i].blue = colors[i].blue;
    }

  gdk_colormap_change (colormap, ncolors);
}

332
gboolean
Elliot Lee's avatar
Elliot Lee committed
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
gdk_colors_alloc (GdkColormap   *colormap,
		  gint           contiguous,
		  gulong        *planes,
		  gint           nplanes,
		  gulong        *pixels,
		  gint           npixels)
{
  GdkColormapPrivate *private;
  gint return_val;

  g_return_val_if_fail (colormap != NULL, 0);

  private = (GdkColormapPrivate*) colormap;

  return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
				 contiguous, planes, nplanes, pixels, npixels);

  return return_val;
}

void
gdk_colors_free (GdkColormap *colormap,
		 gulong      *pixels,
		 gint         npixels,
		 gulong       planes)
{
  GdkColormapPrivate *private;

  g_return_if_fail (colormap != NULL);

  private = (GdkColormapPrivate*) colormap;

  XFreeColors (private->xdisplay, private->xcolormap,
	       pixels, npixels, planes);
}

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
/*
 *--------------------------------------------------------------
 * gdk_color_copy
 *
 *   Copy a color structure into new storage.
 *
 * Arguments:
 *   "color" is the color struct to copy.
 *
 * Results:
 *   A new color structure.  Free it with gdk_color_free.
 *
 *--------------------------------------------------------------
 */

static GMemChunk *color_chunk;

GdkColor*
gdk_color_copy (GdkColor *color)
{
  GdkColor *new_color;
  
  g_return_val_if_fail (color != NULL, NULL);

  if (color_chunk == NULL)
    color_chunk = g_mem_chunk_new ("colors",
				   sizeof (GdkColor),
				   4096,
				   G_ALLOC_AND_FREE);

  new_color = g_chunk_new (GdkColor, color_chunk);
  *new_color = *color;
  return new_color;
}

/*
 *--------------------------------------------------------------
 * gdk_color_free
 *
 *   Free a color structure obtained from gdk_color_copy.  Do not use
 *   with other color structures.
 *
 * Arguments:
 *   "color" is the color struct to free.
 *
 *-------------------------------------------------------------- */

void
gdk_color_free (GdkColor *color)
{
  g_assert (color_chunk != NULL);
  g_return_if_fail (color != NULL);

  g_mem_chunk_free (color_chunk, color);
}

Elliot Lee's avatar
Elliot Lee committed
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
gint
gdk_color_white (GdkColormap *colormap,
		 GdkColor    *color)
{
  gint return_val;

  g_return_val_if_fail (colormap != NULL, FALSE);

  if (color)
    {
      color->pixel = WhitePixel (gdk_display, gdk_screen);
      color->red = 65535;
      color->green = 65535;
      color->blue = 65535;

      return_val = gdk_color_alloc (colormap, color);
    }
  else
    return_val = FALSE;

  return return_val;
}

gint
gdk_color_black (GdkColormap *colormap,
		 GdkColor    *color)
{
  gint return_val;

  g_return_val_if_fail (colormap != NULL, FALSE);

  if (color)
    {
      color->pixel = BlackPixel (gdk_display, gdk_screen);
      color->red = 0;
      color->green = 0;
      color->blue = 0;

      return_val = gdk_color_alloc (colormap, color);
    }
  else
    return_val = FALSE;

  return return_val;
}

471
gboolean
Elliot Lee's avatar
Elliot Lee committed
472
473
474
475
476
gdk_color_parse (const gchar *spec,
		 GdkColor *color)
{
  Colormap xcolormap;
  XColor xcolor;
477
  gboolean return_val;
Elliot Lee's avatar
Elliot Lee committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

  g_return_val_if_fail (spec != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  xcolormap = DefaultColormap (gdk_display, gdk_screen);

  if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
    {
      return_val = TRUE;
      color->red = xcolor.red;
      color->green = xcolor.green;
      color->blue = xcolor.blue;
    }
  else
    return_val = FALSE;

  return return_val;
}

497
gboolean
Elliot Lee's avatar
Elliot Lee committed
498
499
500
501
502
503
gdk_color_alloc (GdkColormap *colormap,
		 GdkColor    *color)
{
  GdkColormapPrivate *private;
  GdkVisual *visual;
  XColor xcolor;
504
  gchar *available = NULL;
505
  gboolean return_val;
Elliot Lee's avatar
Elliot Lee committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  gint i, index;

  g_return_val_if_fail (colormap != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  xcolor.red = color->red;
  xcolor.green = color->green;
  xcolor.blue = color->blue;
  xcolor.pixel = color->pixel;
  xcolor.flags = DoRed | DoGreen | DoBlue;

  return_val = FALSE;
  private = (GdkColormapPrivate*) colormap;

  switch (private->visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
      if (private->private_val)
	{
526
	  if (private->next_color >= colormap->size)
Elliot Lee's avatar
Elliot Lee committed
527
	    {
528
529
	      available = g_new (gchar, colormap->size);
	      for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
		available[i] = TRUE;

	      index = gdk_colormap_match_color (colormap, color, available);
	      if (index != -1)
		{
		  available[index] = FALSE;
		  *color = colormap->colors[index];
		  return_val = TRUE;
		}
	      else
		{
		  return_val = FALSE;
		}
	    }
	  else
	    {
546
	      xcolor.pixel = colormap->size - 1 -private->next_color;
Elliot Lee's avatar
Elliot Lee committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	      color->pixel = xcolor.pixel;
	      private->next_color += 1;

	      XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
	      return_val = TRUE;
	    }
	}
      else
	{
	  while (1)
	    {
	      if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
		{
		  color->pixel = xcolor.pixel;
		  color->red = xcolor.red;
		  color->green = xcolor.green;
		  color->blue = xcolor.blue;

565
566
		  if (color->pixel < colormap->size)
		    colormap->colors[color->pixel] = *color;
Elliot Lee's avatar
Elliot Lee committed
567
568
569
570
571
572

		  return_val = TRUE;
		  break;
		}
	      else
		{
573
		  if (available == NULL)
Elliot Lee's avatar
Elliot Lee committed
574
		    {
575
576
		      available = g_new (gchar, colormap->size);
		      for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
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
			available[i] = TRUE;
		    }

		  index = gdk_colormap_match_color (colormap, color, available);
		  if (index != -1)
		    {
		      available[index] = FALSE;
		      xcolor.red = colormap->colors[index].red;
		      xcolor.green = colormap->colors[index].green;
		      xcolor.blue = colormap->colors[index].blue;
		    }
		  else
		    {
		      return_val = FALSE;
		      break;
		    }
		}
	    }
	}
      break;

    case GDK_VISUAL_DIRECT_COLOR:
      visual = private->visual;
      xcolor.pixel = (((xcolor.red >> (16 - visual->red_prec)) << visual->red_shift) +
		      ((xcolor.green >> (16 - visual->green_prec)) << visual->green_shift) +
		      ((xcolor.blue >> (16 - visual->blue_prec)) << visual->blue_shift));
      color->pixel = xcolor.pixel;
      return_val = TRUE;
      break;

    case GDK_VISUAL_STATIC_GRAY:
    case GDK_VISUAL_STATIC_COLOR:
    case GDK_VISUAL_TRUE_COLOR:
      if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
	{
	  color->pixel = xcolor.pixel;
	  return_val = TRUE;
	}
      else
	return_val = FALSE;
      break;
    }

620
621
622
  if (available)
    g_free (available);
  
Elliot Lee's avatar
Elliot Lee committed
623
624
625
  return return_val;
}

626
gboolean
Elliot Lee's avatar
Elliot Lee committed
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
gdk_color_change (GdkColormap *colormap,
		  GdkColor    *color)
{
  GdkColormapPrivate *private;
  XColor xcolor;

  g_return_val_if_fail (colormap != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  xcolor.pixel = color->pixel;
  xcolor.red = color->red;
  xcolor.green = color->green;
  xcolor.blue = color->blue;
  xcolor.flags = DoRed | DoGreen | DoBlue;

  private = (GdkColormapPrivate*) colormap;
  XStoreColor (private->xdisplay, private->xcolormap, &xcolor);

  return TRUE;
}

gint
gdk_color_equal (GdkColor *colora,
		 GdkColor *colorb)
{
  g_return_val_if_fail (colora != NULL, FALSE);
  g_return_val_if_fail (colorb != NULL, FALSE);

  return ((colora->red == colorb->red) &&
	  (colora->green == colorb->green) &&
	  (colora->blue == colorb->blue));
}

GdkColormap*
gdkx_colormap_get (Colormap xcolormap)
{
  GdkColormap *colormap;
  GdkColormapPrivate *private;

  colormap = gdk_colormap_lookup (xcolormap);
  if (colormap)
    return colormap;

  if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
    return gdk_colormap_get_system ();

  private = g_new (GdkColormapPrivate, 1);
  colormap = (GdkColormap*) private;

  private->xdisplay = gdk_display;
  private->xcolormap = xcolormap;
  private->visual = NULL;
  private->private_val = TRUE;
  private->next_color = 0;

682
683
684
685
686
687
  /* To do the following safely, we would have to have some way of finding
   * out what the size or visual of the given colormap is. It seems
   * X doesn't allow this
   */

#if 0
Elliot Lee's avatar
Elliot Lee committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  for (i = 0; i < 256; i++)
    {
      xpalette[i].pixel = i;
      xpalette[i].red = 0;
      xpalette[i].green = 0;
      xpalette[i].blue = 0;
    }

  XQueryColors (gdk_display, private->xcolormap, xpalette, 256);

  for (i = 0; i < 256; i++)
    {
      colormap->colors[i].pixel = xpalette[i].pixel;
      colormap->colors[i].red = xpalette[i].red;
      colormap->colors[i].green = xpalette[i].green;
      colormap->colors[i].blue = xpalette[i].blue;
    }
705
706
707
708
#endif

  colormap->colors = NULL;
  colormap->size = 0;
Elliot Lee's avatar
Elliot Lee committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732

  gdk_colormap_add (colormap);

  return colormap;
}


static gint
gdk_colormap_match_color (GdkColormap *cmap,
			  GdkColor    *color,
			  const gchar *available)
{
  GdkColor *colors;
  guint sum, max;
  gint rdiff, gdiff, bdiff;
  gint i, index;

  g_return_val_if_fail (cmap != NULL, 0);
  g_return_val_if_fail (color != NULL, 0);

  colors = cmap->colors;
  max = 3 * (65536);
  index = -1;

733
  for (i = 0; i < cmap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
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
    {
      if ((!available) || (available && available[i]))
	{
	  rdiff = (color->red - colors[i].red);
	  gdiff = (color->green - colors[i].green);
	  bdiff = (color->blue - colors[i].blue);

	  sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);

	  if (sum < max)
	    {
	      index = i;
	      max = sum;
	    }
	}
    }

  return index;
}


GdkColormap*
gdk_colormap_lookup (Colormap xcolormap)
{
  GdkColormap *cmap;

  if (!colormap_hash)
    return NULL;

  cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
  return cmap;
}

static void
gdk_colormap_add (GdkColormap *cmap)
{
  GdkColormapPrivate *private;

  if (!colormap_hash)
    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
				      (GCompareFunc) gdk_colormap_cmp);

  private = (GdkColormapPrivate*) cmap;

  g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
}

static void
gdk_colormap_remove (GdkColormap *cmap)
{
  GdkColormapPrivate *private;

  if (!colormap_hash)
    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
				      (GCompareFunc) gdk_colormap_cmp);

  private = (GdkColormapPrivate*) cmap;

  g_hash_table_remove (colormap_hash, &private->xcolormap);
}

static guint
gdk_colormap_hash (Colormap *cmap)
{
  return *cmap;
}

static gint
gdk_colormap_cmp (Colormap *a,
		  Colormap *b)
{
  return (*a == *b);
}