[go: up one dir, main page]

Menu

[897c37]: / mlib / std / colour.lpc  Maximize  Restore  History

Download this file

133 lines (116 with data), 4.2 kB

/* Dodinas's Colour Server

Shattered World Colour Codes
----------------------------
    {r = dark red       {R = bright red
    {g = dark green     {G = bright green
    {y = dark yellow    {Y = bright yellow
    {b = dark blue      {B = bright blue
    {p = dark purple    {P = bright purple
    {c = dark cyan      {C = bright cyan
    {w = grey           {W = white
    {k = black          }  = reset to standard

    {!r = inverse red   {!p = inverse purple
    {!g = inverse green {!c = inverse cyan
    {!y = inverse yellow{!w = inverse grey
    {!b = inverse blue
    
    Specials:
    {#l = coloured line
    {#h = heading colour
    {#s = syntax colour (eg. for hilighting: type 'look')
    {#c = colour another standard colour

*/

/* Trax - added result function 010613 */

#include <ansi.h>
#define STRINGS "RO/lib/strings"

array colours = [   "r",ESC+"[0m"+ESC+"[31;2m","g",ESC+"[0m"+ESC+"[32;2m",
                    "y",ESC+"[0m"+ESC+"[33;2m","b",ESC+"[0m"+ESC+"[34;2m",
                    "p",ESC+"[0m"+ESC+"[35;2m","c",ESC+"[0m"+ESC+"[36;2m",
                    "w",ESC+"[0m"+ESC+"[37;2m","k",ESC+"[0m"+ESC+"[30;1m",
                    "R",ESC+"[31;1m","G",ESC+"[32;1m","Y",ESC+"[33;1m",
                    "B",ESC+"[34;1m","P",ESC+"[35;1m","C",ESC+"[36;1m",
                    "W",ESC+"[39;1m","!r",ESC+"[41;1m","!g",ESC+"[42;1m",
                    "!y",ESC+"[43;1m","!b",ESC+"[44;1m","!p",ESC+"[45;1m",
                    "!c",ESC+"[46;1m","!w",ESC+"[47;1m",
                    "#l",ESC+"[32;1m"
                    +"----------------------------------------------------------------------------"
                    +ESC+"[0m","#s",ESC+"[33;1m","#c",ESC+"[32;1m" ];

/* Added by Trax 010613 */
result((array|int|string) str, pob) {
    if (!pob) pob=this_player();
    if (pob->query_store("hilite_on"))
        return colour(str, pob);
    else
    	return stripcolour(str, pob);
}

/* This function takes a string, replaces the colour codes with
   ansi colour characters, and returns the coloured string. */
colour((array|int|string) str, pob) {
    if (arrayp(str)) {
        int i;
        for (i=0;i < sizeof(str); i++) {
            str[i] = convertcolour(str[i], pob);
        }
    }
    
    if (stringp(str)) str = convertcolour(str, pob);
    return str;
}

stripcolour((array|int|string) str, pob) {
    if (arrayp(str)) {
        int i;
        for (i=0;i < sizeof(str); i++) {
            str[i] = nocolour(str[i], pob);
        }
    }
    
    if (stringp(str)) str = nocolour(str, pob);
    return str;
}

convertcolour(str, pob) {
    /* Heading Colour is Dark Green */
    int pos, x, i, j;
    if (!pob) pob = this_player();
    x = pob->query_store("hilite_things");
    if (x > 100)
        str = STRINGS->string_replace(str,"{#h",(ESC+"[0m"+ESC+"[32;2m"));
    else
        str = STRINGS->string_replace(str,"{#h",(ESC+"[0m"+ESC+"[32;1m"));
    for (i = 0; i < sizeof(str); i++) {
        if (str[i] == '}') {
            str = str[..i-1] + ESC + "[0m" + str[i+1..];
            continue;
        }
        if (str[i] == '{') {
            if ((str[i+1] == '{') || (str[i+1] == '}')) {
                str = str[..i-1] + str[i+1..];
                continue;
            }
            for (j = 0; j < sizeof(colours); j = j+2) {
                if (str[i+1..(i+sizeof(colours[j]))] != colours[j][0..(sizeof(colours[j])-1)]) continue;
                str = str[..i-1] + colours[j+1] + str[i+1+sizeof(colours[j])..];
                break;
            } 
        }
    }
    str += ESC+"[0m";
    return str;
}


/* Strips the string of colour symbols */
nocolour(str, pob) {
    /* Heading Colour is Dark Green */
    int pos, i, j;
    str = STRINGS->string_replace(str,"{#h","");

    for (i = 0; i < sizeof(str); i++) {
        if (str[i] == '}') {
            str = str[..i-1] + str[i+1..];
            continue;
        }
        if (str[i] == '{') {
            for (j = 0; j < sizeof(colours); j = j+2) {
                if (str[i+1..(i+sizeof(colours[j]))] != colours[j][0..(sizeof(colours[j])-1)]) continue;
                str = str[..i-1] + str[i+1+sizeof(colours[j])..];
                break;
            } 
        }
    }
    str += ESC+"[0m";
    return str;
}