Colour For Circle (More) [by Jorgen Sigvardsson]
Snippet Posted Wednesday, August 12th @ 11:25:33 PM, by George Greer in the Utils dept.
Added Jan 16, 1997. Click the link below to read it or download it.

From: Jorgen Sigvardsson <di4sig@sce.hks.se>
Subject: Color Codes


Instructions
  * Edit your comm.c file.
  * Add these function to comm.c

int count_chars(char *txt, char character)
{
   int i, cnt = 0;

   for(i = 0; txt[i]; i++)
      if(txt[i] == character)
         cnt++;

   return cnt;
}

char *parse_color(char *txt, struct descriptor_data *t)
{
   char *new_txt;
   char *toret;
   register int i, j = 0;

   i = count_chars(txt, '&'); /* count how many control-chars there
                                 are in the string */


   new_txt = malloc(i * 5 + strlen(txt) + 1); /* no ansi-escape code is larger
                                                 than 5 bytes so a 5 * times
                                                 the '&' appears + strlen(txt)
                                                 + 1 character big buffer
                                                 should be enough */
   /* the parser.. Huge but fast */
   for(i = 0; txt[i]; i++)
   {
      if(txt[i] == '&')
      {
         i++;

         switch(txt[i])
         {
            case '0' : strcpy(new_txt + j, CCNRM(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 4;
            break;
            case '1' : strcpy(new_txt + j, CCRED(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '2' : strcpy(new_txt + j, CCGRN(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '3' : strcpy(new_txt + j, CCYEL(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '4' : strcpy(new_txt + j, CCBLU(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '5' : strcpy(new_txt + j, CCMAG(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '6' : strcpy(new_txt + j, CCCYN(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '7' : strcpy(new_txt + j, CCWHT(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '8' : strcpy(new_txt + j, CCBLD(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 4;
            break;
            case '9' : strcpy(new_txt + j, CCBLK(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'u' : strcpy(new_txt + j, CCUND(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 4;
            break;
            case 'd' : strcpy(new_txt + j, CCDAR(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 4;
            break;
            case 'b' : strcpy(new_txt + j, CCBLD(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 4;
            break;

            case 'R' : strcpy(new_txt + j, CCBRED(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'G' : strcpy(new_txt + j, CCBGRN(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'Y' : strcpy(new_txt + j, CCBYEL(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'B' : strcpy(new_txt + j, CCBBLU(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'M' : strcpy(new_txt + j, CCBMAG(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'C' : strcpy(new_txt + j, CCBCYN(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'W' : strcpy(new_txt + j, CCBWHT(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case 'S' : strcpy(new_txt + j, CCBBLK(t->character, C_NRM));
            if(clr(t->character, C_NRM))
               j += 5;
            break;
            case '&' : new_txt[j] = txt[i]; j++; break;

            default:  break;
         }

      }
      else
      {
         new_txt[j] = txt[i]; j++;
      }
   }

   new_txt[j] = '\0'; /* terminate the string */
   toret = strdup(new_txt); /* create a new string with no eventual memoryloss
*/
   free(new_txt); /* free the old buffer */

   return toret; /* the colorized buffer */
}

 * Change the function write_to_output(const char *txt, struct descriptor_data
*t)
   to this:

void write_to_output(const char *txt, struct descriptor_data *t)
{
  int size;
  char *new_txt;

  new_txt = parse_color(txt, t);

  size = strlen(new_txt);

  /* if we're in the overflow state already, ignore this new output */
  if (t->bufptr < 0)
    return;

  /* if we have enough space, just write to buffer and that's it! */
  if (t->bufspace >= size) {
    strcpy(t->output + t->bufptr, new_txt);
    t->bufspace -= size;
    t->bufptr += size;
    free(new_txt);
    return;
  }
  /*
   * If we're already using the large buffer, or if even the large buffer
   * is too small to handle this new text, chuck the text and switch to the
   * overflow state.
   */
  if (t->large_outbuf || ((size + strlen(t->output)) > LARGE_BUFSIZE)) {
    t->bufptr = -1;
    buf_overflows++;
    free(new_txt);
    return;
  }
  buf_switches++;

  /* if the pool has a buffer in it, grab it */
  if (bufpool != NULL) {
    t->large_outbuf = bufpool;
    bufpool = bufpool->next;
  } else {                      /* else create a new one */
    CREATE(t->large_outbuf, struct txt_block, 1);
    CREATE(t->large_outbuf->text, char, LARGE_BUFSIZE);
    buf_largecount++;
  }

  strcpy(t->large_outbuf->text, t->output);     /* copy to big buffer */
  t->output = t->large_outbuf->text;    /* make big buffer primary */
  strcat(t->output, new_txt);   /* now add new text */

  /* calculate how much space is left in the buffer */
  t->bufspace = LARGE_BUFSIZE - 1 - strlen(t->output);

  /* set the pointer for the next write */
  t->bufptr = strlen(t->output);

  free(new_txt);
}

 * My screen.h

/* ************************************************************************
*   File: screen.h                                      Part of CircleMUD *
*  Usage: header file with ANSI color codes for online color              *
*                                                                         *
*  All rights reserved.  See license.doc for complete information.        *
*                                                                         *
*  Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
*  CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.               *
************************************************************************ */

#define KNRM  "\033[0m"
#define KRED  "\033[31m"
#define KGRN  "\033[32m"
#define KYEL  "\033[33m"
#define KBLU  "\033[34m"
#define KMAG  "\033[35m"
#define KCYN  "\033[36m"
#define KWHT  "\033[37m"
#define KBLK  "\033[30m"
#define KBLD  "\033[1m"
#define KBLN  "\033[5m"
#define KNUL  ""
#define KCLR  "\033[2J"
#define KUND  "\033[4m"
#define KDAR  "\033[2m"

#define KBRED  "\033[41m"
#define KBGRN  "\033[42m"
#define KBYEL  "\033[43m"
#define KBBLU  "\033[44m"
#define KBMAG  "\033[45m"
#define KBCYN  "\033[46m"
#define KBWHT  "\033[47m"
#define KBBLK  "\033[40m"

/* conditional color.  pass it a pointer to a char_data and a color level. */
#define C_OFF   0
#define C_SPR   1
#define C_NRM   2
#define C_CMP   3
#define _clrlevel(ch) ((PRF_FLAGGED((ch), PRF_COLOR_1) ? 1 : 0) + \
                       (PRF_FLAGGED((ch), PRF_COLOR_2) ? 2 : 0))
#define clr(ch,lvl) (_clrlevel(ch) >= (lvl))
#define CCNRM(ch,lvl)  (clr((ch),(lvl))?KNRM:KNUL)
#define CCBLK(ch,lvl)  (clr((ch),(lvl))?KBLK:KNUL)
#define CCRED(ch,lvl)  (clr((ch),(lvl))?KRED:KNUL)
#define CCGRN(ch,lvl)  (clr((ch),(lvl))?KGRN:KNUL)
#define CCYEL(ch,lvl)  (clr((ch),(lvl))?KYEL:KNUL)
#define CCBLU(ch,lvl)  (clr((ch),(lvl))?KBLU:KNUL)
#define CCMAG(ch,lvl)  (clr((ch),(lvl))?KMAG:KNUL)
#define CCCYN(ch,lvl)  (clr((ch),(lvl))?KCYN:KNUL)
#define CCWHT(ch,lvl)  (clr((ch),(lvl))?KWHT:KNUL)
#define CCBLD(ch,lvl)  (clr((ch),(lvl))?KBLD:KNUL)
#define CCBLN(ch,lvl)  (clr((ch),(lvl))?KBLN:KNUL)
#define CCCLR(ch,lvl)  (clr((ch),(lvl))?KCLR:KNUL)
#define CCUND(ch,lvl)  (clr((ch),(lvl))?KUND:KNUL)
#define CCDAR(ch,lvl)  (clr((ch),(lvl))?KDAR:KNUL)

#define CCBBLK(ch,lvl)  (clr((ch),(lvl))?KBBLK:KNUL)
#define CCBRED(ch,lvl)  (clr((ch),(lvl))?KBRED:KNUL)
#define CCBGRN(ch,lvl)  (clr((ch),(lvl))?KBGRN:KNUL)
#define CCBYEL(ch,lvl)  (clr((ch),(lvl))?KBYEL:KNUL)
#define CCBBLU(ch,lvl)  (clr((ch),(lvl))?KBBLU:KNUL)
#define CCBMAG(ch,lvl)  (clr((ch),(lvl))?KBMAG:KNUL)
#define CCBCYN(ch,lvl)  (clr((ch),(lvl))?KBCYN:KNUL)
#define CCBWHT(ch,lvl)  (clr((ch),(lvl))?KBWHT:KNUL)

#define COLOR_LEV(ch) (_clrlevel(ch))

#define QNRM CCNRM(ch,C_SPR)
#define QBLK CCBLK(ch,C_SPR)
#define QRED CCRED(ch,C_SPR)
#define QGRN CCGRN(ch,C_SPR)
#define QYEL CCYEL(ch,C_SPR)
#define QBLU CCBLU(ch,C_SPR)
#define QMAG CCMAG(ch,C_SPR)
#define QCYN CCCYN(ch,C_SPR)
#define QWHT CCWHT(ch,C_SPR)
#define QBLD CCBLD(ch,C_SPR)
#define QBLN CCBLN(ch,C_SPR)
#define QCLR CCBLN(ch,C_SPR)
#define QUND CCUND(ch,C_SPR)
#define QDAR CCDAR(ch,C_SPR)
#define QBBLK CCBBLK(ch,C_SPR)
#define QBRED CCBRED(ch,C_SPR)
#define QBGRN CCBGRN(ch,C_SPR)
#define QBYEL CCBYEL(ch,C_SPR)
#define QBBLU CCBBLU(ch,C_SPR)
#define QBMAG CCBMAG(ch,C_SPR)
#define QBCYN CCBCYN(ch,C_SPR)
#define QBWHT CCBWHT(ch,C_SPR)

 * Compile and you're done.
 * How to use it + color-codes

How to use it:

Wherever whenever you feel like adding some color (for those who have
colorlevel >= NORMAL) you just do something like this:

&1Red&0 &4Blue&0 and so on.. Works in room_descs, gossips, tells, you name it.

Here's a list of the color-codes:

0 Normal attributes
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
8 bold
9 black

u underline
d dark
b bright
R red bground
G green brgound
B Blue bground
Y yellow brgound
M magenta bground
C Cyan bground
W white bground
S black bground (S as in Svart (swedish))
& the character &

Anything other than these characters, will produce nothing.


<< Colour For Circle [by Nashak] | Reply | View as text | Threaded | Colour For Circle (More - Updated) [by Jason Berg] >>

 


Related Links
  CircleMUD
download
Related Articles
More by greerga
 
 

CircleMUD Snippets
 
Note: Not all of these snippets will work perfectly with your version of code, so be prepared to fix one or two bugs that may arise, and please let me know what you needed to do to fix it. Sending a corrected version is always welcome.
Finally, if you wish to use any of the snippets from this page, you are more than welcome, just mention the authors in your credits. If you wish to release any of these snippets to the public on another site, contact me FIRST.