diff --git a/src/Degree.php b/src/Degree.php new file mode 100644 index 0000000..eaaaa8e --- /dev/null +++ b/src/Degree.php @@ -0,0 +1,19 @@ += 0 && $degree <= 360) { + return new self($degree); + } + + throw Exception('Degree must be between 0 and 360'); + } +} diff --git a/src/GradientImage.php b/src/GradientImage.php new file mode 100644 index 0000000..b84d04a --- /dev/null +++ b/src/GradientImage.php @@ -0,0 +1,50 @@ +toRgb($from); + $to = $this->toRgb($to); + + $image = imagecreatetruecolor($image_width, $image_height); + $image = $this->makeGradient($image, $from, $to, $prop->calculatorCallback()); + $image = imagerotate($image, 360 - $degree->value, 0); + + imagepng($image, 'image.png'); + imagedestroy($image); + } + + public function toRgb(string $hexColor): RgbColor + { + $hexColor = substr($hexColor, 1); + + $colors = collect(str_split($hexColor, 2)) + ->map(fn ($hex) => hexdec($hex)) + ->toArray(); + + return new RgbColor(...$colors); + } + + private function makeGradient(GdImage $image, RgbColor $from, RgbColor $to, callable $calculator): GdImage + { + $image_width = imagesx($image); + $image_height = imagesy($image); + + for ($i = 0; $i < $image_height; ++$i) { + $percent = $calculator($i, $image_height); + $color_r = (int) floor(($from->r - $to->r) * $percent) + $to->r; + $color_g = (int) floor(($from->g - $to->g) * $percent) + $to->g; + $color_b = (int) floor(($from->b - $to->b) * $percent) + $to->b; + + $color = imagecolorallocate($image, $color_r, $color_g, $color_b); + imageline($image, 0, $i, $image_width, $i, $color); + } + + return $image; + } +} diff --git a/src/Prop.php b/src/Prop.php new file mode 100644 index 0000000..dbb59fe --- /dev/null +++ b/src/Prop.php @@ -0,0 +1,17 @@ + fn ($i, $height) => $i / $height, + static::WAVE => fn ($i, $height) => abs(($i / $height) - 0.5), + }; + } +} diff --git a/src/RgbColor.php b/src/RgbColor.php new file mode 100644 index 0000000..26216ff --- /dev/null +++ b/src/RgbColor.php @@ -0,0 +1,10 @@ +