Shoutbox

Replace Colour Codes - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Replace Colour Codes (/showthread.php?tid=65950)

Replace Colour Codes by Spunky on 09-07-2006 at 08:20 PM

I'm trying to convert a string containing BBcode colours into the old 3.xx style. It seems to work partially (It converts the first few, but not others). This is the code I'm using at the moment:

code:
    for(var a=0;a<=54;a++){
    var before = "[c="+a+"]";
    var after = "·$"+a+" ";
    strMyNewPSM = strMyNewPSM.replace(before,after);
    }
    Debug.Trace(strMyNewPSM);   
    strMyNewPSM = strMyNewPSM.replace('[/c]','·$1');
    Messenger.MyPersonalMessage = strMyNewPSM;


Any ideas why its not working properly?

Also, I need to remove leading spaces... I know how to check for them, but not how to remove them.
RE: Replace Colour Codes by alexp2_ad on 09-07-2006 at 08:31 PM

JScript's replace function only replaces the first occurence in a string.  You can use regex (regular expressions) to get them all, but I'm only good with simple regex, so you can wait for a regex master to help. :P


RE: Replace Colour Codes by Spunky on 09-07-2006 at 08:32 PM

I'm just doing some research on that now... I've seen it used in other scripts and it baffles me :p

I'll let you know if I manage it ;)


RE: Replace Colour Codes by KnRd_WC on 09-07-2006 at 09:15 PM

Hi ! ;)

I don't know if it's the best way to do what you want, but maybe you can try this :

code:
var MyPSM = "[c=1]Color1[/c][c=2]Color2[/c]";
for(var a=0;a<=54;a++){
     RgEx = new RegExp("[[]+c="+a+"+[]]","gi");
     MyPSM = MyPSM.replace(RgEx,"·$"+a);
}
RgEx = new RegExp("[[]+/c+[]]","gi");
MyPSM = MyPSM.replace(RgEx,"·$");
MsgPlus.DisplayToast("",MyPSM);


I've got a lot of Web sites which explain how RegExp works... but they're only in French... :s
RE: Replace Colour Codes by deAd on 09-07-2006 at 09:17 PM

Replacing can also be done with a simple loop.

Here's a function I made that replaces the default String.replace function. I haven't tested it thoroughly, but I'm sure the bugs are nothing complicated ;)

To use it, use the normal String.replace function. However, there is an extra parameter. "Count" is an integer, it'll specify how many occurrences to replace in the string. If you leave it out, it will replace all of them.

code:
String.prototype.replace = function(term,replacement,count){
    var string = this;
    if(count == null){
        while(string.indexOf(term) > -1){
            string = string.slice(0,string.indexOf(term)) + replacement + string.slice(string.indexOf(term)+term.length,string.length);
        }
    } else if(typeof count == 'number'){
        for(var i = 0; i < count; i++){
            string = string.slice(0,string.indexOf(term)) + replacement + string.slice(string.indexOf(term)+term.length,string.length);
        }
    }
    return string;
}

RE: Replace Colour Codes by Spunky on 09-07-2006 at 09:18 PM

Thanks, been talking with SilentDragon on WLM and we seem to have sorted it out :D

I'll post the code for others that are looking for the same thing so it does not get asked again :p

code:
for(a=0;a<=54;a++){

    strMyNewPSM = strMyNewPSM.replace(new RegExp("\\[c="+a+"\\]",'gi'),"·$"+a+" ");
    strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/g,'·$1');
   
    }
    strMyNewPSM = strMyNewPSM.replace(/ [\s]/g,"");


EDIT: My code was using a loop to change EACH colour individually so putting it in another loop was the only alternative... After trying this I had to close WLM and edit the file in Notepad as it caused a wierd loop error :p
RE: Replace Colour Codes by Shondoit on 09-08-2006 at 03:42 PM

I believe this should work, it worked for me...

code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-4]?\d|5[0-4])\]/gi, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/gi, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");

And no lopes at all (y)

I had to search how to get the '$'-sign as the replace text, because $1-$9 are used for backreference... (putting part of the matched text in the replaced text)

If want/need to know how it works, just ask me on WLM sometimes...
RE: Replace Colour Codes by Matti on 09-08-2006 at 04:17 PM

In fact, there never was need to use a loop for it. By placing the moderator "g" (global) after the end slash of a regular expression, it'll look for all matches and replace all matches. ;)

Also, don't forget people can use hexadecimal codes like [c=#ff00ff]...[/c]!!! :o


RE: Replace Colour Codes by Shondoit on 09-08-2006 at 04:21 PM

Yeah, correct, for support for the hex codes:...

code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-4]?\d|5[0-4]|#[0-9A-F]{6})\]/gi, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/gi, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");


-edit- I just found out, that the 'c' in the tag is case sensitive, only lowercase is allowed and also the numbers go 'til 69 :S, therefore this would be a better solution
code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-6]?\d|#[0-9A-Fa-f]{6})\]/g, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/g, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");


RE: Replace Colour Codes by CookieRevised on 09-08-2006 at 04:35 PM

quote:
Originally posted by Mattike
Also, don't forget people can use hexadecimal codes like [c=#ff00ff]...[/c]!!! :o
and gradients, and background colors, ... and tags are only valid if there is a closing tag, ...and numbers can include a leading 0, and hexadecimal numbers are only read for the first 6 characters/ the rest is ignored but can exist! ...

It is not so strait forward to make something like this (but no impossible; you just need to think about all the possebilities there are).

RE: Replace Colour Codes by Shondoit on 09-08-2006 at 04:45 PM

I never saw any of those features in the old IRC codes of Msg+ but please correct me if I'm wrong...

If you want, you can erase them with this code

code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-6]?\d|#[0-9A-Fa-f]{6})\](.*?)\[\/c(?:=(?:[1-6]?\d|#[0-9A-Fa-f]{6}))\]/g, "·\$$$1$2·\$")
strMyNewPSM = strMyNewPSM.replace(/\[a=(?:[1-6]?\d|#[0-9A-Fa-f]{6})\](.*?)\[\/a(?:=(?:[1-6]?\d|#[0-9A-Fa-f]{6}))\]/g, "$1")
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");

If it finds a gradient only the starting color will be used...
RE: RE: Replace Colour Codes by CookieRevised on 09-08-2006 at 04:51 PM

quote:
Originally posted by Shondoit
I never saw any of those features in the old IRC codes of Msg+ but please correct me if I'm wrong...
So? the request is to replace bbcode color tags with irc color tags. This means that ALL bbcode color tags should be replaced (supported in Plus!3 or not).
RE: Replace Colour Codes by Shondoit on 09-08-2006 at 04:57 PM

So? They are replaced now... to replace it for gradients, would mean alot of ·$n codes, wich will boost your text length with umm, at least 300% :O
Unpractical

If he wants it, I could have a look at it

(btw, I saw the text you now edited out, please tell what didn't work, for learning purpose)


RE: Replace Colour Codes by CookieRevised on 09-08-2006 at 05:07 PM

quote:
Originally posted by Shondoit
So? They are replaced now... to replace it for gradients, would mean alot of ·$n codes, wich will boost your text length with umm, at least 300% :O
Unpractical

If he wants it, I could have a look at it
That's not the point. I didn't say you need to simulate a gradient with irc codes (it would be cool though).

The request was to replace bbcode color codes, so they don't exist anymore in the string. This means if a gradient is done, that code should be replaced too (with whatever) and not be left in because "irc codes don't support gradients".

This is the same if you have a file saved in Word and you want to convert it to a textfile. You don't want to keep all the "not supporting" codes in it, they should be replaced too (with nothing; aka removed).

PS: as said, if hexadecimal color codes are used, Plus! will interpret the first 6 characters, but anything after that is ignored (as long as the string doesn't exceep 53 characters). So add ".{0,53}?" after each "[0-9A-Fa-f]{6}" (but they shouldn't be captured of course).

quote:
Originally posted by Shondoit
(btw, I saw the text you now edited out, please tell what didn't work, for learning purpose)
I removed it because a) I thought I was wrong in saying that, b) nobody replied yet.

but as a matter of fact, I wasn't wrong :D

1) "[1-6]?\d" should actually be "[0-9]{1,2}" or "\d{1,2}". Numbers from 0 to 99 are allowed.

2) the irc codes should contain 2 digits, not 1. 1 digit is possible to use, but may conflict with the actual string you try to color if it begins with a number. Same for backgrounds.


EDIT: blah... what SpunkyLoveMuff said below, while I was editing this :p

EDIT2: ".{0,53}" should be ".{0,53}?"

RE: Replace Colour Codes by Spunky on 09-08-2006 at 05:15 PM

Yeah, I just needed [c=?] replaced with ·$? and I changed all the [/c] tags to ·$1. It is very unlikely people would want to use gradients in this script, however the hex values are something I may have to factor in. The only problem I have had with this is that if the string (for example) is: "[c=2]5 Days til...[/c]" it gets replaced by "·$25 Days til...·$1" giving the wrong colour and also removing part of the string... I added a space after the replacement value and this fixes the problem, but adds spaces that make the PSM look slightly wierd.  I'm adding an option to turn this function on and off so that there is always an alternative method.

Also, someone said there were a particular amount of colours (I think they said 62), I counted 54... The only reason I used a loop before was because I wasn't using RegExp so i was looping through replacing "[c="+i+"]" with "·$"+i


RE: Replace Colour Codes by Eljay on 09-08-2006 at 05:20 PM

quote:
Originally posted by SpunkyLoveMuff
Also, someone said there were a particular amount of colours (I think they said 62), I counted 54

there are 67 colours.
RE: Replace Colour Codes by Spunky on 09-08-2006 at 05:23 PM

Using the Format Codes button in the Display Name changer section of MSN gives a total of 56 colours to use ranging from 0 to 55 and then it uses hex codes to fill in the gaps from what I can tell...


RE: Replace Colour Codes by CookieRevised on 09-08-2006 at 05:24 PM

quote:
Originally posted by SpunkyLoveMuff
It is very unlikely people would want to use gradients in this script
If the script doesn't support all kind of color tags, I frankly don't see why I should use this script tbh.

I though the purpose of such a script is to change bbcode color codes to irc color codes so contacts with Plus!3 can see the colors too (which I find an extreme good idea, don't get me wrong on that). This means the script should support all possebilities (ok, not right away, but it sure must be the goal in the end).

quote:
Originally posted by SpunkyLoveMuff
I'm adding an option to turn this function on and off so that there is always an alternative method.
instead of doing such a workaround it would be relativly easy to put a 0 in front of the singel digit numbers (as it should be anyways) by making the text used to replace as a function.

quote:
It is not so strait forward to make something like this (but no impossible; you just need to think about all the possebilities there are).


RE: Replace Colour Codes by Spunky on 09-08-2006 at 05:28 PM

quote:
Originally posted by CookieRevised
If the script doesn't support all kind of color tags, I frankly don't see why I should use this script tbh.

The script is not primarily for this purpose, but I know several people that are testing it for me that use colours using the script. The reason I want to use IRC codes are because they take less space and so allow for longer strings and older versions of plus will still be able to read them properly.

EDIT: Personally, I prefer BBCode as you can nest tags etc. I just thought it would be nice to add support for IRC

RE: Replace Colour Codes by Shondoit on 09-08-2006 at 10:40 PM

quote:
Originally posted by CookieRevised
The request was to replace bbcode color codes, so they don't exist anymore in the string. This means if a gradient is done, that code should be replaced too (with whatever) and not be left in because "irc codes don't support gradients".
I removed them already, didn't I?

quote:
Originally posted by CookieRevised
PS: as said, if hexadecimal color codes are used, Plus! will interpret the first 6 characters, but anything after that is ignored (as long as the string doesn't exceep 53 characters). So add ".{0,53}" after each "[0-9A-Fa-f]{6}" (but they shouldn't be captured of course).
Didn't know that, just thought it used standard colorcodes (a 6 char Hex val)

quote:
Originally posted by CookieRevised
1) "[1-6]?\d" should actually be "[0-9]{1,2}". Numbers from 0 to 99 are allowed.
The numbers are only relevant til a certain amount, after that, the standard is black...  But like you said, that doesn't mean it shouldn't be replaced

quote:
Originally posted by CookieRevised
2) the irc codes should contain 2 digits, not 1. 1 digit is possible to use, but may conflict with the actual string you try to color if it begins with a number. Same for backgrounds.
True, and I have no idea how to do this :S... unfortunately you can't check during the 'replace' call... But it should be possible, just have to take a closer look into it...

quote:
Originally posted by SpunkyLoveMuff
Yeah, I just needed [c=?] replaced with ·$? and I changed all the [/c] tags to ·$1.
Actually, you need to replace [/c] with ·$, because ·$1 will give you black, not the standard color (But that'll give problems with a number after it)
RE: Replace Colour Codes by Spunky on 09-08-2006 at 10:43 PM

quote:
Originally posted by Shondoit
Actually, you need to replace [/c] with ·$, because ·$1 will give you black, not the standard color (But that'll give problems with a number after it)

I thought black was the default colour... im sure i can make it use double digits for numbers less than 10 :p
RE: Replace Colour Codes by Shondoit on 09-08-2006 at 10:49 PM

I have purple as standard font color

I would really like to know how you'd do that (make it use double digits) if you're still gonna use RegExp's


RE: Replace Colour Codes by Spunky on 09-08-2006 at 10:51 PM

Use a seperate function to check for ·$ and then find the number, if it's less than 10 add a 0 in front of it... not gonna be easy I don't think but it should work (I think :S)


RE: Replace Colour Codes by Shondoit on 09-08-2006 at 10:53 PM

I think it shouldn't work...

How would you tell the difference between ·$1 and the text 5 dollar after it and ·$25 with the text dollar after it?

You have the same problem as you had before :S


RE: RE: Replace Colour Codes by CookieRevised on 09-09-2006 at 02:08 AM

quote:
Originally posted by SpunkyLoveMuff
quote:
Originally posted by Eljay
quote:
Originally posted by SpunkyLoveMuff
Also, someone said there were a particular amount of colours (I think they said 62), I counted 54
there are 67 colours.
Using the Format Codes button in the Display Name changer section of MSN gives a total of 56 colours to use ranging from 0 to 55 and then it uses hex codes to fill in the gaps from what I can tell...
again, it is actually not a matter of what is supported, it is a matter of what is allowed on the command line. And that is actually a number ranging from 0 to 99 (also for irc color codes).

(and there are 68 different colors defined by an index)

attached the whole list in RGB (hexadecimal)


quote:
Originally posted by Shondoit
quote:
Originally posted by CookieRevised
The request was to replace bbcode color codes, so they don't exist anymore in the string. This means if a gradient is done, that code should be replaced too (with whatever) and not be left in because "irc codes don't support gradients".
I removed them already, didn't I?
Yes, but now normal colors aren't replaced with your reg.exp.....

This is because you forgot a "?". And the non-capturing match (?:xxx) can be used, but isn't needed. You could capture the match/grouping, you only don't use it in the text to replace with. In fact if you use capturing matching/grouping, you could use this match to calculate your gradient in IRC style codes and replace match $2 (the text) with the proper gradient colors if you wish.


quote:
Originally posted by Shondoit
quote:
Originally posted by CookieRevised
2) the irc codes should contain 2 digits, not 1. 1 digit is possible to use, but may conflict with the actual string you try to color if it begins with a number. Same for backgrounds.
True, and I have no idea how to do this :S... unfortunately you can't check during the 'replace' call... But it should be possible, just have to take a closer look into it...
Yes you can check during a replacement. The 'replacetext' (thus the "·\$$$1$2·\$" in your reg.exp.) can be a function. That's the big power of the replace function:
quote:
Originally posted by CookieRevised
quote:
Originally posted by SpunkyLoveMuff
I'm adding an option to turn this function on and off so that there is always an alternative method.
instead of doing such a workaround it would be relativly easy to put a 0 in front of the singel digit numbers (as it should be anyways) by making the text used to replace as a function.



quote:
Originally posted by SpunkyLoveMuff
quote:
Originally posted by Shondoit
quote:
Originally posted by SpunkyLoveMuff
Yeah, I just needed [c=?] replaced with ·$? and I changed all the [/c] tags to ·$1.
Actually, you need to replace [/c] with ·$, because ·$1 will give you black, not the standard color (But that'll give problems with a number after it)
I thought black was the default colour...
The default color is whatever you set it to be in Messenger.

Also note that the actual format reset code is: "·0", not "·$". However, to only reset the colors to the default (and not the bold etc), you indeed must use "·$".


quote:
Originally posted by SpunkyLoveMuff
Use a seperate function to check for ·$ and then find the number, if it's less than 10 add a 0 in front of it... not gonna be easy I don't think but it should work (I think :S)
nope, see code below.






PS: to be very specific, what I said about those 53 characters after a hexadecimal color value is not entirly true. If there is a "]" in those 53 characters, the color tag is obviously ended, so that might comprimise coloring.

eg: [c=#FF00FF h]ello ]boo[/c]

the "h]ello" will not be entirly interpreted as being part of those 53 character long redundant text.

However!!! if the redundant text contains a complete set of color coding tags, they are ignored:

[c=#FF00FF  [c=3]hello[/c]  ]boo[/c]

here "[c=3]hello[/c]" will actually be ignored. This is because Plus! first parses the deepest nested color tags and then builds the string upwards. So while parsing "[c=3]hello[/c]", this is replaced with colors. Then [c=#FF00FF] is parsed, but since that colored "hello" string is now part of those redundant characters, it will be ignored.

To also accomplish this in our code too with need to use a positive lookahead match (?=xxxx) for those redundant characters. See code below.





The below code will:
1) replace color indexes (0 to 99)
2) replace hexadecimal color values
3) add a leading zero only if there is 1 digit as color index and the text between the color tags will conflict with the set color
4) ignore those 53 possible characters after a hexadecimal color value
5) adds a space after the reset code only if the text after the closing color tag will conflict with it.
6) can easly be adapted to include gradient converting

note that the below code is actually just 1 replace function!!

code:
var strMyNewPSM = "Hello[c=#123456blah]test[/c=3] [c=4]5555[/c] [c=4]red[/c]";

strMyNewPSM = strMyNewPSM.replace(
        /\[c=(\d{1,2}|#[0-9A-F]{6}(?=.{0,53}))\](.*?)\[\/c(?:=(\d{1,2}|#[0-9A-F]{6}(?=.{0,53})))?\]/gi,
        function (m0, m1, m2, m3, m4, m5) {
          // m0 = the complete substring that matched
          // m1 = capture 1: color value in the opening tag ([c=?])
          // m2 = capture 2: text between the color tags
          // m3 = capture 3: color value in the closing tag ([/c=?])
          // m4 = the offset where the match (m0) occured
          // m5 = entire string object

          var regConflict = /^,?(\d|#[0-9A-F]{6})/i;

          // add leading 0 if color value is only 1 digit and the text
          // between the color tags could conflict with the set color
          if (m1.length === 1 && regConflict.test(m2)) m1 = "0" + m1;
         
          var replacetext = "·$" + m1 + m2 + "·$"
 
          // check if the text that comes after the matching substring (m0)
          // wont conflict with the color reset code. if so, add a space
          // after the replacetext to avoid forcing a color value 
          if (regConflict.test(m5.substr(m0.length + m4))) replacetext += " ";

          // return the text to replace with
          return replacetext;
        }
      )

Debug.Trace(strMyNewPSM);
PS: remember that there is also another type of codes for colors. This code are the internal control characters used by Plus!. They were also used in plugins.

eg: to set a color you can use the ascii code 3. So this shortens the "·$" code to even only 1 character.

To adapt the above script with this, replace all "·$" with "\x03".

control ascii character for color ( ·$ ): 0x3
control ascii character for bold ( ·# ): 0x2
control ascii character for italic ( ·& ): 0x5
control ascii character for underlined ( ·@ ) : 0x1f
control ascii character for strikeout ( ·' ): 0x6
control ascii character for format reset ( ·0 ): 0xf