Add Railgun compatible Colour class

This commit is contained in:
flash 2017-12-16 22:44:45 +01:00
parent db12ccd25b
commit dd318c7dfa
2 changed files with 150 additions and 0 deletions

114
src/Colour.php Normal file
View file

@ -0,0 +1,114 @@
<?php
namespace Misuzu;
class Colour {
private const INHERIT = 0x40000000;
private $rawValue = 0;
public function __get(string $name) {
switch ($name) {
case 'raw':
return $this->rawValue;
case 'inherit':
return ($this->rawValue & self::INHERIT) > 0;
case 'red':
return $this->rawValue >> 16 & 0xFF;
case 'green':
return $this->rawValue >> 8 & 0xFF;
case 'blue':
return $this->rawValue & 0xFF;
case 'none':
return new static(self::INHERIT);
case 'hex':
return dechex_pad($this->red) . dechex_pad($this->green) . dechex_pad($this->blue);
}
return null;
}
public function __set(string $name, $value): void {
switch ($name) {
case 'raw':
if (!is_int32($value) && !is_uint32($value))
break;
$this->rawValue = $value;
break;
case 'inherit':
if (!is_bool($value))
break;
if ($value)
$this->rawValue |= self::INHERIT;
else
$this->rawValue &= ~self::INHERIT;
break;
case 'red':
if (!is_byte($value))
break;
$this->rawValue &= ~0xFF0000;
$this->rawValue |= $value << 16;
break;
case 'green':
if (!is_byte($value))
break;
$this->rawValue &= ~0xFF00;
$this->rawValue |= $value << 8;
break;
case 'blue':
if (!is_byte($value))
break;
$this->rawValue &= ~0xFF;
$this->rawValue |= $value;
break;
}
}
public function __construct(int $raw) {
$this->rawValue = $raw;
}
public static function fromRGB(int $red, int $green, int $blue): Colour {
$raw = 0;
$raw |= ($red & 0xFF) << 16;
$raw |= ($green & 0xFF) << 8;
$raw |= $blue & 0xFF;
return new static($raw);
}
public static function fromHex(string $hex): Colour {
$hex = ltrim(strtolower($hex), '#');
$hex_length = strlen($hex);
if ($hex_length == 3)
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
if ($hex_length != 6)
throw new \Exception('Invalid hex colour format! (find a more appropiate exception type)');
return static::fromRGB(
hexdec(substr($hex, 0, 2)),
hexdec(substr($hex, 2, 2)),
hexdec(substr($hex, 4, 2))
);
}
public function __toString() {
return "#{$this->hex}";
}
}

View file

@ -27,3 +27,39 @@ function array_rand_value(array $array, $fallback = null) {
function has_flag(int $flags, int $flag): bool {
return ($flags & $flag) > 0;
}
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 {
return is_int_ex($value, -0x80, 0x7F);
}
function is_byte($value): bool {
return is_int_ex($value, 0x0, 0xFF);
}
function is_int16($value): bool {
return is_int_ex($value, -0x8000, 0x7FFF);
}
function is_uint16($value): bool {
return is_int_ex($value, 0x0, 0xFFFF);
}
function is_int32($value): bool {
return is_int_ex($value, -0x80000000, 0x7FFFFFFF);
}
function is_uint32($value): bool {
return is_int_ex($value, 0x0, 0xFFFFFFFF);
}
function is_int64($value): bool {
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
}
function is_uint64($value): bool {
return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF);
}