473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

random text & background colors?

Hi

Is there a way to generate colour hex strings that are guaranteed to be dark
and thus good as random-generated text colours?

something like
-------------------
srand((double)microtime()*1000000);

function tc() {

$frag = range(0,6);
$text = "#";

for ($i = 1; $i <= 6; $i++) {
$text = $text . $frag[mt_rand(0, count($frag)-1)];
}
return $text;
}

$text = tc();
--------------------

is what I have in mind.... but I really don't know how the hex codes for rgb
realte to "darkness"

And while I'm on the subject, whats the converse (for pale colours, for
backgrounds) look like?

I'm sure someone will have done this somewhere before...

Cheers

Bill
Jul 16 '05 #1
2 9233
Message-ID: <bd**********@news-reader2.wanadoo.fr> from Bill Parker
contained the following:
is what I have in mind.... but I really don't know how the hex codes for rgb
realte to "darkness"

You have six hexadecimal values - two for red, two for green, two for blue.
The two values multiplied together give a range of 0-255 for each colour

A hex number is a number between 0 and 15 which is displayed as the numbers
0-9 and the letters A-F. Hence A = 10, B=11 and so on. Light colours have
higher values, and hence #000000 is black and #FFFFFF is white. So a mid
grey is between #777777 and #888888.

I assume that your definition of a dark colour would be any colour that was
darker than this mid point. (though this is clearly going to be subjective)

From the above you can see that any colour whose hex values are less than
or equal to 7 is guaranteed to be a dark colour and any colour where values
are greater than or equal to 8 is light. Also, any colour where the
/average/ of the hex values is less than 7 is dark also. So #000ccc has an
average of (0+0+0+12+12+12)/6 which equals 6 and so is 'dark'. Similarly,
#00FFFF has an average of 10 and is hence 'light'.
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 16 '05 #2
Thanks for the detailed explanation. The bit about taking an average of the
three values was especially informative to me.

If anyone else is interested, I've put together this very simple function by
way of an example (I'm using VERY conservative values for "lightness" and
"darkness")

Bill
-----------
function colour($tint) {

$frag = range(0,255);

$red = "";
$green = "";
$blue = "";

for (;;) {

$red = $frag[mt_rand(0, count($frag)-1)];
$green = $frag[mt_rand(0, count($frag)-1)];
$blue = $frag[mt_rand(0, count($frag)-1)];

switch ($tint) {
case 'light':
if (($red + $green + $blue / 3) >= 200) break 2;
break;
case 'dark' :
default:
if (($red + $green + $blue / 3) <= 50) break 2;
break;
}
}
return sprintf("#%02s%02s%02s", dechex($red), dechex($green),
dechex($blue));
}

$background = colour('light');
$text = colour('dark');
--------------
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
4
by: Franklin | last post by:
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the foreground/background colors of a link when someone hovers over it. Is this possible with HTML, CSS, DOM, & JavaScript? If...
12
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could post a few lines of code which would produce random colors, which will be used in defining different regions on a mesh. So in addition to having n...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
10
by: Michael B. Trausch | last post by:
Alright... I am attempting to find a way to parse ANSI text from a telnet application. However, I am experiencing a bit of trouble. What I want to do is have all ANSI sequences _removed_ from...
39
by: dancer | last post by:
Can somebody tell me why I get this message with the following code? Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.TextBox'. ...
1
by: markfrmtls | last post by:
I am not totally a novice with css, but only have self-trained experience. I am looking for a way to randomly change the background color and text color with css. My thought is to put a counter on...
5
by: markfrmtls | last post by:
I am not totally a novice, but only have self-trained experience. I am looking for a way to randomly change the background color and text color with my web pages. My thought is to put a counter on my...
3
by: schof | last post by:
I am using the following code to call a random background image to be displayed on the far right hand side of the page ... <script type="text/javascript"> function random(n) { return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.