Project

General

Profile

Full-Pallette Random Nick Color Script fork v0.1.3.ini

Mr. BS, 05/06/2016 05:57 AM

 
1
; https://dev.adiirc.com/boards/5/topics/308
2

    
3
; Hackjob full-palette Random Nick Color script for AdiIRC
4
; Generate consistent but random, full-palette colors for nicks & set custom colors as you please
5
;
6
; Version 1.2 - Fork v0.1.3 by Pereba
7
;
8
;
9
; *Now allows storage of custom colors!
10
;		Usage: /setCustomColor <Nick> <Hex Color Code> - Example: /setCustomColor JohnDoe F854A7
11
;				/delCustomColor <Nick> - Example: /delCustomColor JohnDoe
12
;
13
; *Colors are now calculated once and stored as needed for both random and custom colors
14
;	This decreases CPU time (especially on bouncer playback) for each line displayed
15
; 	Custom colors persist between sessions (stored as rcolorscustom in your AdiIRC folder)
16
;
17
; *Buffer playback from bouncers is now properly supported without mangling output
18
;
19
; **NOTE: The built-in "Use Random Nick Colors" (Options -> Nick Colors) must be disabled for this script to work 
20
; **NOTE: Any nick colors set in the AdiIRC options menu override random or custom colors set by this script
21
;
22
; Thanks to pereba for the original inspiration script and kr0n for troubleshooting help, as well as 
23
; introducing me to hashtables :)
24
;
25
; For any of the output strings, if you don't want to show usermodes, you can replace $nick($chan, $nick).pnick
26
;  with just plain $nick
27
;
28
; If you're curious, $chr(61442) is a special AdiIRC character that interprets the following string as a hex 
29
; color code and allows for colorizing text
30
;
31
; If the script is acting weird for you, consider updating to the latest release or beta of AdiIRC; 
32
; some of this code relies on features or bufixes in later versions
33
;
34
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35
;
36
; This is the initialization section, it handles actions to take when the script is loaded for 
37
; the first time or reloaded, 
38
;
39
;
40
; whenever the script is loaded for the first time or reloaded, call /savereload, 
41
; which saves the rcolors and rcolorscustom hashtables to disk, clears them from memory, 
42
; then reloads them--this also handles reloading them from disk when AdiIRC first starts
43
;
44
on *:LOAD:savereload
45
on *:START:{
46
  hadd -m rcolors 0bgcolor $color(34)
47
  savereload
48
}
49
;
50
; save & overwrite the rcolors & rcolorscustom hashtables to disk on exit
51
;
52
on *:Exit:{
53
hsave -o rcolors rcolors
54
hsave -o rcolorscustom rcolorscustom
55
}
56
; dump all cached colors when the options screen is loaded; this ensures no stale colors are used
57
; if the background color is changed, since they might not be easy enough to see
58
on *:OPTIONS:{
59
  if ($hget(rcolors,0bgcolor) != $color(34)) {
60
    reset
61
  }
62
  if ($readini($adiircini,n,NickColors,NickColorsEntireLine) == True) {
63
    set -e %rcolors_ColorEntireLine True
64
  }
65
  else {
66
    unset %rcolors_ColorEntireLine
67
  }
68
}
69

    
70
;
71
; This section responds to normal in-channel text events
72
;
73
on &^*:TEXT:*:#:{
74
;
75
; Stop here if user turned off script
76
;
77
if (%rcolors_disabled) return
78
;
79
; Disable script on blacklisted networks/channels
80
;
81
if ($network $+ . $+ # isin %rcolors_blacklist) return
82
;
83
; Check if message is ZNC buffer playback and use values accordingly
84
; ZNC buffer playback has different message content than a normal message and needs different handling
85
;
86
  var %time $spacefix($asctime($iif($msgstamp, $v1, $ctime), $timestampfmt))
87
  var %pnick $iif($nick(#,$nick).cnick, $v1, $nick)
88
  ;;$nick($chan, $nick).pnick
89
; build the echo string
90
  ;;;echo -vrlmbfw2 # %time $replace($replace($prefixuser, $chr(32), $chr(160)), $ $+ pnick, $+($chr(61442), $getcolor, %pnick, $chr(3)))  $1-
91
  
92
  var %linecolor $chr(3) $+ $chr(61442) $+ $getcolor
93
  var %prefixuser $replace($prefixuser,$!pnick,%pnick,$chr(32),$chr(160))
94
  var %msg $msgx
95

    
96
  ; Color entire prefixuser or just the nick? follow user preference:
97
  if (%rcolors_ColorEntirePrefixUser) {
98
    var %prefixuser %linecolor $+ $replace($prefixuser,$!pnick,%pnick,$chr(32),$chr(160)) $+ $chr(3)
99
  }
100
  else {
101
    var %prefixuser $replace($prefixuser,$!pnick,%linecolor $+ %pnick $+ $chr(3),$!nick,%linecolor $+ %pnick $+ $chr(3),$chr(32),$chr(160))
102
  }
103
  
104
  ; If Options -> Nick Colors -> Color entire line is enabled:
105
  if (%rcolors_ColorEntireLine) {
106
    %prefixuser = %prefixuser $+ %linecolor
107
    %msg = $regsubex(%msg,$patLine,%linecolor)
108
    if (%rcolors_ColorTimestamp) { %time = %linecolor $+ $regsubex(%time,$patLine,%linecolor) $+ $chr(3) }
109
  }
110
  
111
  %msg = %time %prefixuser %msg
112
  echox -vrlmbfw2 # %msg
113
  halt
114
}
115
;
116
; This section handles ACTIONs, like /me or /emote commands
117
; Mostly the same as PRIVMSG but with the output string adjusted to properly display these types of messages
118
;
119
on &^*:ACTION:*:#:{
120
  ;
121
  ; Stop here if user turned off script
122
  ;
123
  if (%rcolors_disabled) return
124
  var %time $spacefix($asctime($iif($msgstamp, $v1, $ctime), $timestampfmt))
125
  var %pnick $iif($nick(#,$nick).cnick, $v1, $nick)
126
  var %msg %time $replace($replace($prefixemote, $chr(32), $chr(160)), $ $+ pnick, $+($chr(61442), $getcolor, %pnick, $chr(3))) $msgx
127
;build the echo string  
128
  echox -vrlmbfw2c action # %msg
129
  halt 
130
}
131

    
132
;
133
; User own text
134
;
135
on *:INPUT:#: {
136
  ;
137
  ; Stop here if user turned off script
138
  ;
139
  if (%rcolors_disabled) return
140
  
141
  if (($left($1,1) != /) || ($ctrlenter) || ($regex($1,/^(\/{1,4})(me|action)/i)) && ($status == connected) {
142
    var %time $spacefix($asctime($iif($msgstamp, $v1, $ctime), $timestampfmt))
143
    var %pnick $iif($nick(#,$nick).cnick, $v1, $nick)
144
    var %linecolor $chr(3) $+ $chr(61442) $+ $getcolor   
145
    var %msgxraw $replacex($iif($regml(1), $iif($len($regml(1)) > 1,$($mid($msgx,$calc($len($1) +2)),2),$mid($msgx,$calc($len($1) +2))), $msgx),$chr(9),$chr(160))
146
    var %msg %msgxraw
147
    
148
    ; Color entire prefixuser or just the nick? follow user preference:
149
    if (%rcolors_ColorEntirePrefixUser) {
150
      var %prefixuser %linecolor $+ $replace($iif($regml(2),$prefixemote,$prefixuser),$!pnick,%pnick,$chr(32),$chr(160)) $+ $chr(3)
151
    }
152
    else {
153
      var %prefixuser $replace($iif($regml(2),$prefixemote,$prefixuser),$!pnick,%linecolor $+ %pnick $+ $chr(3),$!nick,%linecolor $+ %pnick $+ $chr(3),$chr(32),$chr(160))
154
    }
155

    
156
    ; If Options -> Nick Colors -> Color entire line is enabled: (except /action msg)
157
    if (%rcolors_ColorEntireLine && !$regml(2)) {
158
      %prefixuser = %prefixuser $+ %linecolor
159
      %msg = $regsubex(new,%msgxraw,$patLine,%linecolor)
160
      if (%rcolors_ColorTimestamp) { %time = %linecolor $+ $regsubex(time,%time,$patLine,%linecolor) $+ $chr(3) }
161
    }
162

    
163
    ; Action VS regular message
164
    if ($regml(2)) {
165
      var %msgx %time %prefixuser $+ $chr(3) %msgxraw, %text PRIVMSG $active : $+ $chr(1) $+ ACTION %msgxraw $+ $chr(1)
166
      echox -vw2c action # %msgx
167
    }
168
    else {
169
      var %msgx %time %prefixuser %msg, %text PRIVMSG $active : $+ %msgxraw
170
      echox -vw2c own # %msgx
171
    }
172
    .rawx %text
173
    halt
174
  }
175
}
176

    
177
;
178
; Coloring nicklist
179
;
180
on me:*:join:#:{
181
  ; If enabled continue
182
  if (%rcolors_nicklist) {
183
    .timer 1 3 clinergb #
184
  }
185
}
186

    
187
on *:JOIN:#:{
188
  ; If enabled continue
189
  if (%rcolors_nicklist) {
190
    cline -b $rgb( [ $hex2rgb($iif(%rcolors_nicklistbg == 1, $getDarkColor($nick), $getBrightColor($nick))) ] ) $nick
191
  }
192
}
193
; credits @ http://forums.mirc.com/ubbthreads.php/topics/153480/HEX_=%3E_RGB?
194
alias hex2rgb {
195
  $iif($isid,return,echo -a) $+($base($left($1,2),16,10),$chr(44),$base($mid($1,3,2),16,10),$chr(44),$base($right($1,2),16,10))
196
}
197

    
198
alias -l clinergb {
199
  if ($istok(%rcolors_blacklist,$network $+ . $+ $1,32)) {
200
    var %rcolors_disabled 1
201
  }
202
  var %n $nick($1,0)
203
  while (%n) {
204
    cline $iif(%rcolors_nicklist && %rcolors_disabled != 1,-b $rgb( [ $hex2rgb($iif(%rcolors_nicklistbg == 1, $getDarkColor($nick($1,%n)), $getBrightColor($nick($1,%n)))) ] ), -r) $1 $nick($1,%n)
205
    dec %n
206
  }
207
}
208

    
209
alias -l resetNicklist {
210
  tokenize 32 $regsubex($str(x,$chan(0)),/x/g,\n $chr(32))
211
  clinergb $chan($*)
212
}
213

    
214
; credits Kin @ GeekShed
215
alias -l patLine return /\co|(\cc(?!\d(?:\d(?:,\d\d?)?)?))/gu
216

    
217
alias -l spacefix {
218
  return $replacex($1,$chr(32),$chr(160))
219
}
220

    
221
alias -l getcolor {
222
; Initialize %color as neutral grey in case we end up with nothing else to return
223
  var %color = 888888
224
; Check if nick has a custom color set in AdiIRC options; if it does, return that color
225
  if ($nick(#, $nick).color > 1) {
226
    var %color = $getHexOfColor($nick(#, $nick).color))
227
    return %color
228
}
229
; Check if nick has a script-custom color; if it does, return that color
230
  else if ($hget(rcolorscustom, $nick)) {
231
    var %color = $hget(rcolorscustom, $nick)
232
    return %color
233
    }
234
; Check if nick has a cached color, if it does, return that color
235
  else if ($hget(rcolors, $nick)) {
236
    var %color = $hget(rcolors, $nick)
237
    return %color
238
  }
239
; If no cached or custom colors are found, roll for a new color and add it to the cache
240
  else {
241
    var %color $iif(%rcolors_bg == 1, $getDarkColor, $getBrightColor)
242
    hadd -m rcolors $nick %color
243
    return %color
244
  }
245
; If something fails, return %color as originally set to 888888
246
  return %color
247
}
248

    
249
;
250
;
251
;
252
; This is the alias for generating bright colors
253
;
254
alias -l getBrightColor {
255
;
256
; Generate a unique seed number by calculating the MD5 hash of a nick, interpreting the last 8 characters as a hex number, then converting that to decimal
257
;
258
  ;;;var %nickSeedNum = $base($right($md5($nick),8),16,10)
259
  var %nickSeedNum = $base($right($md5($iif($1,$1,$nick)),8),16,10)
260
;
261
; Multiply by a high prime number, then take the modulus of a prime number < 255 to get 3 values for R,G,B color; this induces a bit more variety in the colors, and
262
; for whatever reason (I'm not a mathematician) tends to "favor" returning numbers close to 255 (brighter colors)
263
;
264
  var %dec1 = $calc((%nickSeedNum * 7793) % 251)
265
  var %dec2 = $calc((%nickSeedNum * 5153)  % 241)
266
  var %dec3 = $calc((%nickSeedNum * 3623) % 239)
267
;
268
; Take the average of the RGB values 
269
;
270
  var %decaverage = $calc((%dec1 + %dec2 + %dec3)/3)
271
;
272
; Generate colors until you get one that is "bright" (average RGB value > 128)
273
; If you find these colors are too dark or bright, you can adjust the %decaverage <= 128
274
;
275
  while (%decaverage <= 128) {
276
    var %dec1 = $calc((%dec1 * 7793) % 251)
277
    var %dec2 = $calc((%dec2 * 5153) % 241)
278
    var %dec3 = $calc((%dec3 * 3623) % 239)
279
    var %decaverage = $calc((%dec1 + %dec2 + %dec3)/3)
280
  }
281
;
282
; call $RGBToHex to turn the RGB Value into a hex color code and return that code
283
;
284
  var %brightColorReturn = $RGBToHex(%dec1,%dec2,%dec3)
285
  return %brightColorReturn
286
}
287
;
288
; This is the alias for generating dark colors
289
; It's the same as $getBrightColor but looks for colors that are "dark", or have
290
; average RGB value < 128 - Again you can tweak the %decaverage <= 128 if they're too dark or bright
291
;
292
alias -l getDarkColor {
293
  ;var %nickSeedNum = $base($right($md5($nick),8),16,10)
294
  var %nickSeedNum = $base($right($md5($iif($1,$1,$nick)),8),16,10)
295
  var %dec1 = $calc((%nickSeedNum * 7793) % 113)
296
  var %dec2 = $calc((%nickSeedNum * 5153) % 127)
297
  var %dec3 = $calc((%nickSeedNum * 3623) % 131)
298
  var %decaverage = $calc((%dec1 + %dec2 + %dec3)/3)
299

    
300
  while (%decaverage >= 128) {
301
    var %dec1 = $calc((%dec1 * 7793) % 113)
302
    var %dec2 = $calc((%dec2 * 5153) % 127)
303
    var %dec3 = $calc((%dec3 * 3623) % 131)
304
    var %decaverage = $calc((%dec1 + %dec2 + %dec3)/3)
305
  }
306
  var %darkColorReturn = $RGBToHex(%dec1,%dec2,%dec3)
307
  return %darkColorReturn
308
}
309
;
310
; This is the alias for converting RGB values to Hex, used here for getting hex strings to put 
311
; after $chr(61442) - see top if you're not sure what this character is
312
;
313
; it takes in 3 numbers from 0-255, converts them to hex numbers, 
314
; then returns them as a single string, EG $RGBToHex(255,255,255) or "White" returns FFFFFF
315
;
316
alias -l RGBToHex {
317
  var %RtoHex = $base($1,10,16,2)
318
  var %GtoHex = $base($2,10,16,2)
319
  var %BtoHex = $base($3,10,16,2)
320
  var %HexCodeReturn = %RToHex $+ %GToHex $+ %BToHex
321
  return %HexCodeReturn
322
}
323
;
324
; This is the alias for determining if your background is bright
325
;
326
; Average RGB of 100 seems to work well for determining if background is light or dark,
327
; but if you find the script is using colors that aren't showing well you might want to 
328
; adjust this
329
;
330
alias -l isBackgroundBright {
331
;
332
; get the backgound color
333
;
334
  var %bgRGB = $rgb($iif($1,$color(27),$color(34)))
335
;
336
; strip out the commas and replace them with the + sign
337
;
338
  var %bgRGB = $replace(%bgRGB,$chr(44),$chr(43))
339
;
340
; evaluate the string from above
341
;
342
  var %bgRGBAverage = $calc((%bgRGB)/3)
343
;
344
; check if the average RGB value of the background is > 100; 
345
; return 1 if it is (bright) or 0 if it isn't (dark)
346
;
347
  if (%bgRGBAverage >= 100) { 
348
    return 1
349
  }
350
  else return 0
351
}
352

    
353
;
354
; This is the alias that gets the hex value of numbered config colors 
355
; https://dev.adiirc.com/projects/adiirc/wiki/Extra_Colors
356
;
357
; It is used in this script only to keep nicks with custom colors set using 
358
; those colors instead of picking random colors
359
;
360
alias -l getHexOfColor {
361
;
362
; get the specified color's R,G,B value
363
  var %colorToGet = $rgb($color($1))
364
;
365
; tokenize the string returned above with commas ($chr(44)) as delimiter; pass those values into the $RGBToHex alias and return the value
366
  tokenize 44 %colorToGet
367
  return $RGBToHex($1,$2,$3)
368
}
369
;
370
; This is the alias for setting custom nick colors, it takes a Nick and a hex color code as arguments
371
; EXAMPLE /setCustomColor JohnDoe f854a7
372
;
373
alias setCustomColor {
374
; typing just /setcustomcolor returns rudimentary help
375
; otherwise write the nick+color to hash table rcolorscustom and save to disk
376
   $iif($1, /echo -at $+($chr(61442),$2) Set this color for nick $1, /echo -at Usage: /setCustomColor <Nick> <Hex Color Code> Example: /setCustomColor JohnDoe F854A7)
377
   
378
   hadd -m rcolorscustom $1 $2
379
   hsave -o rcolorscustom rcolorscustom
380
}
381
;
382
; This is the alias for deleting custom nick colors, it takes only a nick as an argument
383
; EXAMPLE /delCustomColor JohnDoe
384
alias delCustomColor {
385
; typing just /delcustomcolor returns rudimentary help
386
; otherwise remove nick+color from hash table rcolorscustom and save to disk
387
   $iif($1, echo -at Trying to remove custom color for $1, /echo -at Usage: /delCustomColor  <Nick> Example: /delCustomColor JohnDoe)
388
   hdel rcolorscustom $1
389
   hsave -o rcolorscustom rcolorscustom
390
}
391
;
392
; This is the alias for clearing all cached random colors
393
; It empties the rcolors hashtable, then saves the empty table back to the disk
394
; It is generally used in this script for ensuring that colors adjust accordingly to background color changes
395
;
396
alias -l reset {
397
; Remove all cached colors
398
  if ($hget(rcolors)) {
399
    .hdel -w rcolors *
400
	savereload
401
  }  
402
; Check if background is light or dark and set %rcolors_bg accordingly
403
  ;;;set %rcolors_bg $isBackgroundBright
404
  set %rcolors_bg $iif($var(%rcolors_forcestyle,1).value != $null,$v1,$isBackgroundBright)
405
  rcolors_resetnicklist
406
}
407
alias -l rcolors_resetnicklist {
408
  set %rcolors_nicklistbg $iif($var(%rcolors_nickliststyle,1).value != $null,$v1,$isBackgroundBright(nicklist))
409
}
410
;
411
; This is the alias for saving & reloading cached & custom colors to disk. It is primarily used on first run, or when
412
; settings are changed and need to be stored
413
; 
414

    
415
alias savereload {
416
; check if the rcolors hashtable exists, if it does, save it to disk, purge all entries from memory and reload it.
417
; it also checks if the hashtable is too large (more than 1000 entries) and purges it if that's the case
418
; if the hashtable is purged, colors will be recalculated and entered the next time a person speaks
419
	if ($hget(rcolors)) {
420
		if ($hget(rcolors,0).item > 1000) {
421
			.hdel -w rcolors *
422
		}
423
		hsave -o rcolors rcolors
424
		.hdel -w rcolors *
425
		hload rcolors rcolors
426
	}
427
    else { 
428
		hmake rcolors 100
429
	}
430
; same as above, but with rcolorscustom
431
; rcolorscustom will also never be auto-cleared  
432
   if ($hget(rcolorscustom)) {
433
	hsave -o rcolorscustom rcolorscustom
434
	.hdel -w rcolorscustom *
435
	hload rcolorscustom rcolorscustom
436
	}
437
            else { 
438
            hmake rcolorscustom 100
439
            }
440
                      
441
}
442

    
443
menu channel {
444
  RGB Random Nick Colors
445
  .Disable in #
446
  ..$iif($network $+ . $+ # isin %rcolors_blacklist,$style(1)) Yes:set %rcolors_blacklist $addtok(%rcolors_blacklist,$network $+ . $+ #,32) | clinergb #
447
  ..$iif($network $+ . $+ # !isin %rcolors_blacklist,$style(1)) No:set %rcolors_blacklist $deltok(%rcolors_blacklist,1,$network $+ . $+ #) | clinergb #
448
}
449

    
450
menu menubar {
451
  RGB Random Nick Colors
452
  .$iif(!%rcolors_disabled,$style(1)) Enabled:$iif(%rcolors_disabled,unset %rcolors_disabled,set %rcolors_disabled 1) | scid -a resetNicklist
453
  .-
454
  .Color messages style
455
  ..$iif(%rcolors_forcestyle == $null,$style(1)) Auto:unset %rcolors_forcestyle | reset
456
  ..$iif(%rcolors_forcestyle == 0,$style(1)) Bright:set %rcolors_forcestyle 0 | reset
457
  ..$iif(%rcolors_forcestyle == 1,$style(1)) Dark:set %rcolors_forcestyle 1 | reset
458
  .Color entire prefix user
459
  ..$iif(%rcolors_ColorEntirePrefixUser,$style(1)) Yes:set %rcolors_ColorEntirePrefixUser True
460
  ..$iif(%rcolors_ColorEntirePrefixUser != True,$style(1)) No:unset %rcolors_ColorEntirePrefixUser
461
  .Color entire message
462
  ..$iif(%rcolors_ColorEntireLine,$style(1)) Yes:.setconfig NickColors NickColorsEntireLine True | set %rcolors_ColorEntireLine True
463
  ..$iif(%rcolors_ColorEntireLine != True,$style(1)) No:.setconfig NickColors NickColorsEntireLine False | unset %rcolors_ColorEntireLine False
464
  .└ Including timestamp
465
  ..$iif(%rcolors_ColorTimestamp,$style(1)) Yes:set %rcolors_ColorTimestamp True
466
  ..$iif(%rcolors_ColorTimestamp != True,$style(1)) No:unset %rcolors_ColorTimestamp
467
  .-
468
  .$iif(%rcolors_nicklist,$style(1)) Enable nicklist colors:$iif(%rcolors_nicklist,unset %rcolors_nicklist,set %rcolors_nicklist True) | scid -a resetNicklist
469
  .Color nicklist style
470
  ..$iif(%rcolors_nickliststyle == $null,$style(1)) Auto:unset %rcolors_nickliststyle | rcolors_resetnicklist | scid -a resetNicklist
471
  ..$iif(%rcolors_nickliststyle == 0,$style(1)) Bright:set %rcolors_nickliststyle 0 | rcolors_resetnicklist | scid -a resetNicklist
472
  ..$iif(%rcolors_nickliststyle == 1,$style(1)) Dark:set %rcolors_nickliststyle 1 | rcolors_resetnicklist | scid -a resetNicklist
473
}