Project

General

Profile

RE: [Script] Full-Pallette Random Nick Color Script 1.2 » Full-Pallette Random Nick Color Script fork v0.1.ini

Mr. BS, 05/03/2016 11:59 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 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
    %msg = %linecolor $+ $regsubex(%msg,$patLine,%linecolor)
107
    if (%rcolors_ColorTimestamp) { %time = %linecolor $+ $regsubex(%time,$patLine,%linecolor) $+ $chr(3) }
108
  }
109
  
110
  %msg = %time %prefixuser %msg
111
  echox -vrlmbfw2 # %msg
112
  halt
113
}
114
;
115
; This section handles ACTIONs, like /me or /emote commands
116
; Mostly the same as PRIVMSG but with the output string adjusted to properly display these types of messages
117
;
118
on &^*:ACTION:*:#:{
119
  ;
120
  ; Stop here if user turned off script
121
  ;
122
  if (%rcolors_disabled) return
123
  var %time $spacefix($asctime($iif($msgstamp, $v1, $ctime), $timestampfmt))
124
  var %pnick $iif($nick(#,$nick).cnick, $v1, $nick)
125
  var %msg %time $replace($replace($prefixemote, $chr(32), $chr(160)), $ $+ pnick, $+($chr(61442), $getcolor, %pnick, $chr(3))) $msgx
126
;build the echo string  
127
  echox -vrlmbfw2c action # %msg
128
  halt 
129
}
130

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

    
155
    ; If Options -> Nick Colors -> Color entire line is enabled:
156
    if (%rcolors_ColorEntireLine) {
157
      %msg = %linecolor $+ $regsubex(new,%msgxraw,$patLine,%linecolor)
158
      if (%rcolors_ColorTimestamp) { %time = %linecolor $+ $regsubex(%time,$patLine,%linecolor) $+ $chr(3) }
159
    }
160

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

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

    
185
on *:JOIN:#:{
186
  ; If enabled continue
187
  if (%rcolors_nicklist) {
188
    cline -b $rgb( [ $hex2rgb($iif(%rcolors_nicklistbg == 1, $getDarkColor($nick), $getBrightColor($nick))) ] ) $nick
189
  }
190
}
191
; credits @ http://forums.mirc.com/ubbthreads.php/topics/153480/HEX_=%3E_RGB?
192
alias hex2rgb {
193
  $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))
194
}
195

    
196
alias -l clinergb {
197
  if ($istok(%rcolors_blacklist,$network $+ . $+ $1,32)) {
198
    var %rcolors_disabled 1
199
  }
200
  var %n $nick($1,0)
201
  while (%n) {
202
    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)
203
    dec %n
204
  }
205
}
206

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

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

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

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

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

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

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

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

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

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