我正在创建一个功能来创建像Image link这样的图像 我的意思是我想显示像
这样的颜色目前,我认为只有if else条件才有可能。
我是按照以下方式做的:
function getBackground($val) {
if($val<=0.3)
return "#0A3A0A";
if($val< 0.5)
return "#1E4E1E";
if($val< 1)
return "#508050";
if($val< 1.4)
return "#FFFF0A";
if($val< 1.8)
return "#FFFF1E";
if($val< 2)
return "#FFFF32";
if($val< 2.4)
return "#FFFF46";
return "red";
}
并以这种方式在HTML中。它在循环1-15
$class=getBackground($val);
echo "<li style='background:{$class}'>{$val}%</li>";
但我不认为这是一个很好的解决方案,因为我必须为每个条件创建if else。
有更好的方法吗?如果有,那么请向我推荐。
更新
主要问题是:
如果颜色为0.2%颜色将为亮绿色,则颜色将如此表现;如果颜色为0.6%则颜色将为绿色,依此类推。如果它是0.9%,它将变为浅绿色。
我正在寻找一种解决方案,以显示不同的颜色,如果值为0.1然后是深绿色,如果它是0.1深绿色................. 0.9浅绿色。同样,如果val为1.1,则为深黄色。如果它是1.2深黄色........ 1.9浅黄色。像这样的东西
答案 0 :(得分:4)
这应该适合你:
只需将颜色放入具有特定范围的数组中即可。然后遍历所有颜色,如果$val
在范围内,则将颜色指定为$class
并打破循环。
<?php
$arr = [
"green" => ["start" => 0, "end" => 1],
"yellow" => ["start" => 1, "end" => 3],
"orange" => ["start" => 3, "end" => 5],
"red" => ["start" => 5, "end" => 6],
];
$class = "default";
foreach($arr as $color => $range) {
if($val <= $range["end"] && $val >= $range["start"]) {
$class = $color;
break;
}
}
?>
修改强>
从您更新的代码中,这应该适合您:
<?php
$arr = [
["start" => 0, "end" => 0.3, "color" => "#0A3A0A"],
["start" => 0.3, "end" => 0.5, "color" => "#1E4E1E"],
["start" => 0.5, "end" => 1, "color" => "#508050"],
["start" => 1, "end" => 1.4, "color" => "#FFFF0A"],
["start" => 1.4, "end" => 1.8, "color" => "#FFFF1E"],
["start" => 1.8, "end" => 2, "color" => "#FFFF32"],
["start" => 2, "end" => 2.4, "color" => "#FFFF46"],
];
$class = "red";
foreach($arr as $range) {
if($val <= $range["end"] && $val >= $range["start"]) {
$class = $range["color"];
break;
}
}
?>
编辑2:
由于您不需要“静态”值,因此这适用于您:
<?php
$arr = [
["start" => 0, "end" => 0.8, "colorStart" => "FFE6E6", "colorEnd" => "4C0000", "interval" => ""],
["start" => 0.8, "end" => 1.8, "colorStart" => "C2FFC2", "colorEnd" => "296629", "interval" => ""],
["start" => 1.8, "end" => 2.7, "colorStart" => "C2C2FF", "colorEnd" => "6666FF", "interval" => ""],
];
$arr = array_map(function($v){
$steps = intval((($v["end"] - $v["start"]) * 10));
list($redStart, $greenStart, $blueStart) = array_map("hexdec", str_split($v["colorStart"], 2));
list($redEnd, $greenEnd, $blueEnd) = array_map("hexdec", str_split($v["colorEnd"], 2));
$interval = sprintf("%02d", intval(($redStart - $redEnd) / $steps)) . sprintf("%02d", intval(($greenStart - $greenEnd) / $steps)) . sprintf("%02d", intval(($blueStart - $blueEnd) / $steps));
return array_merge($v, ["interval" => $interval]);
}, $arr);
$val = 1.1;
$class = "FF0000";
foreach($arr as $range) {
if($val < $range["end"] && $val >= $range["start"]) {
$class = "";
$multiplier = intval((($val - $range["start"]) * 10));
list($redAdd, $greenAdd, $blueAdd) = array_map(function($v)use($multiplier){return dechex($v*$multiplier);}, str_split($range["interval"], 2));
list($redStart, $greenStart, $blueStart) = str_split($range["colorStart"], 2);
list($redEnd, $greenEnd, $blueEnd) = str_split($range["colorEnd"], 2);
$colors = ["red", "green", "blue"];
foreach($colors as $color) {
if(hexdec(${$color . "End"}) > hexdec(${$color . "Start"}))
$r = hexdec(${$color . "Start"}) + hexdec(${$color. "Add"});
else
$r = hexdec(${$color . "Start"}) - hexdec(${$color. "Add"});
$class .= sprintf("%02X", ($r >= 0 && $r <= 255 ? $r : ($r<0?0:255)));
}
break;
}
}
echo "<div style='hight:100px;width:200px;background-color:#$class;'>some text</div>";
?>