|
Page 1 of 1
|
mort
Spam Basher
Joined: August 2010
Posts: 998
Location: Up a tree
|
Randomising Text Colour
Mmm? I can't post in "PHP Scripts" so I'll dump it here.
Here's a cute little thing that I found in my travels that randomises the colour of texts etc - - - -
Da Script:
<?php
function get_random_color()
{
for ($i = 0; $i<6; $i++)
{
$c .= dechex(rand(0,15));
}
return "#$c";
}
?>
Da Usage Sample:
<?php
echo '<span style="color:'.get_random_color().'; font-weight: bold;">'.$r['username'].'</span><br />';
?>
And one gets something like this:
Mort
Mighty G
Joshua
Spydie
Knackers
|
#1 Sun 16 Oct, 2011 05:08 |
|
Sponsors
|
Icy Phoenix is an open source project, you can show your appreciation and support future development by donating to the project.
|
|
TheSteffen
Joined: August 2006
Posts: 1625
Location: Magdeburg
|
Re: Randomising Text Colour
Mmm? I can't post in "PHP Scripts" so I'll dump it here.
Thanks Mort, I moved it to Free PHP Scripts
____________ TheSteffen
Often SEARCH is the quickest way to get support.
IcyPhoenix German Support
|
#2 Sun 16 Oct, 2011 09:40 |
|
Mighty Gorgon
Luca Libralato
Joined: August 2006
Posts: 7192
Location: Borgo San Michele
|
Re: Randomising Text Colour
Thanks for sharing Mort, there are similar (not identical!) scripts in bbcode.php that you can use here.
/**
* Load rainbow colors
*/
function load_rainbow_colors()
{
return array(
1 => 'red',
2 => 'orange',
3 => 'yellow',
4 => 'green',
5 => 'blue',
6 => 'indigo',
7 => 'violet'
);
}
/**
* Apply rainbow effect
*/
function rainbow($text)
{
// Returns text highlighted in rainbow colours
$colors = $this->load_rainbow_colors();
$text = trim($text);
$length = strlen($text);
$result = '';
$color_counter = 0;
$TAG_OPEN = false;
for ($i = 0; $i < $length; $i++)
{
$char = substr($text, $i, 1);
if (!$TAG_OPEN)
{
if ($char == '<')
{
$TAG_OPEN = true;
$result .= $char;
}
elseif (preg_match("#\S#i", $char))
{
$color_counter++;
$result .= '<span style="color: ' . $colors[$color_counter] . ';">' . $char . '</span>';
$color_counter = ($color_counter == 7) ? 0 : $color_counter;
}
else
{
$result .= $char;
}
}
else
{
if ($char == '>')
{
$TAG_OPEN = false;
}
$result .= $char;
}
}
return $result;
}
function rand_color()
{
$color_code = mt_rand(0, 255);
if ($color_code < 16)
{
return ('0' . dechex($color_code));
}
else
{
return dechex($color_code);
}
}
function load_random_colors($iterations = 10)
{
$random_color = array();
for ($i = 0; $i < $iterations; $i++)
{
$random_color[$i + 1] = '#' . $this->rand_color() . $this->rand_color() . $this->rand_color();
}
return $random_color;
}
function load_gradient_colors($color1, $color2, $iterations = 10)
{
$col1_array = array();
$col2_array = array();
$col_dif_array = array();
$gradient_color = array();
$col1_array[0] = hexdec(substr($color1, 1, 2));
$col1_array[1] = hexdec(substr($color1, 3, 2));
$col1_array[2] = hexdec(substr($color1, 5, 2));
$col2_array[0] = hexdec(substr($color2, 1, 2));
$col2_array[1] = hexdec(substr($color2, 3, 2));
$col2_array[2] = hexdec(substr($color2, 5, 2));
$col_dif_array[0] = ($col2_array[0] - $col1_array[0]) / ($iterations - 1);
$col_dif_array[1] = ($col2_array[1] - $col1_array[1]) / ($iterations - 1);
$col_dif_array[2] = ($col2_array[2] - $col1_array[2]) / ($iterations - 1);
for ($i = 0; $i < $iterations; $i++)
{
$part1 = round($col1_array[0] + ($col_dif_array[0] * $i));
$part2 = round($col1_array[1] + ($col_dif_array[1] * $i));
$part3 = round($col1_array[2] + ($col_dif_array[2] * $i));
$part1 = ($part1 < 16) ? ('0' . dechex($part1)) : (dechex($part1));
$part2 = ($part2 < 16) ? ('0' . dechex($part2)) : (dechex($part2));
$part3 = ($part3 < 16) ? ('0' . dechex($part3)) : (dechex($part3));
$gradient_color[$i + 1] = '#' . $part1 . $part2 . $part3;
}
return $gradient_color;
}
function gradient($text, $color1, $color2, $mode = 'random', $iterations = 10)
{
// Returns text highlighted in random gradient colours
if ($mode == 'random')
{
$colors = $this->load_random_colors();
}
else
{
$colors = $this->load_gradient_colors($color1, $color2, $iterations);
}
$text = trim(stripslashes($text));
$length = strlen($text);
$result = '';
$color_counter = 0;
$TAG_OPEN = false;
for ($i = 0; $i < $length; $i++)
{
$char = substr($text, $i, 1);
if (!$TAG_OPEN)
{
if ($char == '<')
{
$TAG_OPEN = true;
$result .= $char;
}
elseif (preg_match("#\S#i", $char))
{
$color_counter++;
$result .= '<span style="color: ' . $colors[$color_counter] . ';">' . $char . '</span>';
$color_counter = ($color_counter == $iterations) ? 0 : $color_counter;
}
else
{
$result .= $char;
}
}
else
{
if ($char == '>')
{
$TAG_OPEN = false;
}
$result .= $char;
}
}
return $result;
}
____________ Luca
SEARCH is the quickest way to get support.
Icy Phoenix ColorizeIt - CustomIcy - HON
|
#3 Sun 16 Oct, 2011 13:46 |
|
mort
Spam Basher
Joined: August 2010
Posts: 998
Location: Up a tree
|
Re: Randomising Text Colour
Yep, I knew that was there Luca , but IMHO they're both toys for those who don't know any better.
Both have foreground-background colour compatibility problems with words and letters disappearing, and that's what I mean by toys!
Imagine if one was to script them to use the forum css tags?
No maybe not!
|
#4 Thu 20 Oct, 2011 14:15 |
|
Arsen01
Arsen Adams
Joined: December 2017
Posts: 3
|
Re: Randomising Text Colour
I am currently successfully using the following code to randomly change the color of specified text ("quotations" - see below) each time the page is reloaded. The only problem is that the colors are too random. Instead of the code I now have, I'm looking for code that would allow me to define a distinct list of colors through which the text would randomly rotate each time the page loads. In other words, I want to choose each of the randomly loaded colors of my text. Hope that makes sense... Perhaps some kind of an array?
|
#5 Wed 13 Dec, 2017 14:55 |
|
Informpro
Joined: October 2008
Posts: 1110
Location:
|
Re: Randomising Text Colour
Hi, sorry for the delay.
Can you tell me which colors you'd like?
|
#6 Wed 27 Dec, 2017 17:19 |
|
|
Page 1 of 1
|
Was this topic useful?
Was this topic useful?
Link this topic |
URL |
|
BBCode |
|
HTML |
|
You cannot post new topics You cannot reply to topics You cannot edit your posts You cannot delete your posts You cannot vote in polls You cannot attach files You can download files You cannot post calendar events
|
|
|
|