Passed
Pull Request — master (#5)
by
unknown
02:54
created

ColorValue::initializeFromOldFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
    private $red;
21
    private $green;
22
    private $blue;
23
    private $alpha;
24
25
    /**
26
     * @inheritdoc
27
     */
28 13
    public function getCompiled()
29
    {
30 13
        $red   = round($this->red);
31 13
        $green = round($this->green);
32 13
        $blue  = round($this->blue);
33
34 13
        if ($this->alpha !== null && $this->alpha != 1) {
35 4
            return 'rgba('.$red.','.$green.','.$blue.','.$this->alpha.')';
36
        }
37
38 12
        $hex = sprintf("#%02x%02x%02x", $red, $green, $blue);
39
40 12
        if ($this->options['compressColors']) {
41
            // Converting hex color to short notation (e.g. #003399 to #039)
42 3
            if ($hex[1] === $hex[2] && $hex[3] === $hex[4] && $hex[5] === $hex[6]) {
43 3
                $hex = '#'.$hex[1].$hex[3].$hex[5];
44
            }
45
        }
46
47 12
        return $hex;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 13
    public function initializeFromOldFormat(array $value)
54
    {
55 13
        $this->red   = $value[1];
56 13
        $this->green = $value[2];
57 13
        $this->blue  = $value[3];
58
59 13
        if (isset($value[4])) {
60 6
            $this->alpha = $value[4];
61
        }
62
    }
63
}