Re: Color Codes

From: Akuma/Chris Baggett/DOOMer (doomer@BAYOU.COM)
Date: 03/19/98


At 10:47 PM 3/19/98 -0500, George Greer wrote:
>
>Nope, but I'm sure people using it are interested.
>

Well, here it is.
my color.c file.
This is for all people whose color is done by typing  &##
where ## is a 2-digit number from 00 - 15 (and up).
now you can do up to &99 without having to add anything to an array.
btw, if anyone is using HEDIT and my help color addition,
they may need to have a strip_color_codes function for when they use HEDIT.
I have included the updated addition here.
----------------------------------------------------------------------------
strip_color_codes (prototype it in olc.h btw)
----------------------------------------------------------------------------
void strip_color_codes(char *string)
{
register char *ptr, *str;
 ptr = string;
 str = ptr;
while (*ptr == '&') {
  if (*(ptr + 1) && isdigit(*(ptr + 1)) && *(ptr + 2) && isdigit(*(ptr +
2))) {
        ptr += 3;
     }
  else if (*(ptr + 1) && *(ptr + 1) == '&')
     { *str = *ptr; str++; ptr++; break; }
  else break;
}

while ((*str = *ptr)) {
   str++;
   ptr++;
   while (*ptr == '&') {
     if (*(ptr + 1) && isdigit(*(ptr + 1)) && *(ptr + 2) && isdigit(*(ptr +
2))) {
           ptr += 3;
        }
     else if (*(ptr + 1) && *(ptr + 1) == '&')
        { *str = *ptr; str++; ptr++; break; }
     else break;
     }
   }
}
 ===========================================================================
/* color.c */

#include "conf.h"
#include "sysdep.h"

#define MAX_COLORS 100
#define ABS(a) ((a < 0) ? (-a) : (a))
#define MAX(a, b) ((a > b) ? (a) : (b))
#define MIN(a, b) ((a < b) ? (a) : (b))

/*         Underline, Flashing         */
#define UNDER  "\x1B[4m"        /* %25 */
#define FLASH  "\x1B[5m"        /* %26 */

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

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

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

  while (inbuf[j]!='\0') {
    if (inbuf[j] == '&' && isnum(inbuf[j+1]) && isnum(inbuf[j+2])) {
       c = (inbuf[j+1]-'0')*10 + inbuf[j+2]-'0';
       j += 3;
       if (!color)
          continue;
       c = MAX(0, MIN(c, (MAX_COLORS-1)));
       if ((c % 16) >= 8)
          p += sprintf(out_buf + p, "\x1B[1;");
       else if ((c % 16) < 8)
          p += sprintf(out_buf + p, "\x1B[0;");
       p += sprintf(out_buf + p, "3%d", c % 8);
       if ((c / 16) != 0)
          p += sprintf(out_buf + p, ";4%dm", (c / 16));
       else p+= sprintf(out_buf + p, "m");
       }
    else if (inbuf[j] == '&' && inbuf[j+1] == '&')
       { out_buf[p] = inbuf[j]; j += 2; p++; } // so typing &&09 prints &09
(not color)
    else { out_buf[p] = inbuf[j]; j++; p++; }
  }

  out_buf[p] = '\0';

  strcpy(inbuf, out_buf);
}

Sample Colorlist Function
-----------------------------
ACMD(do_colorlist) {
int i = 0;
   strcpy(buf, "Colors &&00 - &&99 : \r\n");
   for (i = 0; i < 100; i++) {
       sprintf(buf + strlen(buf), "&%.2d&&%.2d", (int)i, (int)i);
       if (!((i+1) % 16))
          strcat(buf, "&07\r\n");
       }
   send_to_char(buf, ch);
}
-----------------------------------------------------------------------
This should all add in easily enough,
although if you have made any changes to the color.c file,
try and find a way to add back in those changes.

anyway, good luck, and as always i'm not responsible for blah blah blah.
also, please give credit where credit is due.
especially since I saved you the trouble of having to memorize
ANSI codes :-)

Code On
Akuma the Raging Coder

  +------------------------------------------------------------+
  | "The poets talk about love, but what I talk about is DOOM, |
  |      because in the end, DOOM is all that counts." -       |
  |   Alex Machine/George Stark/Stephen King, The Dark Half    |
  |        "Nothing is IMPOSSIBLE, Just IMPROBABLE"            |
  |   "Easier Said Than Done, But Better Done Than Said..."    |
  +------------------------------------------------------------+


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



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