chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
 
namespace PhpOffice\PhpSpreadsheet\Chart;
 
class ChartColor
{
    const EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
    const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
    const EXCEL_COLOR_TYPE_RGB = 'srgbClr';
    const EXCEL_COLOR_TYPES = [
        self::EXCEL_COLOR_TYPE_RGB,
        self::EXCEL_COLOR_TYPE_SCHEME,
        self::EXCEL_COLOR_TYPE_STANDARD,
    ];
 
    private string $value = '';
 
    private string $type = '';
 
    private ?int $alpha = null;
 
    private ?int $brightness = null;
 
    /**
     * @param string|string[] $value
     */
    public function __construct($value = '', ?int $alpha = null, ?string $type = null, ?int $brightness = null)
    {
        if (is_array($value)) {
            $this->setColorPropertiesArray($value);
        } else {
            $this->setColorProperties($value, $alpha, $type, $brightness);
        }
    }
 
    public function getValue(): string
    {
        return $this->value;
    }
 
    public function setValue(string $value): self
    {
        $this->value = $value;
 
        return $this;
    }
 
    public function getType(): string
    {
        return $this->type;
    }
 
    public function setType(string $type): self
    {
        $this->type = $type;
 
        return $this;
    }
 
    public function getAlpha(): ?int
    {
        return $this->alpha;
    }
 
    public function setAlpha(?int $alpha): self
    {
        $this->alpha = $alpha;
 
        return $this;
    }
 
    public function getBrightness(): ?int
    {
        return $this->brightness;
    }
 
    public function setBrightness(?int $brightness): self
    {
        $this->brightness = $brightness;
 
        return $this;
    }
 
    public function setColorProperties(?string $color, null|float|int|string $alpha = null, ?string $type = null, null|float|int|string $brightness = null): self
    {
        if (empty($type) && !empty($color)) {
            if (str_starts_with($color, '*')) {
                $type = 'schemeClr';
                $color = substr($color, 1);
            } elseif (str_starts_with($color, '/')) {
                $type = 'prstClr';
                $color = substr($color, 1);
            } elseif (preg_match('/^[0-9A-Fa-f]{6}$/', $color) === 1) {
                $type = 'srgbClr';
            }
        }
        if ($color !== null) {
            $this->setValue("$color");
        }
        if ($type !== null) {
            $this->setType($type);
        }
        if ($alpha === null) {
            $this->setAlpha(null);
        } elseif (is_numeric($alpha)) {
            $this->setAlpha((int) $alpha);
        }
        if ($brightness === null) {
            $this->setBrightness(null);
        } elseif (is_numeric($brightness)) {
            $this->setBrightness((int) $brightness);
        }
 
        return $this;
    }
 
    public function setColorPropertiesArray(array $color): self
    {
        return $this->setColorProperties(
            $color['value'] ?? '',
            $color['alpha'] ?? null,
            $color['type'] ?? null,
            $color['brightness'] ?? null
        );
    }
 
    public function isUsable(): bool
    {
        return $this->type !== '' && $this->value !== '';
    }
 
    /**
     * Get Color Property.
     */
    public function getColorProperty(string $propertyName): null|int|string
    {
        $retVal = null;
        if ($propertyName === 'value') {
            $retVal = $this->value;
        } elseif ($propertyName === 'type') {
            $retVal = $this->type;
        } elseif ($propertyName === 'alpha') {
            $retVal = $this->alpha;
        } elseif ($propertyName === 'brightness') {
            $retVal = $this->brightness;
        }
 
        return $retVal;
    }
 
    public static function alphaToXml(int $alpha): string
    {
        return (string) (100 - $alpha) . '000';
    }
 
    public static function alphaFromXml(float|int|string $alpha): int
    {
        return 100 - ((int) $alpha / 1000);
    }
}