2013-11-28 01:12:52 +01:00
|
|
|
<?php
|
|
|
|
|
2014-05-13 14:32:30 +02:00
|
|
|
namespace ManiaControl\Utils;
|
2013-11-28 01:12:52 +01:00
|
|
|
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
2014-04-12 12:14:37 +02:00
|
|
|
* Utility Class offering Methods to convert and use ManiaPlanet Colors
|
2013-11-28 01:12:52 +01:00
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
abstract class ColorUtil implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
2013-11-28 01:12:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given float value to a color code from red to green
|
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @param float $value
|
2013-11-28 01:12:52 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-04-24 18:52:18 +02:00
|
|
|
public static function floatToStatusColor($value, $addBlue = true) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$value = floatval($value);
|
2014-05-02 17:40:47 +02:00
|
|
|
$red = 1.;
|
2013-11-28 01:12:52 +01:00
|
|
|
$green = 1.;
|
|
|
|
if ($value < 0.5) {
|
|
|
|
$green = $value * 2.;
|
|
|
|
}
|
|
|
|
if ($value > 0.5) {
|
|
|
|
$red = 2. * (1. - $value);
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
$red = ColorUtil::floatToCode($red);
|
2013-11-28 01:12:52 +01:00
|
|
|
$green = ColorUtil::floatToCode($green);
|
2020-04-24 18:52:18 +02:00
|
|
|
|
|
|
|
if ($addBlue) {
|
|
|
|
return $red . $green . '0';
|
|
|
|
} else {
|
|
|
|
return $red . $green;
|
|
|
|
}
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get hex color representation of the float
|
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @param float $value
|
2013-11-28 01:12:52 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function floatToCode($value) {
|
|
|
|
$value = floatval($value);
|
|
|
|
if ($value < 0.) {
|
|
|
|
$value = 0.;
|
|
|
|
}
|
|
|
|
if ($value > 1.) {
|
|
|
|
$value = 1.;
|
|
|
|
}
|
|
|
|
$value *= 15.;
|
2017-03-26 19:44:55 +02:00
|
|
|
$value = (int) round($value);
|
2013-11-28 01:12:52 +01:00
|
|
|
if ($value < 10) {
|
2017-03-26 19:44:55 +02:00
|
|
|
return (string) $value;
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
2020-04-24 18:52:18 +02:00
|
|
|
static $codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f');
|
2013-11-28 01:12:52 +01:00
|
|
|
return $codes[$value];
|
|
|
|
}
|
|
|
|
}
|