70 lines
3.2 KiB
PHP
70 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace swf2ass;
|
|
|
|
|
|
class ColorTransform {
|
|
public Color $mult;
|
|
public Color $add;
|
|
|
|
public function __construct(Color $mult, Color $add) {
|
|
$this->mult = $mult;
|
|
$this->add = $add;
|
|
}
|
|
|
|
public static function identity(): ColorTransform {
|
|
return new ColorTransform(new Color(256, 256, 256, 256), new Color(0, 0, 0, 0));
|
|
}
|
|
|
|
public function applyToStyleRecord(StyleRecord $record): StyleRecord {
|
|
if($record instanceof LineStyleRecord){
|
|
return new LineStyleRecord($record->width, $this->applyToColor($record->color));
|
|
}else if ($record instanceof FillStyleRecord){
|
|
if($record->fill instanceof Color){
|
|
return new FillStyleRecord($this->applyToColor($record->fill));
|
|
}elseif ($record->fill instanceof Gradient){
|
|
return new FillStyleRecord($record->fill->applyColorTransform($this));
|
|
}
|
|
}
|
|
throw new \Exception();
|
|
}
|
|
|
|
public function applyMultiplyToColor(Color $color): Color {
|
|
return new Color(max(0, min(($this->mult->r * $color->r) / 256, 255)), max(0, min(($this->mult->g * $color->g) / 256, 255)), max(0, min(($this->mult->b * $color->b) / 256, 255)), max(0, min(($this->mult->alpha * $color->alpha) / 256, 255)),);
|
|
}
|
|
|
|
public function applyAdditionToColor(Color $color): Color {
|
|
return new Color(max(0, min($this->add->r + $color->r, 255)), max(0, min($this->add->g + $color->g, 255)), max(0, min($this->add->b + $color->b, 255)), max(0, min($this->add->alpha + $color->alpha, 255)),);
|
|
}
|
|
|
|
public function applyToColor(Color $color): Color {
|
|
return new Color(max(0, min((($this->mult->r * $color->r) / 256) + $this->add->r, 255)), max(0, min((($this->mult->g * $color->g) / 256) + $this->add->g, 255)), max(0, min((($this->mult->b * $color->b) / 256) + $this->add->b, 255)), max(0, min((($this->mult->alpha * $color->alpha) / 256) + $this->add->alpha, 255)),);
|
|
}
|
|
|
|
public function combine(ColorTransform $transform) {
|
|
//TODO: maybe these get altered all at once?
|
|
return new ColorTransform($this->applyMultiplyToColor($transform->mult), $this->applyAdditionToColor($this->applyMultiplyToColor($transform->add)));
|
|
}
|
|
|
|
public function equals(ColorTransform $other, $epsilon = Constants::EPSILON): bool {
|
|
return ($this->mult === $other->mult or ($this->mult !== null and $this->mult->equals($other->mult))) and ($this->add === $other->add or ($this->add !== null and $this->add->equals($other->add)));
|
|
}
|
|
|
|
public static function fromArray(array $element): ColorTransform {
|
|
$add = new Color(0, 0, 0, 0);
|
|
$mult = new Color(256, 256, 256, 256);
|
|
if (isset($element["redMultTerm"])) {
|
|
$mult->r = $element["redMultTerm"];
|
|
$mult->g = $element["greenMultTerm"];
|
|
$mult->b = $element["blueMultTerm"];
|
|
$mult->alpha = $element["alphaMultTerm"];
|
|
}
|
|
if (isset($element["redAddTerm"])) {
|
|
$add->r = $element["redAddTerm"];
|
|
$add->g = $element["greenAddTerm"];
|
|
$add->b = $element["blueAddTerm"];
|
|
$add->alpha = $element["alphaAddTerm"];
|
|
}
|
|
return new ColorTransform($mult, $add);
|
|
}
|
|
} |