Skip to content

Commit 70c7e1f

Browse files
authored
Use ColourUtil
1 parent 3344690 commit 70c7e1f

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

src/CliMenuBuilder.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpSchool\CliMenu\MenuItem\SelectableItem;
1212
use PhpSchool\CliMenu\MenuItem\StaticItem;
1313
use PhpSchool\CliMenu\Terminal\TerminalFactory;
14+
use PhpSchool\CliMenu\Util\ColourUtil;
1415
use Assert\Assertion;
1516
use PhpSchool\Terminal\Terminal;
1617
use RuntimeException;
@@ -200,36 +201,22 @@ public function setExitButtonText(string $exitButtonText) : self
200201

201202
public function setBackgroundColour($colour, string $fallback = null) : self
202203
{
203-
if (is_int($colour)) {
204-
if ($this->terminal->getColourSupport() < 256) {
205-
$colour = $fallback;
206-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
207-
} elseif ($colour < 0 || $colour > 255) {
208-
throw new \InvalidArgumentException("Invalid colour code");
209-
}
210-
} else {
211-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
212-
}
213-
214-
$this->style['bg'] = $colour;
204+
$this->style['bg'] = ColourUtil::validateColour(
205+
$this->terminal,
206+
$colour,
207+
$fallback
208+
);
215209

216210
return $this;
217211
}
218212

219213
public function setForegroundColour($colour, string $fallback = null) : self
220214
{
221-
if (is_int($colour)) {
222-
if ($this->terminal->getColourSupport() < 256) {
223-
$colour = $fallback;
224-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
225-
} elseif ($colour < 0 || $colour > 255) {
226-
throw new \InvalidArgumentException("Invalid colour code");
227-
}
228-
} else {
229-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
230-
}
231-
232-
$this->style['fg'] = $colour;
215+
$this->style['fg'] = ColourUtil::validateColour(
216+
$this->terminal,
217+
$colour,
218+
$fallback
219+
);
233220

234221
return $this;
235222
}

0 commit comments

Comments
 (0)