2013-11-28 01:12:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
|
|
|
/**
|
2013-12-09 13:45:58 +01:00
|
|
|
* Utility class offering methods to convert and use ManiaPlanet colors
|
2013-11-28 01:12:52 +01:00
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
|
|
|
abstract class ColorUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given float value to a color code from red to green
|
|
|
|
*
|
|
|
|
* @param float $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function floatToStatusColor($value) {
|
|
|
|
$value = floatval($value);
|
|
|
|
$red = 1.;
|
|
|
|
$green = 1.;
|
|
|
|
if ($value < 0.5) {
|
|
|
|
$green = $value * 2.;
|
|
|
|
}
|
|
|
|
if ($value > 0.5) {
|
|
|
|
$red = 2. * (1. - $value);
|
|
|
|
}
|
|
|
|
$red = ColorUtil::floatToCode($red);
|
|
|
|
$green = ColorUtil::floatToCode($green);
|
|
|
|
return $red . $green . '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get hex color representation of the float
|
|
|
|
*
|
|
|
|
* @param float $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function floatToCode($value) {
|
|
|
|
$value = floatval($value);
|
|
|
|
if ($value < 0.) {
|
|
|
|
$value = 0.;
|
|
|
|
}
|
|
|
|
if ($value > 1.) {
|
|
|
|
$value = 1.;
|
|
|
|
}
|
|
|
|
$value *= 15.;
|
2013-12-09 13:45:58 +01:00
|
|
|
$value = (int) round($value);
|
2013-11-28 01:12:52 +01:00
|
|
|
if ($value < 10) {
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
$codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f');
|
|
|
|
return $codes[$value];
|
|
|
|
}
|
|
|
|
}
|