Skip to content

Instantly share code, notes, and snippets.

@carlosmcevilly
Created September 28, 2011 20:39
Show Gist options
  • Save carlosmcevilly/1249194 to your computer and use it in GitHub Desktop.
Save carlosmcevilly/1249194 to your computer and use it in GitHub Desktop.
hex2uicolor command line script - given a hex value color, print several useful representations of the color
#!/usr/bin/perl
# hex2uicolor
# Copyright 2009 by Carlos McEvilly - Apache License
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";
}
printf("[UIColor colorWithRed:%.1f/255.0 green:%.1f/255.0 blue:%.1f/255.0 alpha:1.0f]; $comment\n",
hex($r),
hex($g),
hex($b));
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"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment