[DGD]Switch colors On and Off

Matthew Jenkins matt at esb.co.uk
Mon Nov 27 10:29:24 CET 2000



On Fri, 24 Nov 2000, Risto Karjalainen wrote:

> | 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.

I haven't noticed any particular speed degradation...

The colour code system I use is pretty much lifted from PG+ (telnet based
talkers) which have had this kind of colour for may years now.

Here's my code snippett...

--CUT HERE--

rand(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;3"+random(7)+"m";
        return "^[[1;3"+random(7)+"m";
}
    
grey(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;30m";
        return "^[[1;30m";
}
        
red(i)   
{
        if(colour==0) return "";
        if(i==0) return "^[[0;31m";
        return "^[[1;31m";
}
     
green(i)      
{
        if(colour==0) return "";
        if(i==0) return "^[[0;32m";
        return "^[[1;32m";
}
     
yellow(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;33m";
        return "^[[1;33m";
}

blue(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;34m";
        return "^[[1;34m";
}

pink(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;35m";
        return "^[[1;35m";
}

cyan(i)
{
        if(colour==0) return "";
        if(i==0) return "^[[0;36m";
        return "^[[1;36m";
}
norm()
{
        if(colour==0) return "";
        return "^[[0m";
}

italic() /* S */
{
        if(colour==0) return "";
        return "^[[3m";
}

blink() /* K */
{
        if(colour==0) return "";
        return "^[[5m";
}

underline() /* U */
{
        if(colour==0) return "";
        return "^[[4m";
}

inverse() /* I */
{
        if(colour==0) return "";
        return "^[[7m";
}

bold()  /* H */
{
        if(colour==0) return "";
        return "^[[0;1m";
}

creplace(haystack)
{
        string newstr;
        int i;
        newstr="";

        for(i=0; i<(strlen(haystack)); i++)
        {
                if(haystack[i]=='^')
                {
                        if(haystack[i+1]=='^')
                        {
                                newstr = newstr + "^";
                                i++;
                        } else {
                                i++;
                                switch (haystack[i])
                                {
                                        case 'G':
                                                newstr = newstr +
green(1);
                                                break;
                                        case 'g':
                                                newstr = newstr +
green(0);
                                                break;
                                        case 'R':
                                                newstr = newstr + red(1);
                                                break;
                                        case 'r':
                                                newstr = newstr + red(0);
                                                break;
                                        case 'B':
                                                newstr = newstr + blue(1);
                                                break;
                                        case 'b':
                                                newstr = newstr + blue(0);
                                                break;
                                        case 'Y':
                                                newstr = newstr +
yellow(1);
                                                break;
                                        case 'y':
                                                newstr = newstr +
yellow(0);
                                                break;
                                        case 'C':
                                                newstr = newstr + cyan(1);
                                                break;
                                        case 'c':
                                                newstr = newstr + cyan(0);
                                                break;
                                        case 'P':
                                                newstr = newstr + pink(1);
                                                break;
                                        case 'p':
                                                newstr = newstr + pink(0);
                                                break;
                                        case 'N':
                                        case 'n':
                                                newstr = newstr + norm();
                                                break;
                                        case 'A':
                                                newstr = newstr + grey(1);
                                                break;
                                        case 'a':
                                                newstr = newstr + grey(0);
                                                break;
                                        case 'X':
                                                newstr = newstr + rand(1);
                                                break;
                                        case 'x':
                                                newstr = newstr + rand(0);
                                                break;
                                        case 'S':
                                        case 's':
                                                newstr = newstr + italic();
                                                break;
                                        case 'K':
                                        case 'k':
                                                newstr = newstr + blink();
                                                break;
                                        case 'U':
                                        case 'u':
                                                newstr = newstr + underline();
                                                break;
                                        case 'I':
                                        case 'i':
                                                newstr = newstr + inverse();
                                                break;
                                        case 'H':
                                        case 'h':
                                                newstr = newstr + bold();
                                                break;
                                }
                        }
                } else {
                        newstr = newstr + " ";
                        newstr[strlen(newstr)-1] = haystack[i];
                }
        }
        return newstr;
}

--CUT HERE--

Matt


List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list