Here's a slightly hacky-way of doing what you want.
==========================================
char *colorize_string (const char *name, const char *colors)
{
static char buf[MSL];
char color_char = '&';
int pos = 0;
int name_pos = 0;
int length;
int Color1;
int Color2;
int Color3;
buf[0] = '\0';
if (!name || !colors || strlen(name) < 5 || strlen(colors) < 3)
return const_cast<char *>(name);
length = strlen(name);
Color1 = 1;
Color2 = UMAX(length / 4, 1);
Color3 = length - (2 * (Color1 + Color2));
buf[pos++] = color_char;
buf[pos++] = colors[0];
for (int i = Color1; i; i--)
buf[pos++] = name[name_pos++];
buf[pos++] = color_char;
buf[pos++] = colors[1];
for (int i = Color2; i; i--)
buf[pos++] = name[name_pos++];
buf[pos++] = color_char;
buf[pos++] = colors[2];
for (int i = Color3; i; i--)
buf[pos++] = name[name_pos++];
buf[pos++] = color_char;
buf[pos++] = colors[1];
for (int i = Color2; i; i--)
buf[pos++] = name[name_pos++];
buf[pos++] = color_char;
buf[pos++] = colors[0];
for (int i = Color1; i; i--)
buf[pos++] = name[name_pos++];
buf[pos] = '\0';
return buf;
}
==========================================
Perks:
- Easy to change styles. Simply comment out
Color2 = UMAX(length / 4, 1);
and replace with:
Color2 = 1;
to get style 1. The above code does style 3 (my own preferred choice). To
change
to style 2, make Color1:
Color1 = UMAX(length / 6, 1);
or something (I don't know offhand. Not hard to figure out. No more
hand-holding).
Caveats:
- Doesn't handle names 4 characters or less; hardcode that in yourself.
- Only handles three color characters, any less results in an error, any more is
ignored.
- Not blazingly efficient.
Oh, don't forget to remove the const_cast thing there. I compile my MUD with
g++.
----- Original Message -----
From: "Robin Björklund" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, May 06, 2004 5:22 PM
Subject: Re: colorize_string
> Thank you, that function worked great and come to think about it I can
> have use for it too. =) However as you said you know that's not what I
> wanted.
> So, let me explain for you exactly what I'm trying to do. I want to
> highlight a name with a color scheme. So the first part of the name
> would start with dark colors, the middle part with bright colors and the
> last part back to dark colors. This can be done with different type of
> colors. I will use grey, white and bright white in this example and the
> name I use in this example is "Desindit".
>
> I call the function by:
> colorize_string( "name", "DwW" ); // D = Grey, w = White, W =
> BrightWhite
>
> The result I would want is:
>
> D is colored as &D
> e is colored as &w
> s is colored as &W
> i is colored as &W
> n is colored as &W
> d is colored as &W
> i is colored as &w
> t is colored as &D
>
> This is fine too:
>
> D is colored as &D
> e is colored as &D
> s is colored as &w
> i is colored as &W
> n is colored as &W
> d is colored as &w
> i is colored as &D
> t is colored as &D
>
> Even this:
>
> D is colored as &D
> e is colored as &w
> s is colored as &w
> i is colored as &W
> n is colored as &W
> d is colored as &w
> i is colored as &w
> t is colored as &D
>
> However, the result is best from example 1 or 2.
>
> You're asking about 9 or 5 character names. Yes ok, solution to that is
> to always color the letter that's left from dividing the character name
> into 2 parts to the last color of first part or the first color of the
> last part which should be same. :) However if the name is 4 or 3
> characters, things won't really work if we use 3 colors to hightlight a
> string. Solution to that would maybe be not to allow colorizing of a
> such string or we could always make an exception to a such string and do
> a little hack by colorizing it a "hardcoded way".
>
> Ok, ok, things got more complicated than I thought. I do not know how to
> continue from here. Yes ok, split the name into two parts.
> Colorize the first part &D &w &W and if it's an odd character number
> colorize the letter that's left after dividing it by 2 to &W.
> Now to the last part, colorize it &W &w &k.
> Hmm when I think about it what about 5 character names...
> Have to make an exception here too, since &D &w &W &w &D is a total of 5
> characters and is odd... so can't really colorize the letter that's left
> to
> the same color that the first part "&D &w" starts with or the first
> color of the last part "&w &D" as that would result in "&k &w &w &w &k".
> Bah, should I just give up?? :P
>
>
> --
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom