Posted By

haxednet on 01/22/14


Tagged

colors mIRC


Versions (?)

Parse MIRC Colors


 / Published in: jQuery
 

URL: http://haxed.net

Parse MIRC colors

  1. function ParseColors(message){
  2.  
  3. var oMessage = message; //keep the original
  4.  
  5. while(message.indexOf(String.fromCharCode(15))>-1){
  6. message = message.replace(String.fromCharCode(15),"");
  7. }
  8. while(message.indexOf(String.fromCharCode(3))>-1){
  9. message = message.replace(String.fromCharCode(3),"^C");
  10. }
  11.  
  12.  
  13. var colors = new Array("white","black","#00008B","green","red","brown","#800080","orange","yellow","#32CD32","#008080","#ADD8E6","#0000FF","#FF69B4","#808080","#D3D3D3");
  14. var match = message.match(/\^C[0-9]?[0-9](\,[0-9]?[0-9])?/g);
  15. var counter = 0; //keep track of how many html elements we make
  16. if(match!=null){
  17. $.each(match, function(index, value) {
  18. var orgValue = value; //for replacing later
  19. value = value.substr(2);
  20. value = value.split(",");
  21. value[0] = parseInt(value[0]); //we always have a value[0]
  22. if(value[0]<16){
  23. if(value.length>1){
  24. value[1] = parseInt(value[1]); //we sometimes have a value[1]
  25. //if it's over 1 then we have a for color and back color
  26. if(value[1]<16){
  27. counter++;
  28. message = message.replace(orgValue,"<span style=\"color:" + colors[value[0]] + ";background:" + colors[value[1]] + "\">");
  29. }else{
  30. //over 16... strip it
  31. message = message.replace(orgValue,"");
  32. }
  33. }else{
  34. //ok so it's just for color
  35. counter++;
  36. message = message.replace(orgValue,"<span style=\"color:" + colors[value[0]] + "\">");
  37. }
  38. }else{
  39. //ok it's a value over 16. but we only have 15 colors
  40. //lets just strip it
  41. message = message.replace(orgValue,"");
  42. }
  43. });
  44. }
  45.  
  46. //now lets close the tags
  47. while(message.indexOf("^C")>-1){
  48. if(counter>0){
  49. message = message.replace("^C","</span>");
  50. counter--;
  51. }else{
  52. message = message.replace("^C","");
  53. }
  54. }
  55.  
  56. if(counter>0){
  57. //ok we closed less tags than we've created... lets close the rest
  58. while(counter>0){
  59. counter--;
  60. message += "</span>";
  61. }
  62. }
  63.  
  64. //following code makes sure all </span>'s precede <span>
  65. var c = message.split("<");
  66. var counter = 0;
  67. $.each(c, function(index, value) {
  68. if(value.length>5 && value.substr(0,5)=="span "){
  69. counter++;
  70. }
  71. if(value.length>5 && value.substr(0,6)=="/span>"){
  72. counter--;
  73. if(counter<0){
  74. //ok the counter is off...
  75. //lets just set it to one and
  76. //exit the loop, so it will return plain text back
  77. counter = 1;
  78. return false;
  79. }
  80. }
  81. });
  82.  
  83. if(counter!=0){
  84. //counter is off... something isn't proper...
  85. //lets just remove all colors and return that.
  86. message = StripColors(oMessage);
  87. }
  88.  
  89. //now for bold and underline
  90. message = replaceWithHtml(message,"\002","<b>","</b>");
  91. message = replaceWithHtml(message,"\037","<u>","</u>");
  92.  
  93. return message;
  94. }

Report this snippet  

You need to login to post a comment.