-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColour.php
More file actions
55 lines (46 loc) · 1.11 KB
/
Colour.php
File metadata and controls
55 lines (46 loc) · 1.11 KB
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
<?php
namespace MatchBot\Domain;
use MatchBot\Application\Assertion;
use OpenApi\Attributes as OA;
/**
* Represents a 24 bit colour in the sRGB colour space.
*/
#[OA\Schema(
description: "Represents a 24-bit color in the sRGB color space",
type: "string",
format: "hex-color",
example: "#B30510"
)]
readonly class Colour
{
private string $hexCode;
private function __construct(
string $hexCode
) {
$this->hexCode = \strtoupper($hexCode);
Assertion::regex($this->hexCode, '/^#[A-F0-9]{6}$/', 'Hex color code required');
}
public static function fromHex(string $hexCode): self
{
return new self($hexCode);
}
public static function black(): Colour
{
return Colour::fromHex('#000000');
}
public static function white(): Colour
{
return Colour::fromHex('#FFFFFF');
}
/**
* @return string hex colour code prefixed with #, e.g. '#B30510'
*/
public function toHex(): string
{
return $this->hexCode;
}
public function __toString(): string
{
return $this->hexCode;
}
}