How to return color value based on date and randomness?

The designer came up with a rather strange idea or a color wheel (with 36 colors).

I need to write a function that returns the color one , but based on date .

How the site works
Based on the current date (you see German dates in the image below), the site should have this background color.

So, January 1st, the first color (blue or whatever you might call it) should be the background on the main page. After 10 days, the next color. Therefore, within one year, all 36 colors must be shorted in wheel order.

I think, until the intermediate programmer can help me, I don’t know how to do it.

But it gets a little complicated
The designer wants each page of the website to have a different color. So, imagine that the site has 10 pages (Home, About, Independently, Gallery), each page should have one of the β€œnearest” 10 colors.

Wow, even I can't stand it when I explain it.

So, I want to create a function that returns a random color from a pool of 10 colors that are based on the current date.

So, on January 1st, I want the following colors to be placed in an array and randomly select one of these colors.

 function colorWheel($alpha) { // 36 colors $colors = array( rgba(170, 207, 172, 1), rgba(180, 211, 164, 1), rgba(189, 214, 145, 1), rgba(196, 217, 134, 1), rgba(206, 222, 124, 1), rgba(214, 226, 124, 1), rgba(226, 233, 124, 1), rgba(234, 235, 122, 1), rgba(236, 235, 120, 1), rgba(241, 231, 118, 1), rgba(240, 224, 118, 1), rgba(240, 216, 117, 1), rgba(237, 208, 115, 1), rgba(233, 199, 112, 1), rgba(230, 191, 110, 1), rgba(226, 177, 115, 1), rgba(221, 162, 110, 1), rgba(218, 153, 116, 1), rgba(215, 141, 112, 1), rgba(209, 140, 120, 1), rgba(203, 138, 119, 1), rgba(197, 136, 126, 1), rgba(191, 138, 134, 1), rgba(186, 142, 144, 1), rgba(181, 145, 157, 1), rgba(176, 151, 170, 1), rgba(170, 135, 178, 1), rgba(164, 159, 189, 1), rgba(166, 167, 194, 1), rgba(166, 177, 201, 1), rgba(166, 182, 204, 1), rgba(163, 186, 201, 1), rgba(164, 190, 196, 1), rgba(166, 196, 191, 1), rgba(167, 198, 185, 1), rgba(168, 201, 178, 1), ); } 

Any idea how to do this?

+4
source share
3 answers

It will be a one-day shift for each leap year, but it should be good for your needs.

 function colorWheel($alpha, $shift = 0) { // 36 colors $time = time(); $yearDay = $time % (60 * 60 * 24 * 365); $idx = $yearDay / 60 / 60 / 24 / 10; $colors = array( rgba(170, 207, 172, $alpha), … rgba(168, 201, 178, $alpha), ); return $colors[($idx + $shift) % count($colors)]; } 

I don’t know much about Wordpress, but to get one color per page, you should do a trick like:

 $page_shift = array( '/about.html' => 1, '/whatever.html' => 2, '/gallery.html' => 3, … ); $shift = $page_shift[$_SERVER['REQUEST_URI']]; $color = colorWheel(1, $shift); 
+3
source

Try the following:

 <?php // $colors contains what the designers you gave ... $colors = array ( array (0x0, 0x0, 0x0), // ... array (0xFF, 0xFF, 0xFF), ); $number_of_colors = count($colors); // 36 in your example $number_of_days_per_year = 365; // for brevity of the example $day = intval(date('z')); // some 'lower school math' magic :) $index = round($day * $number_of_colors / $number_of_days_per_year); // the color of the day is ..... $color = $colors[$index]; 
+1
source

Well, here is what I came up with:

 function colorWheel($time, $page) { // All possible RGB color values $colors = array( array(170, 207, 172, 1), // ... ); $index = ($time % (60 * 60 * 24 * 365) / 60 / 60 / 24 / 10); return 'background-color: rgba(' . ($colors[($index) % count($colors)-1][0]) . ', ' . ($colors[($index) % count($colors)-1][1]) . ', ' . ($colors[($index) % count($colors)-1][2]) . ', ' . ($colors[($index) % count($colors)-1][3]) . ');'; } // Usage: echo "<body style='" . colorWheel(time(), 'home') . "'>"; 

Although I did not implement the functionality of different colors for each page, tell me, is it possible to use a session, a text file or a database to store which page has which color?

+1
source

All Articles