Bedingte Formatierung mit PHP

Dieser Beitrag stellt die Klasse ColorProvider vor, welche die Funktionalität der "Bedingten Formatierung", bekannt aus Excel 2007, nachbildet.

Dabei wird ein Wertebereich einem Farbverlauf gegenübergestellt, so dass man schon aufgrund der Hintergrundfarbe auf den Wert der Zelle schließen kann. Das erleichtert die Übersichtlichkeit einer Tabelle enorm.

Hier der Quelltext als "Out of the box" Beispiel:

<?php 
 
class ColorProvider {

    //Konstruktorparameter
    private $startColor;
    private $endColor;
    private $minWert;
    private $maxWert;
    //Optionale Parameter
    private $increment = 0.1;
    private $outOfRangeColorMin = "#ffffff";
    private $outOfRangeColorMax = "#000000";
    //Hilfsvariablen
    private $steps;
    private $preR;
    private $preG;
    private $preB;
    private $postR;
    private $postG;
    private $postB;
    private $deltaR;
    private $deltaG;
    private $deltaB;

    /**
     * Ermöglicht eine "Bedingte Formatierung" Wie aus Excel 2007 bekannt.
     * @access public
     * @param $startColor String Anfangsfarbe im Farbverlauf.
     * @param $endColor String Endfarbe im Farbverlauf.
     * @param $minWert Double Kleinster Wert im Wertebereich.
     * @param $maxWert Double Größter Wert im Wertebereich.
     */
    function __construct($startColor, $endColor, $minWert, $maxWert) {

        $this->startColor = str_replace("#", "", $startColor);
        $this->endColor = str_replace("#", "", $endColor);
        $this->minWert = $minWert;
        $this->maxWert = $maxWert;

        $this->steps = (($this->maxWert - $this->minWert) / $this->increment);

        $this->preR = hexdec(substr($this->startColor, 0, 2));
        $this->preG = hexdec(substr($this->startColor, 2, 2));
        $this->preB = hexdec(substr($this->startColor, 4, 2));

        $this->postR = hexdec(substr($this->endColor, 0, 2));
        $this->postG = hexdec(substr($this->endColor, 2, 2));
        $this->postB = hexdec(substr($this->endColor, 4, 2));

        $this->deltaR = $this->postR - $this->preR;
        $this->deltaG = $this->postG - $this->preG;
        $this->deltaB = $this->postB - $this->preB;
    }

    /**
     * Stellt die Schrittweite eines Farbsprungs ein. Default auf 0.1
     * @access public
     * @param $increment Double Schrittweite einer Farbänderung.
     */
    public function setIncrement($increment) {
        $this->increment = $increment;
    }

    /**
     * Stellt die Farbe ein, die zurueckgegeben werden soll wenn 
     * der Wertebereich nach unten ueberschritten wird.
     * @access public
     * @param $outOfRangeColorMin HexColor Farbwert in Hex-Schreibweise. Default '#ffffff'
     */
    public function setOutOfRangeColorMin($outOfRangeColorMin) {
        $this->outOfRangeColorMin = $outOfRangeColorMin;
    }

    /**
     * Stellt die Farbe ein, die zurueckgegeben werden soll wenn 
     * der Wertebereich nach oben ueberschritten wird.
     * @access public
     * @param $outOfRangeColorMax HexColor Farbwert in Hex-Schreibweise. Default '#ffffff'
     */
    public function setOutOfRangeColorMax($outOfRangeColorMax) {
        $this->outOfRangeColorMax = $outOfRangeColorMax;
    }

    /**
     * Gibt eine Hexfarbe für für den übergebenen Wert zurück
     * @access public
     * @param $wert Double Wert der in eine Farbe umgewandelt werden soll. 
     */
    public function getColor($wert) {
        if ($wert < $this->minWert) {
            return $this->outOfRangeColorMin;
        }
        if ($wert > $this->maxWert) {
            return $this->outOfRangeColorMax;
        }
        $stand = (($wert - $this->minWert) / $this->increment);
        $factor = $stand / $this->steps;
        $r = round($this->preR + $this->deltaR * $factor);
        $g = round($this->preG + $this->deltaG * $factor);
        $b = round($this->preB + $this->deltaB * $factor);
        $color = "#" . sprintf("%02X", $r) . sprintf("%02X", $g) . sprintf("%02X", $b);
        return $color;
    }

}

// Testbereich Aufruf
$CP = new ColorProvider("#70ef69", "#f97042", 0.0, 30.0);
echo "<table><tr>";
for ($i = 1; $i <= 300; $i++) {
    echo "<td style='background-color:" . $CP->getColor($i / 10) . ";'>" . ($i / 10) . "</td>";
    if ($i % 20 == 0) {
        echo "</tr><tr>";
    }
}
echo "</tr></table>";
?>

 

Hier das Ergebnis:

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6
6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8
8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10
10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 11 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 12
12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 13 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8 13.9 14
14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9 15 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 16
16.1 16.2 16.3 16.4 16.5 16.6 16.7 16.8 16.9 17 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 18
18.1 18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 19 19.1 19.2 19.3 19.4 19.5 19.6 19.7 19.8 19.9 20
20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 22
22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24
24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.9 25 25.1 25.2 25.3 25.4 25.5 25.6 25.7 25.8 25.9 26
26.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27 27.1 27.2 27.3 27.4 27.5 27.6 27.7 27.8 27.9 28
28.1 28.2 28.3 28.4 28.5 28.6 28.7 28.8 28.9 29 29.1 29.2 29.3 29.4 29.5 29.6 29.7 29.8 29.9 30

Aktualisiert (Montag, den 31. Oktober 2011 um 19:30 Uhr)