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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package ColorGradient;
use strict;
use warnings;
# RGB values off (0), on (1), increasing (I), or decreasing (D).
my @ColorPhases = ( ['1', 'I', '0'],
['D', '1', '0'],
['0', '1', 'I'],
['0', 'D', '1'],
['I', '0', '1'],
);
my $num_color_phases = scalar @ColorPhases;
my $discrete_color_phase_percent = 1 /$num_color_phases * 100;
####
sub get_RGB_gradient {
my ($num_colors) = @_;
## Returns a list of [R,G,B], ... values.
unless ($num_colors =~ /^\d+$/) {
die "Error, $num_colors should be a number";
}
my @colors;
$num_colors--;
for my $color_entry (0..$num_colors) {
my $percentage = ($color_entry-0.000001) / $num_colors * 100; # don't ever want to reach 100%
my $index = int ($percentage / $discrete_color_phase_percent);
my $color_phase = $ColorPhases[$index];
my $ratio_into_phase = ($percentage - $discrete_color_phase_percent*$index) / $discrete_color_phase_percent;
my $rgb_color = &_get_color($color_phase, $ratio_into_phase);
push (@colors, $rgb_color);
}
return (@colors);
}
###
sub convert_RGB_hex {
my @rgb_aref_vals = @_;
my @hex_vals;
foreach my $rgb_aref (@rgb_aref_vals) {
my ($r, $g, $b) = @$rgb_aref;
my $hex_r = sprintf("%02x", $r);
my $hex_g = sprintf("%02x", $g);
my $hex_b = sprintf ("%02x", $b);
push (@hex_vals, '#' . $hex_r . $hex_g . $hex_b);
}
return (@hex_vals);
}
####
sub _get_color {
my ($color_phase, $ratio_into_phase) = @_;
my $variable_rgb_val = int($ratio_into_phase * 255 + 4/9);
my @phase_vals = @$color_phase;
my @ret_rgb;
foreach my $phase_val (@phase_vals) {
my $rgb_val;
if ($phase_val eq '1') {
$rgb_val = 255;
}
elsif ($phase_val eq '0') {
$rgb_val = 0;
}
elsif ($phase_val eq 'I') {
$rgb_val = $variable_rgb_val;
}
elsif ($phase_val eq 'D') {
$rgb_val = 255 - $variable_rgb_val;
}
else {
die "Error, unrecognized phase value of $phase_val";
}
push (@ret_rgb, $rgb_val);
}
return ([@ret_rgb]);
}
1;
|