On the Fly Ansi Color Parser

From: Wheel of Fish! (piraeus@connectus.com)
Date: 03/13/95


ok. here it is.. a couple of things first though:

1: it's ugly, it can prolly be tightened down, etc.. BUT it works like a
charm

2: You can if you wish totally replace the standard Circle color functions
with this if you care to take the time.. it takes some work and it's pretty
tedious but persoanlly I think it's worth the time.. it ends up removing the
color sparse and what have ya (leaves ON or OFF) but hell..if yer gonna have
color why bother with sparse :)

3: I'll detail how to use it at the bottom of the file

first the changes to comm.c:

in: void process_output

  static char i[LARGE_BUFSIZE + GARBAGE_SPACE];
  static int result;
/* we add the following line just to make the compiler shut up :) */
+  extern char *fix_color(struct char_data *ch, char *src); 
+  char *col2;

/* I suppose you don't REALLY need to do this but.. */
+  col2 = fix_color(t->character, t->output);
  
  /* we may need this \r\n for later -- see below */
  strcpy(i, "\r\n");

  /* now, append the 'real' output */
- strcpy(i + 2, t->output);
/* we remove this line and add the one below for obvious reasons */
  strcpy(i + 2, col2);


thats it for comm.c.. now for utils.c (or wherever you decide to place it)

add this at the top (or wherever .. the top si where I figure it belongs):

#define colorcode(x) ( (x>=64 && x<=127) ? de_col[x-64] : 0 ) 

char de_col[] =
{                               /* Not beautiful, but efficient :) */
  0, 0, '4', '6',
  0, 0, 0, '2',
  0, 0, 0, 0,
  '0', '5', 0, 0,
  0, 0, '1', 0,
  0, 0, 0, '7',
  0, '3', 0, 0,
  0, 0, 0, 0,
  0, 0, '4', '6',
  0, 0, 0, '2',
  0, 0, 0, 0,
  '0', '5', 0, 0,
  0, 0, '1', 0,
  0, 0, 0, '7',
  0, '3', 0, 0,
  0, 0, 0, 0,
};

then drop this in at the bottom of utils.c (or wherever again):

char *fix_color (struct char_data *ch, unsigned char *src)
{

char *dest;
static char str5[LARGE_BUFSIZE + GARBAGE_SPACE];
int flash = 0;
int changed = 0;
dest = str5;

  while (*src != 0) {
      if (*src != '&') {
	  *dest++ = *src++;
      } else {
        if (*(src + 1) == '*') {
          flash = 1;
          src++;
        }
	  switch (*(src + 1)) {
	    case '&':
	      src += 2;
	      *dest++ = '&';
	      continue;

	    case '+':
		changed = 1;
	      if (colorcode (*(src + 2))) {
		  if (clr(ch, 1)) {
                    if (flash) {
                      strcpy((char *) dest, "\033[1;5;30m");
		      dest[7] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 9;
                    } else {
                      strcpy ((char *) dest, "\033[1;30m");
		      dest[5] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 7;
                    }
		  }
		  src += 3;
		  continue;
 	      } else {
		  *dest++ = *src++;
		  continue;
	      }
	    case '-':
		changed = 1;
	      if (colorcode (*(src + 2))) {
		  if (clr(ch, 1)) {
                    if (flash) {
                      strcpy((char *) dest, "\033[1;5;40m");
		      dest[7] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 9;
                    } else {
		      strcpy ((char *) dest, "\033[1;40m");
		      dest[5] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 7;
		    }
		  }
		  src += 3;
		  continue;
	      } else {
		  *dest++ = *src++;
		  continue;
	      }
	    case '=':
		changed = 1;
	      if (colorcode (*(src + 2)) && colorcode (*(src + 3))) {
		  if (clr(ch, 1)) {
                    if (flash) {
                      strcpy((char *) dest, "\033[1;5;40;30m");
		      dest[7] = colorcode (*(src + 3));
		      dest[9] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 12;
                    } else {
		      strcpy ((char *) dest, "\033[1;40;30m");
		      dest[5] = colorcode (*(src + 3));
		      dest[8] = colorcode (*(src + 2));
		      if (*(src + 2) >= 96)
			dest[2] = '0';
		      dest += 10;
		    }
		  }
		  src += 4;
		  continue;
	      } else {
		  *dest++ = *src++;
		  continue;
              }

	    default:
	      *dest++ = *src++;
	      continue;
	    }
	}
    }
   if(changed == 1)
     strcpy ((char *) dest, "\033[0m\0");
   else
     strcpy((char *) dest, "\0");
     
   return(str5);
}


now good programming dictates that you PROTOTYPE this but I suppose you
don't have to.. I did just because :)

now then... basically you have the following options:

&+r &-r 
&+b &-b		placing a * after the & will cause these to flash
&+g &-g		making the letter a capital will cause BOLD
&+l &-l		+ is standard color - is highlighted
&+c &-c		You may also use &=#? where # is the letter of
&+y &-y		the foreground color and ? is the backgrond color
&+m &-m		you may use the * with the = option as well
&+w &-w		any colored strings are terminted with a return
		to normal text (ie. \033[0m) and a null char (\0)
		any UNcolored strings are terminated with a null char


that should do it I think .. if ya have any questions lemme know.

P.S. some of you may recognize the BASE of this code from that crap trinity
posted a while back on r.g.m.d.. I figured what the hell .. saves me some
time.. so I re-wrote it and made it work (his original code contained
several NASTY bugs that wouldn't work on ANY mud.. end of story)

Brett



This archive was generated by hypermail 2b30 : 12/07/00 PST