Created
December 21, 2011 21:26
-
-
Save carlosmcevilly/1507775 to your computer and use it in GitHub Desktop.
UIColor to image, using Perl and ImageMagick. This is just scary ugly. Next time I'll do it with Cocoa.
This file contains hidden or 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 | |
| use strict; | |
| use warnings; | |
| my $token; | |
| my @tokens; | |
| my ($num, $denom, $value, $hexcolor); | |
| my $outfile = "result.png"; | |
| # don't require the user to quote the argument string | |
| while (defined($token = shift)) { | |
| push(@tokens, $token); | |
| } | |
| my $line = join(' ', @tokens); | |
| my $orig = $line; | |
| die "usage: $0 [UIColor colorWithRed:...]\n" if ($line =~ m{^\s*$}); | |
| die "error: not overwriting existing file $outfile" if ( -e $outfile); | |
| # strip off any trailing 'f' from float, and add 0 if missing | |
| $line =~ s{(\d+)\.(\d*)(?{$denom=($^N)?$denom=$^N:$denom="0"})f?}{$1.$denom}gi; | |
| # extract float values, perform division, and rewrite the result as hex strings | |
| # could redo this and the next if to be combined, but readability is already an issue here | |
| $line =~ s{(\d+\.\d+)(?{$num=$^N})/(\d+\.\d+)(?{$denom=$^N})(?{$value=sprintf("0x%02x", (255*$num/$denom))})}{$value}gi; | |
| # extract the hex value strings and build a hexcolor | |
| if ($line =~ m{colorWithRed:0x(\w+)\s+green:0x(\w+)\s+blue:0x(\w+)\s+alpha:(\d+\.\d+)(?{$value=sprintf("%02x", $^N*255)})}) { | |
| $hexcolor = '#' . $1 . $2 . $3 . $value; | |
| } | |
| if ( ! defined($hexcolor)) { | |
| print "unable to parse string >>$orig<<\n"; | |
| print "result after rewriting was >>$line<<\n"; | |
| exit -1; | |
| } | |
| `convert -size 480x640 xc:"$hexcolor" $outfile`; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment