swf2ass/src/LinearGradient.php

84 lines
2.5 KiB
PHP

<?php
namespace swf2ass;
class LinearGradient implements Gradient {
/** @var GradientItem[] */
public array $colors;
public MatrixTransform $transform;
public int $spreadMode;
public int $interpolationMode;
/**
* @param GradientItem[] $colors
* @param MatrixTransform $transform
*/
public function __construct(array $colors, MatrixTransform $transform, int $spreadMode, int $interpolationMode) {
$this->colors = $colors;
$this->transform = $transform;
$this->spreadMode = $spreadMode;
$this->interpolationMode = $interpolationMode;
}
public function getItems(): array {
return $this->colors;
}
public function getSpreadMode() : int{
return $this->spreadMode;
}
public function getInterpolationMode() : int{
return $this->interpolationMode;
}
public function getInterpolatedDrawPaths(int $overlap = 0, int $slices = self::AUTO_SLICES): DrawPathList{
//items is max size 8 to 15 depending on SWF version
$min = -16384;
$max = 16384;
$diff = $max - $min;
//TODO spreadMode
$paths = new DrawPathList();
foreach (Utils::lerpGradient($this, $slices) as $item){
$paths->commands[] = DrawPath::fill(
new FillStyleRecord($item->color),
$this->getMatrixTransform()->applyToShape(new Shape((new Rectangle(
new Vector2($min + ($item->startRatio / 255) * $diff - $overlap / 2, $min),
new Vector2($min + ($item->endRatio / 255) * $diff + $overlap / 2, $max)
))->draw())));
}
return $paths;
}
public function getMatrixTransform(): MatrixTransform {
return $this->transform;
}
public static function fromArray(array $element, MatrixTransform $transform): LinearGradient {
$colors = [];
foreach ($element["gradientRecords"] as $item) {
$colors[] = GradientItem::fromArray($item);
}
//TODO: interpolationMode, spreadMode
return new LinearGradient($colors, $transform, $element["spreadMode"], $element["interpolationMode"]);
}
public function applyColorTransform(ColorTransform $transform): Gradient{
$new = clone $this;
foreach ($new->colors as $i => $gradientItem){
$new->colors[$i] = new GradientItem($gradientItem->ratio, $transform->applyToColor($gradientItem->color));
}
return $new;
}
}