]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/colour.js
Fix issue #230 (colours not matching mIRC)
[irc/quakenet/qwebirc.git] / js / ui / colour.js
CommitLineData
2cd9e32d 1qwebirc.ui.Colourise = function(line, entity, execfn, cmdfn, window) {
9e769c12
CP
2 var fg;
3 var bg;
4 var underline = false;
5 var bold = false;
f121b688
CP
6 var autoNickColour = false;
7
9e769c12
CP
8 var out = [];
9 var xline = line.split("");
10 var element = document.createElement("span");
11
12 entity.addClass("colourline");
13
14 function isNum(x) {
15 return x >= '0' && x <= '9';
16 }
17
18 function parseColours(xline, i) {
19 if(!isNum(xline[i + 1])) {
20 fg = undefined;
21 bg = undefined;
22 return i;
23 }
24 i++;
25 if(isNum(xline[i + 1])) {
26 fg = parseInt(xline[i] + xline[i + 1]);
27 i++;
28 } else {
29 fg = parseInt(xline[i]);
30 }
31 if(xline[i + 1] != ",")
32 return i;
33 if(!isNum(xline[i + 2]))
34 return i;
35 i+=2;
36
37 if(isNum(xline[i + 1])) {
38 bg = parseInt(xline[i] + xline[i + 1]);
39 i++;
40 } else {
41 bg = parseInt(xline[i]);
42 }
43 return i;
44 }
45
46 function emitEndToken() {
f121b688 47 var data = "";
9e769c12 48 if(out.length > 0) {
2326e8d5
CP
49 var o = out.join("");
50 if (execfn) {
51 qwebirc.ui.urlificate(element, o, execfn, cmdfn, window);
52 } else {
53 element.appendChild(document.createTextNode(o));
54 }
9e769c12
CP
55 entity.appendChild(element);
56 out = [];
57 }
58 element = document.createElement("span");
f121b688 59 return data;
9e769c12 60 }
f121b688 61
9e769c12 62 function emitStartToken() {
f121b688
CP
63 if(autoNickColour)
64 return element;
65
2a802692 66 var classes = []
9e769c12
CP
67 if(fg != undefined)
68 classes.push("Xc" + fg);
69 if(bg != undefined)
70 classes.push("Xbc" + bg);
71 if(bold)
72 classes.push("Xb");
73 if(underline)
74 classes.push("Xu");
75 element.className = classes.join(" ");
76 }
77
f121b688
CP
78 var nickColouring = window.parentObject.uiOptions.NICK_COLOURS; /* HACK */
79 var capturingNick = false;
29453513 80 for(var i=0;i<xline.length;i++) {
9e769c12 81 var lc = xline[i];
f121b688
CP
82
83 if(nickColouring) {
84 if(!capturingNick) {
85 if(lc == "\x00") {
86 capturingNick = true;
87 emitEndToken();
88 continue;
89 }
90 } else {
91 if(lc != "\x00") {
92 out.push(lc);
93 } else {
94 autoNickColour = true;
95 var e = emitStartToken();
96 var text = emitEndToken();
97
98 var c = text.toHSBColour(window.client);
99 if($defined(c))
100 e.style.color = c.rgbToHex();
101 capturingNick = autoNickColour = false;
102 }
103 continue;
104 }
105 } else if(lc == "\x00") {
106 continue;
107 }
108
9e769c12
CP
109 if(lc == "\x02") {
110 emitEndToken();
111
112 bold = !bold;
113
114 emitStartToken();
115 } else if(lc == "\x1F") {
116 emitEndToken();
117
118 underline = !underline;
119
120 emitStartToken();
121 } else if(lc == "\x0F") {
122 emitEndToken();
123
124 fg = undefined;
125 bg = undefined;
126 underline = false;
127 bold = false;
128 } else if(lc == "\x03") {
129 emitEndToken();
130
131 i = parseColours(xline, i);
132 if(bg > 15)
133 bg = undefined;
134 if(fg > 15)
135 fg = undefined;
136
137 emitStartToken();
138 } else {
139 out.push(lc);
140 }
141 }
142
143 emitEndToken();
e20e5a6b 144}
f121b688
CP
145
146String.prototype.toHSBColour = function(client) {
147 var lower = client.toIRCLower(client.stripPrefix(this));
148 if(lower == client.lowerNickname)
149 return null;
150
151 var hash = 0;
152 for(var i=0;i<lower.length;i++)
153 hash = 31 * hash + lower.charCodeAt(i);
154
155 var hue = Math.abs(hash) % 360;
156
157 return new Color([hue, 70, 60], "hsb");
158}