swf2ass/src/Color.php

40 lines
1.4 KiB
PHP

<?php
namespace swf2ass;
class Color {
public $r;
public $g;
public $b;
public $alpha;
public function __construct($r, $g, $b, $alpha = 0) {
$this->r = $r;
$this->g = $g;
$this->b = $b;
$this->alpha = $alpha;
}
public function equals(?Color $other, $alpha = true): bool {
return $other !== null and $other->r === $this->r and $other->g === $this->g and $other->b === $this->b and (!$alpha or $other->alpha == $this->alpha);
}
public function distance(Color $color, $alpha = false): float {
return sqrt(pow($color->r - $this->r, 2) + pow($color->g - $this->g, 2) + pow($color->b - $this->b, 2) + ($alpha ? pow($color->alpha - $this->alpha, 2) : 0));
}
public function toString($nullAlpha = false) {
$c = "\\1c&H" . strtoupper(Utils::padHex(dechex($this->b ?? 0))) . strtoupper(Utils::padHex(dechex($this->g ?? 0))) . strtoupper(Utils::padHex(dechex($this->r ?? 0))) . "&";
if (($this->alpha === 0) and $nullAlpha) {
} else {
$c .= "\\1a&H" . strtoupper(Utils::padHex(dechex($this->alpha ?? 0))) . "&";
}
return $c;
}
public static function fromXML(\DOMElement $element): Color {
return new Color((int)$element->getAttribute("red"), (int)$element->getAttribute("green"), (int)$element->getAttribute("blue"), $element->hasAttribute("alpha") ? (int)$element->getAttribute("alpha") : 0);
}
}