Completed
Push — master ( 6ece6a...9c4912 )
by Marcus
02:53
created

ColorValue::getCompiled()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 7.551
c 0
b 0
f 0
cc 7
eloc 11
nc 4
nop 0
crap 7
1
<?php
2
namespace LesserPhp\Compiler\Value;
3
4
/**
5
 * lesserphp
6
 * https://www.maswaba.de/lesserphp
7
 *
8
 * LESS CSS compiler, adapted from http://lesscss.org
9
 *
10
 * Copyright 2013, Leaf Corcoran <[email protected]>
11
 * Copyright 2016, Marcus Schwarz <[email protected]>
12
 * Copyright 2017, Stefan Pöhner <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 *
15
 * @package LesserPhp
16
 */
17
18
class ColorValue extends AbstractValue
19
{
20
    /**
21
     * @var int
22
     */
23
    private $red;
24
25
    /**
26
     * @var int
27
     */
28
    private $green;
29
30
    /**
31
     * @var int
32
     */
33
    private $blue;
34
35
    /**
36
     * @var float
37
     */
38
    private $alpha;
39
40
    /**
41
     * @inheritdoc
42
     */
43 13
    public function getCompiled()
44
    {
45 13
        $red   = round($this->red);
46 13
        $green = round($this->green);
47 13
        $blue  = round($this->blue);
48
49 13
        if ($this->alpha !== null && $this->alpha != 1) {
50 4
            return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $this->alpha . ')';
51
        }
52
53 12
        $hex = sprintf("#%02x%02x%02x", $red, $green, $blue);
54
55 12
        if ($this->options['compressColors']) {
56
            // Converting hex color to short notation (e.g. #003399 to #039)
57 3
            if ($hex[1] === $hex[2] && $hex[3] === $hex[4] && $hex[5] === $hex[6]) {
58 3
                $hex = '#' . $hex[1] . $hex[3] . $hex[5];
59
            }
60
        }
61
62 12
        return $hex;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 13
    public function initializeFromOldFormat(array $value)
69
    {
70 13
        $this->red   = $value[1];
71 13
        $this->green = $value[2];
72 13
        $this->blue  = $value[3];
73
74 13
        if (isset($value[4])) {
75 6
            $this->alpha = $value[4];
76
        }
77 13
    }
78
}
79