<?php
function interpolar_cor($cor0, $cor1, $valor) {
if ($valor <= 0) return $cor0;
if ($valor >= 1) return $cor1;
list($r0, $g0, $b0) = sscanf($cor0, "#%02x%02x%02x"); list($r1, $g1, $b1) = sscanf($cor1, "#%02x%02x%02x"); $rx = (int) ($valor * ($r1 - $r0) + $r0);
$gx = (int) ($valor * ($g1 - $g0) + $g0);
$bx = (int) ($valor * ($b1 - $b0) + $b0);
return "#" . str_pad(dechex($rx * 256 * 256 + $gx * 256 + $bx), 6, "0", STR_PAD_LEFT
); }
function interpolar_cor_data($cor0, $data0, $cor1, $data1, $dataX) {
$dias_max = $data1->diff($data0)->days;
if ($dias_max <= 0) $dias_max = 1;
$dias_t = $dataX->diff($data0)->days;
return interpolar_cor($cor0, $cor1, $dias_t / (float) $dias_max);
}
$data_teste1 = new DateTime("2018-04-01");
$data_teste2 = new DateTime("2018-04-05");
$data_teste3 = new DateTime("2018-04-10");
$data_teste4 = new DateTime("2018-04-20");
$data_teste5 = new DateTime("2018-04-30");
$data_teste6 = new DateTime("2018-05-04");
$data_hoje = new DateTime("2018-05-04");
$data_inicio = new DateTime("2018-04-01");
// Entre branco e preto:
$branco = "#ffffff";
$preto = "#000000";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste1) . "\n";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste2) . "\n";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste3) . "\n";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste4) . "\n";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste5) . "\n";
echo interpolar_cor_data($branco, $data_inicio, $preto, $data_hoje, $data_teste6) . "\n";
// Entre vermelho e verde:
$vermelho = "#ff0000";
$verde = "#00ff00";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste1) . "\n";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste2) . "\n";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste3) . "\n";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste4) . "\n";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste5) . "\n";
echo interpolar_cor_data($vermelho, $data_inicio, $verde, $data_hoje, $data_teste6) . "\n";
?>
PD9waHAKCiAgICBmdW5jdGlvbiBpbnRlcnBvbGFyX2NvcigkY29yMCwgJGNvcjEsICR2YWxvcikgewogICAgICAgIGlmICgkdmFsb3IgPD0gMCkgcmV0dXJuICRjb3IwOwogICAgICAgIGlmICgkdmFsb3IgPj0gMSkgcmV0dXJuICRjb3IxOwogICAgICAgIGxpc3QoJHIwLCAkZzAsICRiMCkgPSBzc2NhbmYoJGNvcjAsICIjJTAyeCUwMnglMDJ4Iik7CiAgICAgICAgbGlzdCgkcjEsICRnMSwgJGIxKSA9IHNzY2FuZigkY29yMSwgIiMlMDJ4JTAyeCUwMngiKTsKICAgICAgICAkcnggPSAoaW50KSAoJHZhbG9yICogKCRyMSAtICRyMCkgKyAkcjApOwogICAgICAgICRneCA9IChpbnQpICgkdmFsb3IgKiAoJGcxIC0gJGcwKSArICRnMCk7CiAgICAgICAgJGJ4ID0gKGludCkgKCR2YWxvciAqICgkYjEgLSAkYjApICsgJGIwKTsKICAgICAgICByZXR1cm4gIiMiIC4gc3RyX3BhZChkZWNoZXgoJHJ4ICogMjU2ICogMjU2ICsgJGd4ICogMjU2ICsgJGJ4KSwgNiwgIjAiLCBTVFJfUEFEX0xFRlQpOwogICAgfQoKICAgIGZ1bmN0aW9uIGludGVycG9sYXJfY29yX2RhdGEoJGNvcjAsICRkYXRhMCwgJGNvcjEsICRkYXRhMSwgJGRhdGFYKSB7CiAgICAgICAgJGRpYXNfbWF4ID0gJGRhdGExLT5kaWZmKCRkYXRhMCktPmRheXM7CiAgICAgICAgaWYgKCRkaWFzX21heCA8PSAwKSAkZGlhc19tYXggPSAxOw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