This is what I came up with:
Code:
alias bg2own {
  var %b = $color(background), %i = %b + 16, %a 
  while %i < 99 {
    %a = $addtok(%a,%i,124)
    inc %i 16
  }
  %a = $iif(%b < 10,(?:0 $+ $+(%b,|,%b,(?!\d)) $+ )| $+ %a,$+(%b,|,%a))
  !.echo -q $regsub($1,/(?<=)(?: $+ %a $+ )(?:(?!\x2C\d)|(?=\x2C(?: $+ %a $+ )))/g,$color(own).dd,%a)
  return %a
}
The loop is there so that the alias catches the background colour in the form
$color(background) + N*16
for N >= 0 (ie catch the cycling of colours). The performance wouldn't be affected in the majority of cases, since very few people us 'cycling' colours, I guess.

However, there is one little problem, which is hard to overcome with regular expressions. Consider this case: my $color(background) is white and $color(own) is black. In the following string
0,0one 4two 0,3three 00four
the alias will correctly replace the first "0" with "01", but will also replace the "00" (right before "four") with "01". This isn't necessary though, because the previous colour code has specified background "3" (green), and the green is preserver after the 4th colour code. Unfortunately, the regex cannot "look behind" in this way and see the previous bg colour.

I could try some tricks, similar to the ones applied in the $ddc alias, although I'm afraid it would become pretty ugly. I'll let you know if I find an acceptable way to solve this.

Of course, you could do it in pure scripting, parsing the entire string and storing stuff in vars etc, but I guess this is out of the question (it would get pretty slow).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com