wiki:Development/Styles/NickColoring

TracNav(Development)? This is part of the Development Documentation Project, please help document Colloquy if you know anything that would be relevant here for other users to know about.

Using Nick Coloring in Styles

The first Colloquy style to use this technique, which assigns a different color to messages from each nick in a chat room, was "DecafBland", by Les Orchard. Others styles such as Frugal and Lancia use this technique. The technique is fairly straightforward; the trick lies in mapping each nick to a reasonably unique style name. The Frugal style maps hostmasks, since they are usually more unique and give better results.

The mapping from nick to style is most easily done with a hash function. Les Orchard came up with the following (this should appear in the style's XSL file):

<xsl:variable name="senderNick" select="sender | ../sender"/>
<xsl:variable name="senderHash"
	select="number(translate($senderNick,
		'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-_^[]{}|',
		'123456789012345678901234567890123456789012345678901234567890'
	))"
/>

<xsl:variable name="senderColor">
	<xsl:choose>
	<xsl:when test="sender/@self = 'yes'">colorself</xsl:when>
	<xsl:when test="../sender/@self = 'yes'">colorself</xsl:when>
	<xsl:when test="string-length(sender/text()) &gt; 0">
	<xsl:value-of select="concat('color', $senderHash mod 15)" />
	</xsl:when>
	</xsl:choose>
</xsl:variable>

(Note: Here I have made slight formatting changes and omitted a few irrelevant bits of code from DecafBland?'s XSL file.)

"senderHash" is created by mapping each letter in the nick to a number. Then, the resulting number is clamped down to a fixed range (in this case, from 0 to 14), and the string "color" is slapped in front of it to yield something like "color11". Later, when the HTML is generated, this string is used as a class, like in the following:

<span id="{@id}" class="{$senderClasses} {$senderColor}">

By styling this class in the CSS file, you can create all sorts of interesting effects on more or less a per-nick basis. Note that since we only generate 15 color classes, it's very likely that more than one nick will map to a single color class. There are two ways to reduce the likelihood of this happing: one is to increase the modulus used to clamp the hash, the other is to use a more sophisticated hashing function. Of course, combining these two approaches is also possible.

Last modified 11 years ago Last modified on Dec 31, 2006 12:37:51 PM