Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Chart;
4
 
5
class ChartColor
6
{
7
    const EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
8
    const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
9
    const EXCEL_COLOR_TYPE_RGB = 'srgbClr';
10
    const EXCEL_COLOR_TYPES = [
11
        self::EXCEL_COLOR_TYPE_RGB,
12
        self::EXCEL_COLOR_TYPE_SCHEME,
13
        self::EXCEL_COLOR_TYPE_STANDARD,
14
    ];
15
 
16
    private string $value = '';
17
 
18
    private string $type = '';
19
 
20
    private ?int $alpha = null;
21
 
22
    private ?int $brightness = null;
23
 
24
    /**
25
     * @param string|string[] $value
26
     */
27
    public function __construct($value = '', ?int $alpha = null, ?string $type = null, ?int $brightness = null)
28
    {
29
        if (is_array($value)) {
30
            $this->setColorPropertiesArray($value);
31
        } else {
32
            $this->setColorProperties($value, $alpha, $type, $brightness);
33
        }
34
    }
35
 
36
    public function getValue(): string
37
    {
38
        return $this->value;
39
    }
40
 
41
    public function setValue(string $value): self
42
    {
43
        $this->value = $value;
44
 
45
        return $this;
46
    }
47
 
48
    public function getType(): string
49
    {
50
        return $this->type;
51
    }
52
 
53
    public function setType(string $type): self
54
    {
55
        $this->type = $type;
56
 
57
        return $this;
58
    }
59
 
60
    public function getAlpha(): ?int
61
    {
62
        return $this->alpha;
63
    }
64
 
65
    public function setAlpha(?int $alpha): self
66
    {
67
        $this->alpha = $alpha;
68
 
69
        return $this;
70
    }
71
 
72
    public function getBrightness(): ?int
73
    {
74
        return $this->brightness;
75
    }
76
 
77
    public function setBrightness(?int $brightness): self
78
    {
79
        $this->brightness = $brightness;
80
 
81
        return $this;
82
    }
83
 
84
    public function setColorProperties(?string $color, null|float|int|string $alpha = null, ?string $type = null, null|float|int|string $brightness = null): self
85
    {
86
        if (empty($type) && !empty($color)) {
87
            if (str_starts_with($color, '*')) {
88
                $type = 'schemeClr';
89
                $color = substr($color, 1);
90
            } elseif (str_starts_with($color, '/')) {
91
                $type = 'prstClr';
92
                $color = substr($color, 1);
93
            } elseif (preg_match('/^[0-9A-Fa-f]{6}$/', $color) === 1) {
94
                $type = 'srgbClr';
95
            }
96
        }
97
        if ($color !== null) {
98
            $this->setValue("$color");
99
        }
100
        if ($type !== null) {
101
            $this->setType($type);
102
        }
103
        if ($alpha === null) {
104
            $this->setAlpha(null);
105
        } elseif (is_numeric($alpha)) {
106
            $this->setAlpha((int) $alpha);
107
        }
108
        if ($brightness === null) {
109
            $this->setBrightness(null);
110
        } elseif (is_numeric($brightness)) {
111
            $this->setBrightness((int) $brightness);
112
        }
113
 
114
        return $this;
115
    }
116
 
117
    public function setColorPropertiesArray(array $color): self
118
    {
119
        return $this->setColorProperties(
120
            $color['value'] ?? '',
121
            $color['alpha'] ?? null,
122
            $color['type'] ?? null,
123
            $color['brightness'] ?? null
124
        );
125
    }
126
 
127
    public function isUsable(): bool
128
    {
129
        return $this->type !== '' && $this->value !== '';
130
    }
131
 
132
    /**
133
     * Get Color Property.
134
     */
135
    public function getColorProperty(string $propertyName): null|int|string
136
    {
137
        $retVal = null;
138
        if ($propertyName === 'value') {
139
            $retVal = $this->value;
140
        } elseif ($propertyName === 'type') {
141
            $retVal = $this->type;
142
        } elseif ($propertyName === 'alpha') {
143
            $retVal = $this->alpha;
144
        } elseif ($propertyName === 'brightness') {
145
            $retVal = $this->brightness;
146
        }
147
 
148
        return $retVal;
149
    }
150
 
151
    public static function alphaToXml(int $alpha): string
152
    {
153
        return (string) (100 - $alpha) . '000';
154
    }
155
 
156
    public static function alphaFromXml(float|int|string $alpha): int
157
    {
158
        return 100 - ((int) $alpha / 1000);
159
    }
160
}