Set a color for a given time of the year

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bricedre
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 7:52 am

Set a color for a given time of the year

Post by Bricedre »

Hi everybody, I'm a French PHP developper,I recently started working on a website and I was just finishing the layout when a beautiful idea came to my mind. What about a color-changing layout (header jpg, borders of the body and text) according to the time of the year, following this schematic :
Image

Thus affecting the mood of the website. The best way, IMO, to do so would be to select 2 shades of color, a dark and a light one. The light one would affect the hue of images and borders. The main titles would be in light, the sub titles in the dark one.

Could you steer me towards a good way to code this thing, if there is so ??

Bye bye,
Bricedre !
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Set a color for a given time of the year

Post by AbraCadaver »

Several ways. You can use month numbers just as easily, but here is an example:

Code: Select all

$colors = array(
				'January' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								  ),
		  		'February' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								  ),
				'March' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'April' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'May' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'June' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'July' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'August' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'September' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'October' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'November' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								),
				'December' => array('light' => '#FFFFFF',
						'dark'  => '#000000'
								)
			   );
			   
$month = date('F');

$light = $colors[$month]['light'];
$dark  = $colors[$month]['dark'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Bricedre
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 7:52 am

Re: Set a color for a given time of the year

Post by Bricedre »

Thanks very much, I try it right now ^^

PS : What if I want it to be daily-based (different tone each day), and how can I change the hue of my header image ?Please.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Set a color for a given time of the year

Post by requinix »

An interesting idea. Wonder if it's been done before.

HSV (or HSL) would be easiest for the initial math, but the numbers would probably have to be converted to RGB.
That January's blue is HSV 240,100%,100%. The H increases and wraps around 360->0 until getting back to 240. The equation to calculate the H according to the day of the year would be

Code: Select all

H(start, dayofyear, daysinyear) = (start + 360 * (dayofyear - 1) / daysinyear) mod 360
PHPized would be

Code: Select all

$H = fmod($start + 360 * date("z") / (365 + date("L")), 360.0);
S and V would be fixed according to whether you wanted a highlight, shadow, midtone, etc.

While I'm at it,

Code: Select all

function hsv2rgb($h, $s, $v) {
    $c = $v * $s;
    $h1 = fmod($h, 360.0) / 60;
    $x = $c * (1 - abs(fmod($h1, 2.0) - 1));
    $m = $v - $c;

    $lookup = array(
        array($c, $x, 0),
        array($x, $c, 0),
        array(0, $c, $x),
        array(0, $x, $c),
        array($x, 0, $c),
        array($c, 0, $x)
    );
    list($r, $g, $b) = $lookup[floor($h1)];
    $r = round(255 * ($r + $m)); $g = round(255 * ($g + $m)); $b = round(255 * ($b + $m));
    return array(
        0 => ($r << 16) + ($g << 8) + $b,
        "r" => $r, "g" => $g, "b" => $b,
        "hex" => sprintf("%02X%02X%02X", $r, $g, $b),
        "rgb" => sprintf("rgb(%u, %u, %u)", $r, $g, $b)
    );
}
Oct 11's color is this light greenish/blueish color, which matches up with the chart you posted.
Bricedre
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 7:52 am

Re: Set a color for a given time of the year

Post by Bricedre »

Brilliant, thank you very much for the converter and the piece of code :D

Do you know, now, how to modify an image in php ? brightness, hue, saturation, even contrast ??

EDIT : Found the perfect script here ;)
http://stackoverflow.com/questions/1890 ... gd-library

I'll Try this tomorrow, well this afternoon ^^
Post Reply