Professional Web Applications Themes

random text & background colors? - PHP Development

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 ...

  1. #1

    Default 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


    Bill Parker Guest

  2. #2

    Default Re: random text & background colors?

    Message-ID: <bdm774$2jj$1news-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 [url]http://www.ckdog.co.uk/rfdmaker/[/url]
    Geoff Berrow Guest

  3. #3

    Default Re: random text & background colors?

    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');
    --------------


    Bill Parker Guest

Similar Threads

  1. Background colors
    By JB in forum Macromedia Director Basics
    Replies: 10
    Last Post: October 17th, 01:50 PM
  2. Making PDF w/o web page background colors?
    By John E. in forum Adobe Acrobat Macintosh
    Replies: 1
    Last Post: April 23rd, 12:28 PM
  3. Background Colors??
    By Whatcom Co in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: May 1st, 05:43 PM
  4. random/rotating background patterns
    By lipstickface webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 3
    Last Post: July 22nd, 01:35 PM
  5. Change the edit Field background color in alternating portal colors
    By Christoph Bouthillier in forum FileMaker
    Replies: 0
    Last Post: June 30th, 11:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139