This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

random colors per character

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
s
simo
Revered One
Posts: 1106
Joined: Sun Mar 22, 2015 2:41 pm

random colors per character

Post by simo »

greetingz folks,

ive tried this to assign a color to each character in the text but it seems to do for the entire sentence instead of per character:

Code: Select all


set xfgx {00 08}
set xbgx {01 02 05 06 10 12}
proc wildcharcolor {text} {
   set xmfgx [lindex $::xfgx [rand [llength $::xfgx]]]
   set xmbgx [lindex $::xbgx [rand [llength $::xbgx]]]
   set xsepx "\003${xmfgx},${xmbgx}"
   set xregx $xsepx
   for {set i 0} {$i<=[string length $text]} {incr i} {
      append xregx [string index $text $i]$xsepx
   }
   return $xregx 
}



bind pub -|- !xcols pub:text:rcolors

proc  pub:text:rcolors {nick uhost hand chan text} {
      if {![isatleasthalfop2017ewa $nick $chan]} { return 0 } 
      set text [regsub -all -- {\s{2,}} [string trim [stripcodes * $text]] { }]
       putserv "privmsg $chan :[wildcharcolor $text]"
}


M
MMX
Voice
Posts: 5
Joined: Sun Mar 20, 2022 5:42 pm

Post by MMX »

Here are two variations with and without background color.
I hope they will be useful to you.

Code: Select all

# completely random colors per character, no background colors
proc rcpc {text} {
	foreach char [split [string trim [stripcodes * $text]] ""] {
		lappend output "\003[expr int(rand()*15)+1]$char\003"
	}
	return [join $output ""]
}

# random foreground and background colors from lists
proc rcpc_bg {text} {
	set bg_colors "03 08 11"
	set fg_colors "01 02 05 06 10 12"
	foreach char [split [string trim [stripcodes * $text]] ""] {
		lappend output "\003[lindex $fg_colors [rand [llength $fg_colors]]],[lindex $bg_colors [rand [llength $bg_colors]]]$char\003"
	}
	return [join $output ""]
}
s
simo
Revered One
Posts: 1106
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

excellent that seems to work well, thanks MMX
s
simo
Revered One
Posts: 1106
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

except the proc wich has the background color as well seems to color the spaces as well while we want the spaces to be blank and only have the actual characters with fg color and bg color
M
MMX
Voice
Posts: 5
Joined: Sun Mar 20, 2022 5:42 pm

Oops ;)

Post by MMX »

Oops :oops:
Try this one.

Code: Select all

# random foreground and background colors from lists
proc rcpc_bg {text} {
	set bg_colors "03 08 11"
	set fg_colors "01 02 05 06 10 12"
	foreach char [split [string trim [stripcodes * $text]] ""] {
		if {$char eq " "} {
			lappend output $char
		} else {
			lappend output "\003[lindex $fg_colors [rand [llength $fg_colors]]],[lindex $bg_colors [rand [llength $bg_colors]]]$char\003"
		}
	}
	return [join $output ""]
}
s
simo
Revered One
Posts: 1106
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

excellent that seems to work well thanks MMX much apreciated
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Given that the rand that's implemented in eggdrop returns a random integer between 0 and limit-1, for example a [rand 10] would pick a random number from position 0 (in our case 1) up to limit (in our case it's 10) - 1, so last element will be 9, this means at least in theory, 10 will never be picked.

To put this theory to the test I mimic the rand function in tclsh:

Code: Select all

set max 10
% for {set x 0} {$x<10000} {incr x} {
if {[expr {int(rand()*$max)}] == 10} {
puts "match!"
break
}
}
%
and then if I change from 10 to 9:

Code: Select all

% for {set x 0} {$x<10000} {incr x} {
if {[expr {int(rand()*$max)}] == 9} {
puts "match!"
break
}
}
match!
%
Notice the lack of a match in first case. Now, if we add +1 at the end then:

Code: Select all

% for {set x 0} {$x<1000} {incr x} {
if {[expr {int(rand()*$max)} + 1] == 10} {
puts "match!"
break
}
}
match!
%
So with +1 it works as expected.

TLDR: rand works between 0 and limit-1, thus you need to make it [rand limit + 1], else last element will never be picked.
Once the game is over, the king and the pawn go back in the same box.
User avatar
CrazyCat
Revered One
Posts: 1296
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

When working with rand on list, [rand [llength $list]] is perfect as list index begins at 0. llength is alway last index + 1.
Last edited by CrazyCat on Mon May 15, 2023 12:40 pm, edited 1 time in total.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

No need for +1

Post by SpiKe^^ »

caesar,

I don't agree with any of that.

[rand] works well with [lindex] and [llength], without the need for any extra math:)
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

After a bit of consideration, I decided to put things to a test.

The rand command (that comes with eggdrop) outputs a random value from the given LIMIT, of 0 up to LIMIT-1, thus, giving it a limit of 5, it can output the values 0, 1, 2, 3, 4.

Code: Select all

% set fg_colors "01 02 05 06 10 12"
01 02 05 06 10 12
% llength $fg_colors
6
%
With that limit-1 from the rand this means it will start from 0 and end at 5. To test that:

Code: Select all

% set fg_colors "01 02 05 06 10 12"
01 02 05 06 10 12
% lindex $fg_colors 5
12
% lindex $fg_colors 0
01
And the result is correct, no need to change anything if you use the rand output on a list with lindex.

But, if you want to get a random number starting with 1 up to a certain limit, let's say 1 to 100, then you need that +1 I mentioned initially. :)
Once the game is over, the king and the pawn go back in the same box.
Post Reply