Created
May 27, 2013 03:53
-
-
Save alcidesqueiroz/5655107 to your computer and use it in GitHub Desktop.
Perl terminal Script which calculates the difference between two colors in percentages and generates a prepared scale-color SASS function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
if ($#ARGV != 1) { | |
print "usage: colordiff color_a color_b\n"; | |
exit; | |
} | |
$color_a = $ARGV[0]; | |
$color_b = $ARGV[1]; | |
$color_a_red = hex(substr($color_a, 0, 2)); | |
$color_a_green = hex(substr($color_a, 2, 2)); | |
$color_a_blue = hex(substr($color_a, 4, 2)); | |
$color_b_red = hex(substr($color_b, 0, 2)); | |
$color_b_green = hex(substr($color_b, 2, 2)); | |
$color_b_blue = hex(substr($color_b, 4, 2)); | |
printf( "scale-color(#%s, \$red: %.2f\%, \$green: %.2f\%, \$blue: %.2f\%)\n", | |
"$color_a", | |
channel_difference($color_a_red, $color_b_red), | |
channel_difference($color_a_green, $color_b_green), | |
channel_difference($color_a_blue, $color_b_blue)); | |
sub channel_difference{ | |
($color_a, $color_b) = @_; | |
if ($color_a > $color_b){ | |
my $basis = $color_a; | |
my $diff = $color_a - $color_b; | |
my $percent_diff = -($diff / ($basis / 100)); | |
return $percent_diff; | |
} elsif ($color_a < $color_b) { | |
my $basis = 255 - $color_a; | |
my $diff = $color_b - $color_a; | |
my $percent_diff = $diff / ($basis / 100); | |
return $percent_diff; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment