58 lines
1.1 KiB
PHP
58 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace ManiaControl;
|
||
|
|
||
|
/**
|
||
|
* Utitlity class offering methods to convert and use ManiaPlanet colors
|
||
|
*
|
||
|
* @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.;
|
||
|
$value = round($value);
|
||
|
if ($value < 10) {
|
||
|
return (string) $value;
|
||
|
}
|
||
|
$codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f');
|
||
|
return $codes[$value];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|