我的计划是根据当前本地时间(来自 javascript)给网页稍微着色。我不需要像当前流明或任何东西那样具体的东西,但我想获得峰值阳光、日出、日落和午夜 +-2 小时左右的大致时间。我意识到确切的时间会根据纬度和经度以及时区数据而有很大差异,我可能会访问这些数据。但一开始,我想知道是否只有一个公式可以计算 [北] 半球和当前当地时间。
f.lux是如何做到的?
更新 1:我的大部分搜索都刚刚返回了与夏令时相关的信息,这不是很有帮助。我确实找到了这个 JS:http ://www.esrl.noaa.gov/gmd/grad/solcalc/main.js (从这里http://www.esrl.noaa.gov/gmd/grad/solcalc/)但是它充满了无法解释的魔法常数。例如:
function calcGeomMeanLongSun(t)
{
var L0 = 280.46646 + t * (36000.76983 + t*(0.0003032))
while(L0 > 360.0)
{
L0 -= 360.0
}
while(L0 < 0.0)
{
L0 += 360.0
}
return L0 // in degrees
}
function calcGeomMeanAnomalySun(t)
{
var M = 357.52911 + t * (35999.05029 - 0.0001537 * t);
return M; // in degrees
}
function calcEccentricityEarthOrbit(t)
{
var e = 0.016708634 - t * (0.000042037 + 0.0000001267 * t);
return e; // unitless
}
更新 2:我认为通过 gps 或任何其他方式确定用户区域设置的“成本”太大(特别是因为这纯粹是出于美观原因,没有其他功能目的),所以我可能会坚持无论当地时间是通过 javascript 的 12am-6am-12pm-6pm 周期。
更新 3:我只是稍微修改了正弦波,对白天有一点偏好:
var x, fx, hour,
// starting hsl value for midnight:
h = 220,
s = 42,
l = 75;
for (hour = 0; hour < 24; hour++) {
// 0 for midnight, up to 12 for noon
x = 12 - Math.abs(hour - 12);
// 0.0 to 1.0
fx = x / 12;
// factor of pi, shift x axis by 1 half pi
fx = (Math.PI * (fx - (1 / 2)));
// sine function
fx = Math.sin(fx);
// +1 to start at 0, take half to max out at one
fx = (fx + 1) / 2;
// skew the values just slightly for daytime
fx = Math.pow(fx, 0.75);
// change range back to top out at 12
fx = fx * 12;
// base of 220 degrees, 18.25 provided a nice color rage from bluish to yellowish
h = Math.floor((220 + (18.25 * fx)));
// rotate on 360 degrees
while (h >= 360) {
h = h - 360;
}
// base of 42% saturated, multiplied x for a linear slope up to 100%
s = Math.floor(42 + (5.5 * x));
// 100 max
if (s > 100) {
s = 100;
}
// base of 75% lightness, 1.85 factor was a nice linear slope stopping short of 100%
l = Math.floor(75 + (1.85 * x));
// 100 max
if (l > 100) {
l = 100;
}
// "style='background-color: hsl(" + h + ", " + s + "%, " + l + "%);'"
}
这是在 JSBin 上。将来我可能会尝试获得实际金额,但这让我现在足够接近。