Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Plugins ➜ Sending coloured text to the world window with ease

Sending coloured text to the world window with ease

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Wed 23 Oct 2002 03:06 AM (UTC)

Amended on Wed 23 Oct 2002 05:03 AM (UTC) by Nick Gammon

Message
I have been asked about this many times, so here at last is a plugin that will simplify sending text to the world window (like a world.note) with imbedded colour codes.

The general technique is to break up the supplied text at the tilde character (~) and look for a colour code after that character. eg.


~R bright red ~G bright green
~r dull red ~g dull green
~rG dull red on bright green ~YM yellow on bright magenta



You can specify the foreground and background colour (first letter, second letter). If either is a period (.) they are unchanged, so you can change the foreground, background or both.

The exact codes are in the subroutine below. Generally they match the first letter of the colour (excepting K for black), where the upper-case colour is the bright version (Y = bright yellow) and the lower-case colour is the dull version (y = dull yellow).


  • K = bright black (gray), k = black
  • R = bright red, r = dark red
  • G = bright green, g = dark green
  • Y = bright yellow, y = dark yellow
  • B = bright blue, b = dark blue
  • M = bright magenta, m = magenta
  • C = bright cyan, c = dark cyan
  • W = bright white, w = dark white (silver)


If you want you could edit it to add other codes of your own.

There is also a "save" (#) and "restore" (^) code to remember earlier colours (note, these do not nest).

You can copy from below the line below, paste into a notepad window, and save as ColourNote.xml, or just download it from:


http://www.mushclient.com/plugins/ColourNote.xml




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!-- Saved on Wednesday, October 23, 2002, 12:48 PM -->
<!-- MuClient version 3.26 -->

<!-- Plugin "ColourNote" generated by Plugin Wizard -->

<!--
You can edit the lookup_colour function to add more colour codes.
-->

<muclient>
<plugin
   name="ColourNote"
   author="Nick Gammon"
   id="8f86e2da6eea3806f1836050"
   language="JScript"
   purpose="Lets you send a string with colour codes to the world window"
   date_written="2002-10-23 12:46:12"
   requires="3.23"
   version="1.0"
   >
<description trim="y">
<![CDATA[
This is designed to be called from a script like this:

world.CallPlugin "8f86e2da6eea3806f1836050", _
                 "colournote", _
                 "~R red ~G green ~B blue ~RG red-on-green"

To simplify things, make a "stub" routine in your main script file, like this:

// example in Jscript:
function ColourNote (text)
  {
  world.CallPlugin ("8f86e2da6eea3806f1836050", "colournote", text);
  }

' example in VBscript:
sub  ColourNote (text)
  world.CallPlugin "8f86e2da6eea3806f1836050", "colournote", text
end sub

Then you can just do colour text like this:

  ColourNote ("~R This is red ~G This is green ~B This is blue");

 
ColourNote:help  - this help
]]>
</description>

</plugin>


<!--  Script  -->


<script>
<![CDATA[
function lookup_colour (which)
  {
  var c;
 
// add more codes below if you want to process different letters
// Reserved characters are: space, and: . # ^ ~
  
  switch (which)
    {
    case "K": c = "gray"; break;
    case "k": c = "black"; break;

    case "R": c = "red"; break;
    case "r": c = "maroon"; break;

    case "G": c = "lime"; break;
    case "g": c = "green"; break;

    case "Y": c = "yellow"; break;
    case "y": c = "olive"; break;

    case "B": c = "blue"; break;
    case "b": c = "navy"; break;

    case "M": c = "magenta"; break;
    case "m": c = "purple"; break;

    case "C": c = "aqua"; break;
    case "c": c = "teal"; break;

    case "W": c = "white"; break;
    case "w": c = "silver"; break;

    default:  c = ""; break;  // unknown ones are no change
    }  // end of switch on which colour code

  return c;
  }  // end of lookup_colour

/*
 This sends a coloured message to the world window.
  
 Colour codes are like this: ~fb

 ~ = colour code delimiter
 f = foreground colour (eg. R = bright red, g = dull green)
 b = background colour (same meanings)

 If the background colour is a space, then it is assumed to be
 unchanged. eg. ~R red ~G green ~B blue

 Otherwise, it is processed as the background colour, 
  eg.  ~RB red-on-blue ~.G red-on-green ~yc dark-yellow on dark-cyan

 Special colours: 

  . = no change		 (eg. to change background only)
  # = save colours   (save current foreground/background colour)
  ^ = restore colours  (restore to saved colour)

 eg.

 colournote ("This will be a ~R red ~G green ~B blue.");

*/

function colournote (msg) 
  {
  var msg_array,   // split up line
      stext,       // what we are sending
      fcolour,     // foreground colour code
      bcolour,     // background colour code
      foreground,  // converted foreground colour
      background,  // converted background colour
      save_foreground,  // saved foreground colour
      save_background   // saved background colour

// save colours for later restore

  save_foreground = world.RGBColourToName (world.NoteColourFore ());
  save_background = world.RGBColourToName (world.NoteColourBack ());
  foreground = save_foreground;
  background = save_background;

// convert into an array

  msg_array = msg.split ("~");

// first element in array (before tilde) is just shown as is

 world.Tell (msg_array [0]);

 for (i = 1; i < msg_array.length; i++)
   {
   fcolour = msg_array [i].substring (0, 1);  // foreground colour
   bcolour = msg_array [i].substring (1, 2);  // background colour
   stext   = msg_array [i].substring (2);     // rest is message

   // get foreground colour, handle special cases, lookup others
   switch (fcolour)
     {
     case ".":  break;  // colour unchanged
     case "#":  save_foreground = foreground; break;
     case "^":  foreground = save_foreground; break;
     default: foreground = lookup_colour (fcolour); break;
     }  // end of switch on fcolour

   // get background colour, handle special cases, lookup others
   switch (bcolour)
     {
     case " ": 
     case ".":  break;  // colour unchanged
     case "#":  save_background = background; break;
     case "^":  background = save_background; break;
     default: background = lookup_colour (bcolour); break;
     }  // end of switch on fcolour

//
//  now display the text in the appropriate colour
//
   world.ColourTell (foreground, background, stext);

   }  // end of loop on each colour change

  world.Note ("");  // finish off line

}  // end of colournote

]]>
</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="ColourNote:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp (sName, sLine, wildcards)
  {
  world.Note (world.GetPluginInfo (world.GetPluginID, 3));
  }
]]>
</script> 

</muclient>


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 23 Oct 2002 03:08 AM (UTC)

Amended on Wed 23 Oct 2002 04:51 AM (UTC) by Nick Gammon

Message
I suggest making a "stub" routine in your main script file (or other plugins) to make calling the above plugin easier. An example in VBscript would be:


sub  ColourNote(text)
  world.CallPlugin "8f86e2da6eea3806f1836050", "colournote", text
end sub




Example of use:


ColourNote "~R red ~G green ~B blue ~RG red-on-green"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Faolong   USA  (28 posts)  Bio
Date Reply #2 on Thu 24 Oct 2002 12:32 AM (UTC)
Message
this seems way less complicated than mine... and i think it's way faster... good job.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,929 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.