The online racing simulator
LFS Coloured String - JS and PHP [EDIT]
Hi guys:

Just sharing this here. It's a port of a LFS HTML colour formatter I found on the forums by Dygear written in PHP. To change the specific shade of colour used, replaceArray is the place to do it.

Hope someone finds this helpful.

(See below, better one below).
The original version was written by Victor in PHP as well, but in two different functions, I just placed them into a single function call. Also nice work.
Me and Vic today went back and forth regarding this, talking a lot about the colour stuff (and how it's kinda crazy ). Currently, my above method has a bug (^^ won't escape correctly) as does the PHP one I ported this from.

It'll be updated probably tonight.
It's kinda like a smaller version of the ECMA-48. It's kinda crazy, but in the grand scheme of things, it's a lot better implemented then some of the alternatives.

Also, I did not know about the bug. If you could backport / upstream the fix that would be awesome.
I'll wait til Vic appears tomorrow to see if he wants to post it. I'm trying to benchmark it but for all intents and purposes it's much faster. Even my JS one is faster by using the "new" code that Vic and I worked on today. It's about twice as fast approximately.

It should be an improvement over what's currently out there.
Quote from dawesdust_12 :It should be an improvement over what's currently out there.

DO WAHT PLZ.



bumfarts

Seriously though, I'd love to add it to xi4n's string library (if you guys are happy with that) and replace the current half arsed implementation I've got
Ok, here's my JS version. It relys on a bit of an extension to regex (but this extension could be function-ized as well.. I just prototyped it because I do like this functionality everywhere but I understand others may not).

Of note, For speed and simplicity, this pre-replaces ^^ with the HTML Entity for a caret. Because this is HTML formatting the LFS String, it's a safe assumption that it's OK behaviour. If you don't want this, It's simple enough to replace it back to a caret after the fact. The reason it's pre-replaced is for speed. Doing this allows a much simpler regex to be written.

The PHP version is basically 1:1 of this, I'll post it in another post in a few moments.

(Stupid, the forum encodes the HTML entity. I'll leave that one as an exercise.


String.prototype.LFSColouredString = function(){
var colourArray = ['000','F00','0F0','FF0','00F','F0F','0FF','FFF'];
var found = false;
var str = this.replace(/\^\^/g, '<caret HTML entity here>'');
var replaced = /\^([0-9])/.ReplaceCallback(str,function(matches){
var ret;
if(matches[1] < 8){
ret = (found ? '</span>' : '');
ret += '<span style="color:#'+colourArray[matches[1]]+';">';
}
else{
ret = (found ? '</span>' : '');
found = false;
}
return ret;
});
return replaced[0];
}

RegExp.prototype.ReplaceCallback = function (_string,_callback){
var matches = [];
var newString = '';
if(Object.prototype.toString.call(_string) == '[object String]' && Object.prototype.toString.call(_callback) == '[object Function]'){
this.global = false;
while(_string && this.exec(_string)){
var x = this.exec(_string);
if(x[0] != ''){
var tArray = [];
for(var i=0;i<x.length;i++){
tArray.push(x[i]);
}
matches.push(tArray);
}
newString = newString+_string.substr(0,x['index'])+_callback(tArray);
_string = _string.substr(x['index']+x[0].length);
}
if(_string){
newString = newString+_string;
}
}
return [newString,matches];
};

PHP Version:

This one assumes that ^^ has been replaced outside of it (probably before Character encoding handling, but that's a different library all together). Also uses PHP 5.3 anonymous functions, so that's a minimum version. If you need further backwards compatibility, You can use a named function. If it's used in a class (recommended), then $found needs to be defined on the class (as private static), and changed in the function to use self::$found instead of just $found in all occurances.
Use the class Vic has posted below here, includes this code along with a full implementation of Charset handling.

<?php 
private static $colorCodes = array("000","F00","0F0",'FF0','00F','F0F','0FF','FFF');
    public static function 
writeColor($s){
        
$found false;
        
$replaced preg_replace_callback(
            
'#\^([0-9])#',
            function (array 
$matches) use (&$found)
            {
                if (
$matches[1] < 8)
                {
                        
$return = (($found) ? '</span>' '').'<span style="color:#'.self::$colorCodes[$matches[1]].';">';
                        
$found true;
                }
                else
                {
                        
$return = ($found) ? '</span>' '';
                        
$found false;
                }
                return 
$return;
            },
            
$s);
        return (
$found) ? $replaced.'</span>' $replaced;
    }
    
?>


FGED GREDG RDFGDR GSFDG