home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / libsrc / gr / src / colors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-28  |  2.5 KB  |  139 lines

  1. /* This is file COLORS.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include "graphics.h"
  16.  
  17. extern int _GrCurMode;
  18.  
  19. typedef struct {
  20.   unsigned char n;
  21.   unsigned char state; /* 0=free, 1=shared, 2=writable */
  22.   unsigned char r;
  23.   unsigned char g;
  24.   unsigned char b;
  25. } Color;
  26.  
  27. static Color color[256];
  28.  
  29. static int initted=0;
  30.  
  31. static init_colormap()
  32. {
  33.   initted = 1;
  34.   bzero(color, sizeof(color));
  35.   GrAllocColor(0, 0, 0);
  36.   GrAllocColor(255, 255, 255);
  37. }
  38.  
  39. GrBlack()
  40. {
  41.   if (!initted)
  42.     init_colormap();
  43.   return 0;
  44. }
  45.  
  46. GrWhite()
  47. {
  48.   if (!initted)
  49.     init_colormap();
  50.   return 1;
  51. }
  52.  
  53. GrAllocColor(int r, int g, int b)
  54. {
  55.   int n;
  56.   if (!initted)
  57.     init_colormap();
  58.   for (n=0; n<256; n++)
  59.   {
  60.     if ((color[n].state == 1)
  61.       && (color[n].r == r)
  62.       && (color[n].g == g)
  63.       && (color[n].b == b))
  64.     {
  65.       color[n].n++;
  66.       return n;
  67.     }
  68.   }
  69.   for (n=0; n<256; n++)
  70.   {
  71.     if (color[n].state == 0)
  72.     {
  73.       color[n].n = 1;
  74.       color[n].state = 1;
  75.       GrSetColor(n, r, g, b);
  76.       return n;
  77.     }
  78.   }
  79.   return -1;
  80. }
  81.  
  82. int GrAllocCell()
  83. {
  84.   int n;
  85.   if (!initted)
  86.     init_colormap();
  87.   for (n=0; n<256; n++)
  88.   {
  89.     if (color[n].state == 0)
  90.     {
  91.       color[n].n = 1;
  92.       color[n].state = 2;
  93.       return n;
  94.     }
  95.   }
  96.   return -1;
  97. }
  98.  
  99. void GrSetColor(int n, int r, int g, int b)
  100. {
  101.   if (!initted)
  102.     init_colormap();
  103.   color[n].r = r;
  104.   color[n].g = g;
  105.   color[n].b = b;
  106.   _GrSetColor(n, r, g, b);
  107. }
  108.  
  109. void GrQueryColor(int n, int *r, int *g, int *b)
  110. {
  111.   if (!initted)
  112.     init_colormap();
  113.   *r = color[n].r;
  114.   *g = color[n].g;
  115.   *b = color[n].b;
  116. }
  117.  
  118. void GrFreeColor(int n)
  119. {
  120.   if (color[n].n > 0)
  121.   {
  122.     color[n].n --;
  123.     if (color[n].n == 0)
  124.       color[n].state = 0;
  125.   }
  126. }
  127.  
  128. void GrRefreshColors()
  129. {
  130.   int i;
  131.   if (_GrCurMode < GR_320_200_graphics)
  132.     return;
  133.   if (!initted)
  134.     init_colormap();
  135.   for (i=0; i<256; i++)
  136.     if (color[i].state)
  137.       GrSetColor(i, color[i].r, color[i].g, color[i].b);
  138. }
  139.