Skip to content

Commit 5fabca1

Browse files
authored
Use ColourUtil and remove getAvailableColours
1 parent 70c7e1f commit 5fabca1

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/MenuStyle.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
66
use PhpSchool\CliMenu\Terminal\TerminalFactory;
7+
use PhpSchool\CliMenu\Util\ColourUtil;
78
use PhpSchool\Terminal\Terminal;
89

910
//TODO: B/W fallback
@@ -177,11 +178,6 @@ public function __construct(Terminal $terminal = null)
177178
$this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']);
178179
}
179180

180-
public static function getAvailableColours() : array
181-
{
182-
return array_keys(self::$availableBackgroundColors);
183-
}
184-
185181
public function getDisabledItemText(string $text) : string
186182
{
187183
return sprintf(
@@ -259,15 +255,11 @@ public function getFg()
259255

260256
public function setFg($fg, string $fallback = null) : self
261257
{
262-
if (is_int($fg)) {
263-
if ($this->terminal->getColourSupport() < 256) {
264-
$fg = $fallback;
265-
} elseif ($fg < 0 || $fg > 255) {
266-
throw new \InvalidArgumentException("Invalid colour code");
267-
}
268-
}
269-
270-
$this->fg = $fg;
258+
$this->fg = ColourUtil::validateColour(
259+
$this->terminal,
260+
$colour,
261+
$fallback
262+
);
271263
$this->generateColoursSetCode();
272264

273265
return $this;
@@ -280,14 +272,11 @@ public function getBg()
280272

281273
public function setBg($bg, string $fallback = null) : self
282274
{
283-
if (is_int($bg)) {
284-
if ($this->terminal->getColourSupport() < 256) {
285-
$bg = $fallback;
286-
} elseif ($bg < 0 || $bg > 255) {
287-
throw new \InvalidArgumentException("Invalid colour code");
288-
}
289-
}
290-
$this->bg = $bg;
275+
$this->bg = ColourUtil::validateColour(
276+
$this->terminal,
277+
$colour,
278+
$fallback
279+
);
291280
$this->generateColoursSetCode();
292281

293282
return $this;

0 commit comments

Comments
 (0)