473,788 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)m icrotime()*1000 000);

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 9257
Message-ID: <bd**********@n ews-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
2506
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
4
4098
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 so, how? See my HTML below (or go to my test page at http://fmbbowen.com:39353/misc/Misc.html). I also tried treating ..color as a number and subtracting the current color from 0xFFFFFF but that did not work either. The numerous examples I...
12
3099
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 random colors, I feel that there should also be some condition to ensure that they aren't too similar in appearance, given that is likely to range from 2-20 say. Any help much appreciated,
4
2757
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. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is that I can't view the source???? The view source is disabled for this page. What causes this?
10
3203
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 the output, save for those that manage color codes or text presentation (in short, the ones that are ESChttp://fd0man.theunixplace.com/Tmud.tar which contains the code in question. In short, the information is coming in over a TCP/IP socket that...
39
4733
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'. <html> <head> <% @Import Namespace="System.Web.Mail" %>
1
4359
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 my home page and every even number on the counter the background color will be yellow and every odd number the colors will change to something else. If this is possible with css only I would be truly grateful to learn how this would work. I am...
5
1901
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 home page and every even number on the counter the background color will be yellow and every odd number the colors will change to something else. If there4 is a script for this I would be truly grateful to learn how this would work. I have posted on...
3
2745
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 Math.floor((Math.random() % 1) * n); } document.documentElement.style.background="url('photo"++".jpg') repeat-y 890px 0 background-attachment: fixed" </script> This is working fine in explorer - but does not bring up the image in safari or mozilla ... any...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.