ReportExtension

class ReportExtension extends Twig_Extension

ReportExtension.

Methods

public Twig_SimpleFilter[]getFilters() Returns the Twig functions of this extension.
public getName() {@inheritdoc}
public stringtextColorForBackground(Color|string $backgroundColor) Returns an appropiate foreground color based on the brightness.

Details

at line 28

getFilters()

public Twig_SimpleFilter[] getFilters()

Returns the Twig functions of this extension.

Return Value

Twig_SimpleFilter[]
at line 38

getName()

public getName()

{@inheritdoc}

at line 50

textColorForBackground()

public string textColorForBackground(Color|string $backgroundColor)

Returns an appropiate foreground color based on the brightness.

Parameters

Color|string$backgroundColorColor object or hex string. May include a leading '#'.

Return Value

string

Source code

<?php

/*
 * This file is part of nochso/benchmark.
 *
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 * @license   https://github.com/nochso/benchmark/blob/master/LICENSE ISC
 * @link      https://github.com/nochso/benchmark
 */

namespace nochso\Benchmark\Twig;

use nochso\Benchmark\Util\Color;

/**
 * ReportExtension.
 *
 * @author Marcel Voigt <mv@noch.so>
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 */
class ReportExtension extends \Twig_Extension
{
    /**
     * Returns the Twig functions of this extension.
     *
     * @return \Twig_SimpleFilter[]
     */
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('text_color', array($this, 'textColorForBackground')),
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'benchmark_report_extension';
    }

    /**
     * Returns an appropiate foreground color based on the brightness.
     *
     * @param Color|string $backgroundColor Color object or hex string. May include a leading '#'.
     *
     * @return string
     */
    public function textColorForBackground($backgroundColor)
    {
        if (!$backgroundColor instanceof Color) {
            $backgroundColor = Color::fromHex($backgroundColor);
        }
        if ($backgroundColor->getBrightness() < 0.6) {
            return new Color(255, 255, 255);
        }
        return new Color(0, 0, 0);
    }
}