home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / bomb.tar.gz / bomb.tar / bomb / cmap.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  2KB  |  102 lines

  1. /*
  2.     screen hacks
  3.     Copyright (C) 1992  Scott Draves <spot@cs.cmu.edu>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "cmap.h"
  23.  
  24. int ncmaps;
  25. int the_cmaps[maxcmaps][256][3];
  26.  
  27. int
  28. next_contrasting_color(int c, int i, int d)
  29. {
  30.    int nchecked = 0;
  31.    int j = i;
  32.    int dd;
  33.    int max_i = 0;
  34.    int max = 0;
  35.    while (nchecked++ < 250) {
  36.       int t;
  37.       j = (j+1)%256;
  38.  
  39.       t = (the_cmaps[c][i][0] - the_cmaps[c][j][0]);
  40.       dd = t * t;
  41.       t = (the_cmaps[c][i][1] - the_cmaps[c][j][1]);
  42.       dd += t * t;
  43.       t = (the_cmaps[c][i][2] - the_cmaps[c][j][2]);
  44.       dd += t * t;
  45.  
  46.       if (dd > max) {
  47.      max_i = j;
  48.      max = dd;
  49.       }
  50.       if (dd >= d)
  51.      return j;
  52.    }
  53.    return max_i;
  54. }
  55.  
  56. void
  57. init_cmaps()
  58. {
  59.    FILE *in;
  60.    char tag[10];
  61.    char buf[1000];
  62.    ncmaps = 0;
  63.  
  64.    if (getenv("quick"))
  65.       goto quick;
  66.  
  67.    sprintf(buf, "%scmap-data", DATA_DIR);
  68.    in = fopen(buf, "r");
  69.    if (NULL == in) {
  70.        message("in is NULL");
  71.    }
  72.    while (1 == fscanf(in, " ( %s9", tag)) {
  73.       int i;
  74.       if (!strcmp(tag, "comment")) {
  75.      while (')' != fgetc(in));
  76.       } else if (!strcmp(tag, "cmap")) {
  77.      for (i = 0; i < 256; i++) {
  78.         int *entry = &the_cmaps[ncmaps][i][0];
  79.         fscanf(in, " ( %d %d %d )", entry, entry+1, entry+2);
  80.      }
  81.      fscanf(in, " )");
  82.      ncmaps++;
  83.      if (ncmaps == maxcmaps) {
  84.         fprintf(stderr, "maxcmaps=%d exceeded, truncating\n", maxcmaps);
  85.         return;
  86.      }
  87.       } else
  88.      fprintf(stderr, "bad tag: %s\n", tag);
  89.    }
  90.  quick:
  91.    if (0 == ncmaps) {
  92.       int i;
  93.       fprintf(stderr, "no cmaps found, using graymap\n");
  94.       for (i = 0; i < 256; i++) {
  95.      the_cmaps[0][i][0] = i;
  96.      the_cmaps[0][i][1] = i;
  97.      the_cmaps[0][i][2] = i;
  98.       }
  99.       ncmaps = 1;
  100.    }
  101. }
  102.