如何返回基于日期和随机的颜色值? [英] How to return a color-value based a date and random?

查看:201
本文介绍了如何返回基于日期和随机的颜色值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设计师想出了一个奇怪的想法或一个色轮(有36种颜色)。



我需要写一个函数返回一个颜色,但基于日期。


网站应如何工作

根据当前日期(您会在下面的图像上看到德国日期),网站应该有此背景颜色。


所以在1月1日,第一种颜色(蓝紫色或你可能称之为)应该是首页的背景。 10天后下一个颜色。所以在一年内,所有的36种颜色应该按顺序循环。



我想,直到一个中间程序员可以帮助我,



p>

但它稍微更复杂

设计师希望网站的每一页都使用不同的颜色。
因此,假设网站有10页(首页,关于,任何,画廊),每一页都应该有一个最接近的10种颜色。





所以我想做的是创建一个函数,返回一个 < / em>的 颜色 >所以在一月一日我想要下列颜色被推入一个数组,并随机返回这些颜色之一。

  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),
);
}

有什么想法怎么做?



<$ p

$ p> 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)];
}

我不太了解Wordpress,但得到每页一种颜色的功能你应该做一个诀窍:

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


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

I need to write a function that is returning one color but based on the date.

How the website should work
Based on the current date (you see german dates on the image underneath) the site should have this background color.

So on "January 1st" the first color (blueviolet or what you might call that) should be the background of the frontpage. 10 days later the next color. So within one year all 36 colors should be looped through in the order of the wheel.

I guess untill that point an intermediate programmer could help me with this, I don't know how to do that.

But it gets slightly more complicated
The designer wants every page of the website to be in a different color also. So imagine the site has like 10 pages (Home, About, Whatever, Gallery) every page should have one of the "closest" 10 colors.

Wow, even I don't unterstand it when explainig it.

So what I want to do is create a function that is returning a random color out of a pool of 10 colors that are based on the current date.

So on "January 1st" I want the following the colors to be pushed in an array and return one of those colors randomly.

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 so?

解决方案

This will have a one day shift every leap year but this should be ok 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 do not know much about Wordpress but to get the one color per page functionnality 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);

这篇关于如何返回基于日期和随机的颜色值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆