Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Friday, 7 June 2013

Perl script to extract and present hex colors found in arbitrary text

The following script searches STDIN for 3 or 6 symbol hex values and prints a html structure that represents those values as colors on page.
#!/usr/bin/perl -w
use Set::Scalar;

$uniqueColors = Set::Scalar->new;
while( defined( $_ = <STDIN> ) ){
 while ( $_ =~/(#[a-fA-F0-9]{1,6})/g ){
  $uniqueColors->insert( $1 )
 }
}
print <<HEADER;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Found HEX colors</TITLE>
   </HEAD>
   <BODY>
HEADER
while ( defined( my $color = $uniqueColors->each ) ){
 print "<p style=\"background: " . $color . "\">" . $color . "</p>\n"
}
print "</BODY>
</HTML>"
Used this one to parse color-theme-tangotango.el emacs color theme file.
To view example output hit "read more".