Re: Dynamically Generate Pleasing Colors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well I would suggest you find some PHP code That converts between HSB/HSL (Hue-Saturation-Brightness/Lightness). From there are are a multitude of fairly simple algorithms that will give you complementary colors. You can find these by searching on "Complimentary Colors", "Color Wheel", "Adjacent Colors" to name a few.

This has the added bonus that you can restrict the brightness of a color by the brightness itself, not arbirtrailly disallowing any of the 3 RGB values to be below a certain point.

Here is a bit of code that would generate a random color, above a specified brightness, and also give you it's complimentary colors. It assumes a Hue value 0-359 , as opposed to some which are used on a 0-99 scale.

<?php
$iMinB = 62; // This represents a percentage

$iH = rand(0,359); // Generate random Hue
$fS = rand(3000,7000)/10000; // Generate Random Float between 0.3 and 0.7
$fB = rand($iMinB*100,10000)/10000; // Generate Random Float between 0.62 and 1.0


/* Now, the complimentary colors will have the same Saturation and Brightness, jsut different hue values, rotate 120 degrees apart */
$iComp1H = ($iH+120)%360;
$iComp2H = ($iComp1H+120)%360;


echo '       Hue: ',$iH,"\n";
echo 'Comp 1 Hue: ',$iComp1H,"\n";
echo 'Comp 2 Hue: ',$iComp2H,"\n";
echo 'Saturation: ',$fS,"\n";
echo 'Brightness: ',$fB,"\n";
?>

This is hastily put together, but it's the sort of thing that would definitely work, and, in my opinion, works best. You'll just need something to convert HSB to RGB, to get the color in a form GD understands.

One alternative, with HSB, is to generate random RGB values, then just rotate them through (RGB = Primary color, GBR = Color 1, GRB = Color 2)

This generally produces pretty color patterns.

Chris




Richard Lynch wrote:

I'm drawing a GD map from selected points in a database.

I am using imagecreatetruecolor.

I'm outputting PNGs, but could change to JPEG if it would help. [I doubt it]

In experimentation so far, I've found that using the lighter shades is
more pleasing to the eye, in general, so want to stay above 0x66 in R, G,
and B values.

The background will be white, so I can't get too close to that color either.

I'd like the colors to be not TOO ugly with each other, and I absolutely
cannot have the borders of two regions the exact SAME color.

Now, I don't want to get into Fermat's Last Theorom and 2 billion lines of
code to do the 4-color thing, so that's out.

And I'm not a Designer, so I have NO IDEA if there's some magical listing
of "pretty colors number 1 through N" that go together.  I'm still waiting
for "Geranimals for Men" to come out. :-)

I could just use $N colors, for however many colors I need, and not worry
about the borders at all.  The occasional case of two shades of green
being too close is "livable" though not "ideal"

I've tried your basic 3-D loop:
for ($r ...)
 for ($g ...)
   for ($b ...)
      $colors[] = imagecolorallocate($image, $r, $g, $b);
to generate the colors with acceptable ranges in values, skipping shades
of grey, or even any shades where $r == $g || $r == $b || $g == $b to try
to avoid same-looking colors.

I probably won't ever need more than 250 colors, really, so played around
with settings that seemed like they should work.

I used shuffle() sometimes to try and mix up the colors

I've had what you might call mixed results...

I've searched and read, and only vaguely understand the ideas behind
brightness and contrast in RGB number system, so can't figure out anything
useful from my findings of maximizing contrast of an existing image etc.

I'm not using php 5 yet, so imagefilter is "out"

I'm hoping somebody has gone through this already, and has an easy way to
generate a pleasing set of colors, with good contrast, for any given
number $N of colors.




-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux