diff --git a/src/Colour.php b/src/Colour.php index 3116fc32..dfaa25eb 100644 --- a/src/Colour.php +++ b/src/Colour.php @@ -1,12 +1,14 @@ rawValue; @@ -30,44 +32,51 @@ class Colour { return null; } - public function __set(string $name, $value): void { + public function __set(string $name, $value): void + { switch ($name) { case 'raw': - if (!is_int32($value) && !is_uint32($value)) + if (!is_int32($value) && !is_uint32($value)) { break; + } $this->rawValue = $value; break; case 'inherit': - if (!is_bool($value)) + if (!is_bool($value)) { break; + } - if ($value) + if ($value) { $this->rawValue |= self::INHERIT; - else + } else { $this->rawValue &= ~self::INHERIT; + } break; case 'red': - if (!is_byte($value)) + if (!is_byte($value)) { break; + } $this->rawValue &= ~0xFF0000; $this->rawValue |= $value << 16; break; case 'green': - if (!is_byte($value)) + if (!is_byte($value)) { break; + } $this->rawValue &= ~0xFF00; $this->rawValue |= $value << 8; break; case 'blue': - if (!is_byte($value)) + if (!is_byte($value)) { break; + } $this->rawValue &= ~0xFF; $this->rawValue |= $value; @@ -76,11 +85,13 @@ class Colour { } } - public function __construct(int $raw) { + public function __construct(int $raw) + { $this->rawValue = $raw; } - public static function fromRGB(int $red, int $green, int $blue): Colour { + public static function fromRGB(int $red, int $green, int $blue): Colour + { $raw = 0; $raw |= ($red & 0xFF) << 16; $raw |= ($green & 0xFF) << 8; @@ -88,14 +99,16 @@ class Colour { return new static($raw); } - public static function fromHex(string $hex): Colour { + public static function fromHex(string $hex): Colour + { $hex = ltrim(strtolower($hex), '#'); $hex_length = strlen($hex); - if ($hex_length === 3) + if ($hex_length === 3) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; - elseif ($hex_length != 6) + } elseif ($hex_length != 6) { throw new \Exception('Invalid hex colour format! (find a more appropiate exception type)'); + } return static::fromRGB( hexdec(substr($hex, 0, 2)), @@ -104,11 +117,13 @@ class Colour { ); } - public static function none(): Colour { + public static function none(): Colour + { return new static(static::INHERIT); } - public function __toString() { + public function __toString() + { return "#{$this->hex}"; } } diff --git a/src/Zalgo.php b/src/Zalgo.php index 6c5408b5..d22cb497 100644 --- a/src/Zalgo.php +++ b/src/Zalgo.php @@ -1,7 +1,8 @@ assertTrue($colour->inherit); @@ -29,7 +31,8 @@ class ColourTest extends TestCase { $this->assertEquals($colour->hex, '000000'); } - public function testFromRaw() { + public function testFromRaw() + { $colour = new Colour(static::RAW_HEX6); $this->assertEquals($colour->hex, static::STR_HEX6); @@ -40,7 +43,8 @@ class ColourTest extends TestCase { $this->assertFalse($colour->inherit); } - public function testFromRGB() { + public function testFromRGB() + { $colour = Colour::fromRGB(static::RED_HEX6, static::GREEN_HEX6, static::BLUE_HEX6); $this->assertEquals($colour->hex, static::STR_HEX6); @@ -51,7 +55,8 @@ class ColourTest extends TestCase { $this->assertFalse($colour->inherit); } - public function testFromHex() { + public function testFromHex() + { $colour = Colour::fromHex(static::SSTR_HEX6); $this->assertEquals($colour->hex, static::STR_HEX6); @@ -62,7 +67,8 @@ class ColourTest extends TestCase { $this->assertFalse($colour->inherit); } - public function testFromHex3() { + public function testFromHex3() + { $colour = Colour::fromHex(static::SSTR_HEX3); $this->assertEquals($colour->hex, static::STR_HEX3); diff --git a/utility.php b/utility.php index 4fc66826..b9182b86 100644 --- a/utility.php +++ b/utility.php @@ -2,64 +2,79 @@ // both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless if (!function_exists('starts_with')) { - function starts_with(string $string, string $text): bool { + function starts_with(string $string, string $text): bool + { return substr($string, 0, strlen($text)) === $text; } } if (!function_exists('ends_with')) { - function ends_with(string $string, string $text): bool { + function ends_with(string $string, string $text): bool + { return substr($string, 0 - strlen($text)) === $text; } } -function dechex_pad(int $value, int $padding = 2): string { +function dechex_pad(int $value, int $padding = 2): string +{ return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT); } -function array_rand_value(array $array, $fallback = null) { - if (!$array) +function array_rand_value(array $array, $fallback = null) +{ + if (!$array) { return $fallback; + } return $array[array_rand($array)]; } -function has_flag(int $flags, int $flag): bool { +function has_flag(int $flags, int $flag): bool +{ return ($flags & $flag) > 0; } -function is_int_ex($value, int $boundary_low, int $boundary_high): bool { +function is_int_ex($value, int $boundary_low, int $boundary_high): bool +{ return is_int($value) && $value >= $boundary_low && $value <= $boundary_high; } -function is_sbyte($value): bool { +function is_sbyte($value): bool +{ return is_int_ex($value, -0x80, 0x7F); } -function is_byte($value): bool { +function is_byte($value): bool +{ return is_int_ex($value, 0x0, 0xFF); } -function is_int16($value): bool { +function is_int16($value): bool +{ return is_int_ex($value, -0x8000, 0x7FFF); } -function is_uint16($value): bool { +function is_uint16($value): bool +{ return is_int_ex($value, 0x0, 0xFFFF); } -function is_int32($value): bool { +function is_int32($value): bool +{ return is_int_ex($value, -0x80000000, 0x7FFFFFFF); } -function is_uint32($value): bool { +function is_uint32($value): bool +{ return is_int_ex($value, 0x0, 0xFFFFFFFF); } -function is_int64($value): bool { +function is_int64($value): bool +{ return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF); } -function is_uint64($value): bool { +function is_uint64($value): bool +{ return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF); }