[DGD]Switch colors On and Off
Chris Kaczor
zor at mediaone.net
Fri Nov 24 14:50:11 CET 2000
Here is the code I use to handle colors - it seems to perform pretty well so
far. I use this inside the kernel library code but it should be generic
enough to put into any other library.
The color syntax is {color setting:color setting:etc} - so an example string
would look like:
You are hit {red:blink}very{normal} hard!
The lowercase colors are foreground and the uppercase colors are
background - the tags should be able to go in just about any order inside
the brackets.
You should be able to alter this code a little bit and to have it work for
other color syntaxes instead.
The code also supports replacement strings (not fully tested or implemented
yet) so you can use a generic tag like {name} and the name of the person
viewing the string will be substituted in there.
The function could probably be optimized a little bit more but I haven't had
the inclination to try yet. =)
Zor
-------
#define ESC "\033"
string parse_string(string str)
{
string first_part;
string second_part;
string color_tags;
string background;
string foreground;
string attributes;
string sequence;
string replacement;
string *tags;
int index;
while (sscanf(str, "%s{%s}%s", first_part, color_tags, second_part) ==
3)
{
background = "";
foreground = "";
attributes = "";
replacement = "";
sequence = "";
tags = explode(color_tags, ":");
for (index = 0; index < sizeof(tags); index++)
{
switch (tags[index])
{
case "black": foreground = "30"; break;
case "red": foreground = "31"; break;
case "green": foreground = "32"; break;
case "yellow": foreground = "33"; break;
case "blue": foreground = "34"; break;
case "magenta": foreground = "35"; break;
case "cyan": foreground = "36"; break;
case "white": foreground = "37"; break;
case "normal": foreground = "0"; break;
case "BLACK": background = "40"; break;
case "RED": background = "41"; break;
case "GREEN": background = "42"; break;
case "YELLOW": background = "43"; break;
case "BLUE": background = "44"; break;
case "MAGENTA": background = "45"; break;
case "CYAN": background = "46"; break;
case "WHITE": background = "47"; break;
case "NORMAL": background = "0"; break;
case "bold":
if (attributes != "") attributes = attributes + ";";
attributes = attributes + "1"; break;
case "italic":
if (attributes != "") attributes = attributes + ";";
attributes = attributes + "3"; break;
case "underline":
if (attributes != "") attributes = attributes + ";";
attributes = attributes + "4"; break;
case "blink":
if (attributes != "") attributes = attributes + ";";
attributes = attributes + "5"; break;
case "reverse":
if (attributes != "") attributes = attributes + ";";
attributes = attributes + "7"; break;
case "name": replacement = player->query_name(); break;
}
}
if (attributes != "" && foreground != "") attributes =
attributes + ";";
if (foreground != "" && background != "") foreground =
foreground + ";";
if (attributes != "" || foreground != "" || background != "")
sequence = ESC + "[" + attributes + foreground + background
+ "m";
if (player->query_ansi_color())
str = first_part + sequence + replacement + second_part;
else
str = first_part + replacement + second_part;
}
return str;
}
-----Original Message-----
From: dgd-admin at list.imaginary.com
[mailto:dgd-admin at list.imaginary.com]On Behalf Of Risto Karjalainen
Sent: Friday, November 24, 2000 4:05 AM
To: dgd at list.imaginary.com
Subject: Re: [DGD]Switch colors On and Off
| I solved this by adding hooks into user->catch_tell (I use 2.4.5 mudlib)
| It calls the player->creplace(str) function to replace certain colour
| codes (such things as ^R for red, ^G for green etc) with the proper ansi
| sequences, or with nothing at all if no colour is required.
Isn't that terribly slow?! I tried this too on my codebase but I started
getting Too long evaluations more than I could handle so I gave up the idea.
Maybe my code was a bit too unoptimized and hack. Anyway, I'm curious how
you handle this.
Regards,
Risto Karjalainen
List config page: http://list.imaginary.com/mailman/listinfo/dgd
List config page: http://list.imaginary.com/mailman/listinfo/dgd
More information about the DGD
mailing list