random text & background colors?

Ask a Question related to PHP Development, Design and Development.

  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. Similar Questions and Discussions

    1. Background colors
      Jumping into the thread late... When a projector is set to "full screen" the area outside of the stage area is filled with the movie backgrounnd...
    2. Making PDF w/o web page background colors?
      When making PDF from web pages, is there a way to have Acrobat ignore the background color? If I'm only after the text & images, when printed,...
    3. Background Colors??
      Is it possible to customize cell and table background colors? I've got the eyedropper thing down - but is there a way to enter the desired color,...
    4. random/rotating background patterns
      Client wants random rotating background patterns - so each time you reload the page, or click to another page you will see a different background...
    5. Change the edit Field background color in alternating portal colors
      Sure, Marcel, please look at www.tekstotaal.com/c.fmp.portal.blackening.html -- Met vriendelijke groet / Mit freundlichen Grüßen / With kind...
  3. #2

    Default Re: random text & background colors?

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

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

Posting Permissions

  • You may not post new threads
  • You may 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