summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Less/ColorProp.php
blob: 3f83c5eee77acd94ebc7bd47bf6ab1c5996def94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */

namespace Icinga\Less;

use Less_Tree_Call;
use Less_Tree_Color;
use Less_Tree_Keyword;

/**
 * ColorProp renders Less colors as CSS var() function calls
 *
 * It extends {@link Less_Tree_Color} so that Less functions that take a Less_Tree_Color as an argument do not fail.
 */
class ColorProp extends Less_Tree_Color
{
    /** @var Less_Tree_Color Color with which we created the ColorProp */
    protected $color;

    /** @var int */
    protected $index;

    /** @var string Color variable name */
    protected $name;

    public function __construct()
    {
    }

    /**
     * @param Less_Tree_Color $color
     *
     * @return static
     */
    public static function fromColor(Less_Tree_Color $color)
    {
        $self = new static();
        $self->color = $color;

        foreach ($color as $k => $v) {
            if ($k === 'name') {
                $self->setName($v); // Removes the @ char from the name
            } else {
                $self->$k = $v;
            }
        }

        return $self;
    }

    /**
     * @return int
     */
    public function getIndex()
    {
        return $this->index;
    }

    /**
     * @param int $index
     *
     * @return $this
     */
    public function setIndex($index)
    {
        $this->index = $index;

        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        if ($name[0] === '@') {
            $name = substr($name, 1);
        }

        $this->name = $name;

        return $this;
    }

    public function genCSS($output)
    {
        $css = (new Less_Tree_Call(
            'var',
            [
                new Less_Tree_Keyword('--' . $this->getName()),
                // Use the Less_Tree_Color with which we created the ColorProp so that we don't get into genCSS() loops.
                $this->color
            ],
            $this->getIndex()
        ))->toCSS();

        $output->add($css);
    }
}