Easy color for CircleMUD

From: Eduardo Gutierrez de O. (eduo@ciateq.mx)
Date: 07/02/96


Ok, I found Easy Color for Circle... It was sent originally by pi
<mud@proimages.proimages.com>, I hope this is what you guys were looking
for.

Greets
Mythago, MythagoMUD
148.207.20.1 4000
PS: Let me know if this was of any use to anyone, so I keep my archives or
start deleting them...:) Right now I have several thousand messages from
this list (although Eudora makes a great job of finding stuff..:)

-----------------------

After looking at Circle's color code, I decided something more was needed.

The following code provides an easy way to add color to text files, mob
descriptions, etc.

Here's how it works:

1. Put in the code
2. Wherever you want color, use \c followed by a two-digit number.

For example,
\c01Hello! \c10this is yellow.\c00

Will print Hello! in red, "this is yellow," in bold (extended ANSI) yellow,
and then turn off color (revert to default color).

Color sequence is red, green, yellow, blue, magenta, cyan, and white.

Color 1 is red, color 8 is bold red (just add 7), color 15 is red background.

For instance,

\c18\c07White on blue background

is just that.

NOTE that in a .c source file, you will need to specify it as "\\c##", two
backslashes, but in an external text file (motd, etc.) you only need one \.

Ex: when sending something to char --
sprintf(s,"\\c01Hi\\c00\r\n");

You can, of course, change c to whatever character you want, modify the
code, etc., to make it better suited to your needs.

BUGS:
The color code doesn't check to make sure that the buffer doesn't exceed
the maximum output buffer size, but since this is rarely a problem (would
require 6 full screens of text under bpl11) I didn't bother.

Lines getting chopped in the middle (e.g., "hi\c0" .. "1 there") and
output separately will not be colored correctly.


HOW DO I DO IT?

1. Add the following lines to comm.c:

(in header section)
#include "screen.h"

(in extern section)

void proc_color(char *inbuf);


In the function process_output (comm.c), look for the lines
/* add the extra CRLF if the person isn't in compact mode */
if (!t->connected.......  blah blah blah

and add these lines:

if(t->character)
	if(clr(t->character, C_NRM)
		proc_color(i);


(Since almost every one of the old CCRED() or whatever color macros used
C_NRM anyway, this is a blanket check -- if your color is NRM or above,
you get it, if not, you don't.  You can change C_NRM to complete or
sparse or whatever other value you want.)


Those lines should come before

/*
 * now, send the output .............
 */

(in case you've changed something)



2. Add "color.o" to the object list in the Makefile (after comm.o) and after
the comm.o dependencies lines add this:

color.o: color.c
	$(CC) -c $(CFLAGS) color.c


3. Save this file as "color.c" in the src directory:

------ clip here ------

/* color.c */

#define CNRM  "\x1B[0;0m"
#define CRED  "\x1B[31m"
#define CGRN  "\x1B[32m"
#define CYEL  "\x1B[33m"
#define CBLU  "\x1B[34m"
#define CMAG  "\x1B[35m"
#define CCYN  "\x1B[36m"
#define CWHT  "\x1B[37m"
#define CNUL  ""

#define BRED  "\x1B[1;31m"
#define BGRN  "\x1B[1;32m"
#define BYEL  "\x1B[1;33m"
#define BBLU  "\x1B[1;34m"
#define BMAG  "\x1B[1;35m"
#define BCYN  "\x1B[1;36m"
#define BWHT  "\x1B[1;37m"

#define BKRED  "\x1B[41m"
#define BKGRN  "\x1B[42m"
#define BKYEL  "\x1B[43m"
#define BKBLU  "\x1B[44m"
#define BKMAG  "\x1B[45m"
#define BKCYN  "\x1B[46m"
#define BKWHT  "\x1B[47m"

const char *COLORLIST[] = {CNRM,CRED,CGRN,CYEL,CBLU,CMAG,CCYN,CWHT,
			   BRED,BGRN,BYEL,BBLU,BMAG,BCYN,BWHT,
			   BKRED,BKGRN,BKYEL,BKBLU,BKMAG,BKCYN,BKWHT};
#define MAX_COLORS 21


int isnum(char s)
{
  return( (s>='0') && (s<='9') );
}

void proc_color(char *inbuf)
{
	register int j=0,p=0;
	int c,k,max;
	char out_buf[32768];

	if(inbuf[0] == '\0') return;

	while(inbuf[j]!='\0')
	{
		if(  (inbuf[j]=='\\') && (inbuf[j+1]=='c')
			 && isnum(inbuf[j+2]) && isnum(inbuf[j+3]) )
		{
	      		c=(inbuf[j+2]-'0')*10 + inbuf[j+3]-'0';
			if(c>MAX_COLORS) c = 0;
			max=strlen(COLORLIST[c]);
			j+=4;
			for(k=0;k<max;k++)
			 { out_buf[p] = COLORLIST[c][k]; p++; }
		 }
		 else {out_buf[p] = inbuf[j]; j++;p++;}
	}

	out_buf[p] = '\0';

	strcpy(inbuf, out_buf);
}



------ clip here ------

Of course, like any code, this can be improved.... but hey, it's FREE!

Use it as you will, crediting me is not necessary (but appreciated).

The other great thing about it is if you're playing around with ANSI
codes a lot (like I do), you don't have to recompile a bunch of stuff
after changing a header file (screen.h).

Just make it again, which will only recompile color.c, yaay, lots of time
saved for more important stuff!

(BTW: CNUL isn't even used in the function, but it's there if you need
it...)

The other nice thing about this is that you don't have to change all your
CC macro calls (CCRED(), CCBLU(), etc.), this integrates right in.  Saves
you time adding more color later if you already have the CC's all over
the place... ;)


Now it's possible that I missed something or screwed something up in my
listing or my directions, let me know if you have any problems.


- three point ...

---------------------------------------------------------------------------
Eduardo Gutierrez de Oliveira                  eduo@sparc.ciateq.conacyt.mx

Administrador de Internet                           Internet Administrator
 Atencion a Clientes y Soporte Tecnico            Internet Service Provider

                 CIATEQ, A.C.   <--->   CIATEQ-INTERNET
 Centro de Investigacion y Asistencia Tecnica del Estado de Queretaro, A.C.

                     http://sparc.ciateq.conacyt.mx/
---------------------------------------------------------------------------



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