28 lines
723 B
PHP
28 lines
723 B
PHP
<?php
|
|
namespace YTKNS;
|
|
|
|
final class Colour {
|
|
private int $raw = 0;
|
|
|
|
public function __construct(int $raw = 0) {
|
|
$this->setRaw($raw);
|
|
}
|
|
|
|
public static function create($value): ?Colour {
|
|
$colour = is_int($value) ? $value : (is_string($value) && ctype_digit($value) ? (int)$value : -1);
|
|
return $colour < 0 || $colour > 0xFFFFFF ? null : new static($colour);
|
|
}
|
|
|
|
public function getRaw(): int {
|
|
return $this->raw;
|
|
}
|
|
public function setRaw(int $raw): self {
|
|
$this->raw = $raw & 0xFFFFFF;
|
|
return $this;
|
|
}
|
|
|
|
public function getHex(): string {
|
|
return str_pad(dechex($this->raw), 6, '0', STR_PAD_LEFT);
|
|
}
|
|
}
|