Skip to content

Instantly share code, notes, and snippets.

@WebDragon
Last active November 11, 2022 16:34
Show Gist options
  • Save WebDragon/90426e38ce872a3b519b8965047272de to your computer and use it in GitHub Desktop.
Save WebDragon/90426e38ce872a3b519b8965047272de to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
## Author: Scott R. Godin - MAD House Graphics
## Created: Wed Nov 9 12:42:52 EST 2022
## Last Updated: Fri, Nov 11, 2022 11:32:04 AM
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use Regexp::Common qw/ number /;
use POSIX qw/ ceil /;
use v5.16;
my ($help, $usage, $percent, $hex, $steps, @coords);
GetOptions(
'showpercent|percent:i' => sub { $percent = $_[1] || 255 },
'showhex|hex' => \$hex,
'steps|size=i' => \$steps,
'coordinates|coords=i{2}' => \@coords,
'help|?' => \$help,
usage => \$usage
) or die ("error in command line arguments");
pod2usage(1) if $help;
pod2usage( -exitval => 0, -verbose => 2) if $usage;
die "Missing value --steps" unless ( defined($steps) && ( $steps > 0 ) && ( $steps =~ / ^ $RE{num}{int} $ /x ) );
die "missing values in --coords" unless ( ( defined($coords[0]) && defined($coords[1]) ) && ( $coords[0] =~ / ^ $RE{num}{int} $ /x ) && ( $coords[1] =~ / ^ $RE{num}{int} $ /x ) );
my $distance = abs( $coords[0] - $coords[1] );
my $stride = $distance / ($steps - 1 );
sub output ($) {
my ($val, $result) = shift;
$result = $val;
$result .= " => \t( " .ceil( ( $val / $percent ) * 100 ) . "% )" if $percent;
$result .= " => \t" . sprintf("0x%X", $val) if $hex;
return $result;
}
if ($coords[0] > $coords[1]) {
say "Calculating interpolation of values between $coords[0] and $coords[1] in a total of $steps steps, with a stride of $stride";
my $j;
for (my $i = $coords[0]; $i > $coords[1]; $i -= $stride) {
$j = int( $i + $i/abs($i*2 || 1));
say output $j;
}
$j -= $stride;
$j = int( $j + $j/abs($j*2 || 1) );
say output $j;
}
else {
say "Calculating interpolation of values between $coords[0] and $coords[1] in a total of $steps steps, with a stride of $stride";
my $j;
for (my $i = $coords[0]; $i < $coords[1]; $i += $stride) {
$j = int( $i + $i/abs($i*2 || 1));
say output $j;
}
$j += $stride;
$j = int( $j + $j/abs($j*2 || 1) );
say output $j;
}
__END__
=head1 NAME
smoothly Interpolate between two numbers in exactly this number of stages, and output the resultant values in decimal and hex, and optionally percent
=head1 SYNOPSIS
interpolate [options]
=head1 OPTIONS
=over 8
=item B<--steps, --size>, required
specify integer number of how many steps in total between the two coordinates, including the endpoints
=item B<--coords, --coordinates>, required
specify two positive-number integer endpoints in whichever order you prefer
=item B<--showpercent [val], --percent [val]>, optional
specify the max of the range coords to be used in additionally appending the percent of the decimal value relative to the max value to the stages as they are output
defaults to 255 to stay in line with the html hex color range purpose of the script
=item B<--showhex, --hex>, optional
additionally append the hex values to the stages as they are output
=item B<--help, -?>
Print a brief help message and exits
=item B<--usage>
Prints the manual page and exits
=back
=head1 DESCRIPTION
This program calculates values of the steps between and including the two end points in the number of stages specified
and outputs the results as both decimal and hex values. Useful for appending a range of transparency values across multiple hex-value css colors in a linear gradient such as
background-image: linear-gradient(to top, #48bac7, #44b0bd, #41a7b3, #3d9da9, #3a949f);
=head1 EXAMPLE
interpolate --steps 5 --coords 46 255
Calculating interpolation of values between 46 and 255 in a total of 5 steps, with a stride of 52.25
46 => 2E
98 => 62
151 => 97
203 => CB
255 => FF
=head1 AUTHOR
Scott R. Godin - L<MAD House Graphics|https://www.madhousegraphics.com>
Copyright (c) 2022 Scott R. Godin. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
# vim600: set ft=perl :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment