Created
April 26, 2015 17:24
-
-
Save carlosmcevilly/3a99dad351f419617fc7 to your computer and use it in GitHub Desktop.
Generate UIColor code and related images from a hex color RGB string
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 | |
# hex2uicolor by Carlos McEvilly | |
use strict; | |
use warnings; | |
my ($r,$g,$b) = @ARGV; | |
die "\nusage: $0 <red> <green> <blue>\n\n" . | |
"examples:\n\n" . | |
"\t$0 ff aa 00\n" . | |
"\t$0 ffaa00\n" . | |
"\t$0 f a 0\n" . | |
"\t$0 fa0\n\n" | |
unless (defined $r && (($r =~ /^([a-f0-9][a-f0-9][a-f0-9]){1,2}$/i) | |
|| defined($b))); | |
if (! defined($g) && ! defined($b)) { | |
if ($r =~ m{^([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$}i) { | |
$r = $1; | |
$g = $2; | |
$b = $3; | |
} | |
} | |
$r = "$r$r" if ($r =~ /^.$/); | |
$g = "$g$g" if ($g =~ /^.$/); | |
$b = "$b$b" if ($b =~ /^.$/); | |
my $comment = "// #$r$g$b"; | |
# use the short form in the comment if possible | |
if ($comment =~ m{^// #(.)\1(.)\2(.)\3$}) { | |
$comment = "// #$1$2$3"; | |
} | |
my $nscolor = sprintf("[UIColor colorWithRed:%.1f/255.0 green:%.1f/255.0 blue:%.1f/255.0 alpha:1.f]; $comment\n", | |
hex($r), | |
hex($g), | |
hex($b)); | |
print $nscolor; | |
$nscolor = sprintf("[UIColor colorWithRed:%.03f green:%.03f blue:%.03f alpha:1.f]; $comment\n", | |
hex($r)/255.0, | |
hex($g)/255.0, | |
hex($b)/255.0); | |
print $nscolor; | |
$nscolor = sprintf("[UIColor colorWithRed:%.02f green:%.02f blue:%.02f alpha:1.f]; $comment\n", | |
hex($r)/255.0, | |
hex($g)/255.0, | |
hex($b)/255.0); | |
print $nscolor; | |
$nscolor = sprintf("[UIColor colorWithRed:(CGFloat)0x%02X green:(CGFloat)0x%02X blue:(CGFloat)0x%02X alpha:1.f]; $comment\n", | |
hex($r), | |
hex($g), | |
hex($b)); | |
print $nscolor; | |
printf("CGContextSetRGBStrokeColor(context, %.1f/255.0, %.1f/255.0, %.1f/255.0, 1.0f); $comment\n", | |
hex($r), | |
hex($g), | |
hex($b)); | |
printf( | |
"\t<dict>\n" . | |
"\t\t<key>red</key>\n" . | |
"\t\t<real>" . hex($r) . "</real>\n" . | |
"\t\t<key>green</key>\n" . | |
"\t\t<real>" . hex($g) . "</real>\n" . | |
"\t\t<key>blue</key>\n" . | |
"\t\t<real>" . hex($b) . "</real>\n" . | |
"\t\t<key>alpha</key>\n" . | |
"\t\t<real>1.0</real>\n" . | |
"\t</dict>\n" | |
); | |
print "generated files:\n\n"; | |
my $output_file = sprintf("hexcolor-%s%s%s-480x480.png", $r, $g, $b); | |
`convert -size 480x480 xc:"#$r$g$b" -geometry 480x480 $output_file`; | |
print "$output_file\n"; | |
$output_file = sprintf("hexcolor-%s%s%s-1x1.png", $r, $g, $b); | |
`convert -size 1x1 xc:"#$r$g$b" -geometry 1x1 $output_file`; | |
print "$output_file\n"; | |
$output_file = sprintf("hexcolor-%s%s%s-1x1\@2x.png", $r, $g, $b); | |
`convert -size 2x2 xc:"#$r$g$b" -geometry 2x2 $output_file`; | |
print "$output_file\n"; | |
$output_file = sprintf("hexcolor-%s%s%s-1x1\@3x.png", $r, $g, $b); | |
`convert -size 3x3 xc:"#$r$g$b" -geometry 3x3 $output_file`; | |
print "$output_file\n"; | |
$output_file = sprintf("hexcolor-%s%s%s-10x10-stretchable.png", $r, $g, $b); | |
`convert -size 10x10 xc:"#$r$g$b" -geometry 10x10 $output_file`; | |
print "$output_file\n"; | |
#`open $output_file`; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment