random text & background colors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Parker

    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


  • Geoff Berrow

    #2
    Re: random text &amp; background colors?

    Message-ID: <bdm774$2jj$1@n ews-reader2.wanadoo .fr> from Bill Parker
    contained the following:
    [color=blue]
    >is what I have in mind.... but I really don't know how the hex codes for rgb
    >realte to "darkness"[/color]


    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/

    Comment

    • Bill Parker

      #3
      Re: random text &amp; 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');
      --------------


      Comment

      Working...