swf2ass/src/RadialGradient.php

84 lines
2.6 KiB
PHP

<?php
namespace swf2ass;
class RadialGradient 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){
$shape = new Shape();
//Create concentric circles to cut out a shape
$shape = $shape->merge(new Shape((new Circle(new Vector2(0, 0), (($item->endRatio / 255) * $diff) / 2 + $overlap / 4))->draw()));
$shape = $shape->merge(new Shape((new Circle(new Vector2(0, 0), (($item->startRatio / 255) * $diff) / 2 - $overlap / 4))->draw()));
$paths->commands[] = DrawPath::fill(
new FillStyleRecord($item->color),
$this->getMatrixTransform()->applyToShape($shape));
}
return $paths;
}
public function getMatrixTransform(): MatrixTransform {
return $this->transform;
}
public static function fromArray(array $element, MatrixTransform $transform): RadialGradient {
$colors = [];
foreach ($element["gradientRecords"] as $item) {
$colors[] = GradientItem::fromArray($item);
}
//TODO: interpolationMode, spreadMode
return new RadialGradient($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;
}
}