-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcolor_wheel.pl
More file actions
executable file
·38 lines (27 loc) · 848 Bytes
/
color_wheel.pl
File metadata and controls
executable file
·38 lines (27 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/perl
# Draw a HSV color wheel.
# Algorithm from:
# https://rosettacode.org/wiki/Color_wheel
use 5.010;
use strict;
use warnings;
use Imager;
use Math::GComplex qw(cplx i);
my ($width, $height) = (300, 300);
my $center = cplx($width / 2, $height / 2);
my $img = Imager->new(xsize => $width,
ysize => $height);
my $pi = atan2(0, -1);
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my $vector = $center - $x - $y * i;
my $magnitude = 2 * abs($vector) / $width;
my $direction = ($pi + atan2($vector->real, $vector->imag)) / (2 * $pi);
$img->setpixel(
x => $x,
y => $y,
color => {hsv => [360 * $direction, $magnitude, $magnitude < 1 ? 1 : 0]}
);
}
}
$img->write(file => 'color_wheel.png');