Merge pull request #625 from mwmiller/color_sanity

ColorCodes: simplify and comment a bit.
master
Matt Miller 2014-09-10 17:24:22 -04:00
commit b391fe3204
2 changed files with 59 additions and 55 deletions

View File

@ -8,6 +8,7 @@ use Convert::Color;
use Convert::Color::Library;
use Convert::Color::RGB8;
use Math::Round;
use Try::Tiny;
my %types = ( # hash of keyword => Convert::Color prefix
rgb => 'rgb8',
@ -63,69 +64,72 @@ sub percentify {
}
my %trigger_invert = map { $_ => 1 } (qw( inverse negative opposite ));
my %trigger_ignore = map { $_ => 1 } (qw( code ));
my %trigger_filler = map { $_ => 1 } (qw( code ));
handle matches => sub {
my $type;
my $color;
my $inverse;
for (@_) {
next unless defined $_;
my $q = lc;
$type = $types{$q} if exists $types{$q};
if ($trigger_invert{$q}) {
$inverse = 1;
} elsif (!$trigger_ignore{$q}) {
$color = $q unless defined $type && exists $types{$q};
my $type = 'rgb8'; # Default type, can be overridden below.
my @matches = @_;
foreach my $q (map { lc $_ } grep { defined $_ } @matches) {
# $q now contains the defined normalized matches which can be:
if (exists $types{$q}) {
$type = $types{$q}; # - One of our types.
} elsif ($trigger_invert{$q}) {
$inverse = 1; # - An inversion trigger
} elsif (!$trigger_filler{$q}) { # - A filler word for more natural querying
$color = $q; # - A presumed color
}
}
$type //= 'rgb8';
my @colorin = split /(?:,\s*|\s+)/, $color;
my @colorout;
return unless $color; # Need a color to continue!
for (@colorin) {
# handle percents
if (/(\d+(?:\.\d+)?)%$/) {
my $num = $1;
$num =~ s/\.//;
my $len = length($num);
return unless $len > 0 && $len <= 3;
push @colorout, "0.0$num" if $len == 1;
push @colorout, "0.$num" if $len == 2;
push @colorout, "$num" if $len == 3;
$type = 'rgb' if $type eq 'rgb8';
}
else { push @colorout, $_; }
}
$color = join ',', @colorout;
$color =~ s/(,\s*|\s+)/,/g; # Spaces to commas for things like "hsl 194 0.53 0.79"
if ($color =~ /^([0-9a-f]{3,6})$/) {
if(length($color) == 3) {
$color = '';
$color .= $_.$_ for split '', $1;
}
if ($color =~ s/#?([0-9a-f]{3,6})$/$1/) { # Color looks like a hex code, strip the leading #
$color = join('', map { $_ . $_ } (split '', $color)) if (length($color) == 3); # Make three char hex into six chars by repeating each in turn
$type = 'rgb8';
} else {
try {
# See if we can find the color in one of our dictionaries.
$color = join(',', Convert::Color::Library->new($color_dictionaries . '/' . $color)->as_rgb8->hex);
$type = 'rgb8'; # We asked for rgb8 from our dictionary, so make sure our type matches.
};
}
return unless $type && $color;
my $col = try { Convert::Color->new("$type:$color") }; # Everything should be ready for conversion now.
return unless $col; # Guess not.
eval { $color = join(',',Convert::Color::Library->new($color_dictionaries.'/'.$color)->as_rgb8->hex); $type = 'rgb8'; };
my $col;
eval { $col = Convert::Color->new("$type:$color"); };
return if $@;
if ($inverse) {
my $rgb = $col->as_rgb8;
$col = Convert::Color::RGB8->new(255 - $rgb->red, 255 - $rgb->green, 255 - $rgb->blue);
if ($inverse) { # We triggered on the inverse, so do the flip.
my $orig_rgb = $col->as_rgb8;
$col = Convert::Color::RGB8->new(255 - $orig_rgb->red, 255 - $orig_rgb->green, 255 - $orig_rgb->blue);
}
my $rgb = $col->as_rgb8;
my $hsl = $col->as_hsl;
my $text = sprintf("Hex: %s ~ rgb(%d, %d, %d) ~ rgb(%s, %s, %s) ~ hsl(%d, %s, %s) ~ cmyb(%s, %s, %s, %s)", '#'.$rgb->hex, $col->as_rgb8->rgb8, percentify($col->as_rgb->rgb), round($hsl->hue), percentify($hsl->saturation, $hsl->lightness, $col->as_cmyk->cmyk));
return $text, html => '<div style="background:#'.$rgb->hex.';border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>'.$text." [<a href='http://labs.tineye.com/multicolr#colors=".$rgb->hex.";weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/".$rgb->hex."' title='Tints, information and similar colors on color-hex.com'>Info</a>]";
my @color_template_data = (
'#' . $rgb->hex,
$col->as_rgb8->rgb8, percentify($col->as_rgb->rgb),
round($hsl->hue), percentify($hsl->saturation, $hsl->lightness, $col->as_cmyk->cmyk));
# Create the output!
my $text = sprintf("Hex: %s ~ rgb(%d, %d, %d) ~ rgb(%s, %s, %s) ~ hsl(%d, %s, %s) ~ cmyb(%s, %s, %s, %s)", @color_template_data);
my $html_text = sprintf("Hex: %s &middot; rgb(%d, %d, %d) &middot; rgb(%s, %s, %s) <br> hsl(%d, %s, %s) &middot; cmyb(%s, %s, %s, %s) &middot;",
@color_template_data);
return $text,
html => '<div style="background:#'
. $rgb->hex
. ';border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>'
. $html_text
. " [<a href='http://labs.tineye.com/multicolr#colors="
. $rgb->hex
. ";weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/"
. $rgb->hex
. "' title='Tints, information and similar colors on color-hex.com'>Info</a>]";
};
1;

View File

@ -14,43 +14,43 @@ ddg_goodie_test(
)],
'hex color code for cyan' => test_zci(
'Hex: #00ffff ~ rgb(0, 255, 255) ~ rgb(0%, 100%, 100%) ~ hsl(180, 100%, 50%) ~ cmyb(100%, 0%, 0%, 0%)',
html => qq(<div style="background:#00ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ffff ~ rgb(0, 255, 255) ~ rgb(0%, 100%, 100%) ~ hsl(180, 100%, 50%) ~ cmyb(100%, 0%, 0%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=00ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#00ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ffff &middot; rgb(0, 255, 255) &middot; rgb(0%, 100%, 100%) <br> hsl(180, 100%, 50%) &middot; cmyb(100%, 0%, 0%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=00ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'rgb(173,216,230)' => test_zci(
'Hex: #add8e6 ~ rgb(173, 216, 230) ~ rgb(68%, 85%, 90%) ~ hsl(195, 53%, 79%) ~ cmyb(25%, 6%, 0%, 10%)',
html => qq(<div style="background:#add8e6;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #add8e6 ~ rgb(173, 216, 230) ~ rgb(68%, 85%, 90%) ~ hsl(195, 53%, 79%) ~ cmyb(25%, 6%, 0%, 10%) [<a href='http://labs.tineye.com/multicolr#colors=add8e6;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/add8e6' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#add8e6;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #add8e6 &middot; rgb(173, 216, 230) &middot; rgb(68%, 85%, 90%) <br> hsl(195, 53%, 79%) &middot; cmyb(25%, 6%, 0%, 10%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=add8e6;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/add8e6' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'hsl 194 0.53 0.79' => test_zci(
'Hex: #add8e5 ~ rgb(173, 216, 229) ~ rgb(68%, 85%, 90%) ~ hsl(194, 53%, 79%) ~ cmyb(25%, 6%, 0%, 10%)',
html => qq(<div style="background:#add8e5;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #add8e5 ~ rgb(173, 216, 229) ~ rgb(68%, 85%, 90%) ~ hsl(194, 53%, 79%) ~ cmyb(25%, 6%, 0%, 10%) [<a href='http://labs.tineye.com/multicolr#colors=add8e5;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/add8e5' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#add8e5;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #add8e5 &middot; rgb(173, 216, 229) &middot; rgb(68%, 85%, 90%) <br> hsl(194, 53%, 79%) &middot; cmyb(25%, 6%, 0%, 10%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=add8e5;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/add8e5' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'cmyk(0.12, 0, 0, 0)' => test_zci(
'Hex: #e0ffff ~ rgb(224, 255, 255) ~ rgb(88%, 100%, 100%) ~ hsl(180, 100%, 94%) ~ cmyb(12%, 0%, 0%, 0%)',
html => qq(<div style="background:#e0ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #e0ffff ~ rgb(224, 255, 255) ~ rgb(88%, 100%, 100%) ~ hsl(180, 100%, 94%) ~ cmyb(12%, 0%, 0%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=e0ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/e0ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#e0ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #e0ffff &middot; rgb(224, 255, 255) &middot; rgb(88%, 100%, 100%) <br> hsl(180, 100%, 94%) &middot; cmyb(12%, 0%, 0%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=e0ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/e0ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'#00ff00' => test_zci(
'Hex: #00ff00 ~ rgb(0, 255, 0) ~ rgb(0%, 100%, 0%) ~ hsl(120, 100%, 50%) ~ cmyb(100%, 0%, 100%, 0%)',
html => qq(<div style="background:#00ff00;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ff00 ~ rgb(0, 255, 0) ~ rgb(0%, 100%, 0%) ~ hsl(120, 100%, 50%) ~ cmyb(100%, 0%, 100%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=00ff00;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ff00' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#00ff00;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ff00 &middot; rgb(0, 255, 0) &middot; rgb(0%, 100%, 0%) <br> hsl(120, 100%, 50%) &middot; cmyb(100%, 0%, 100%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=00ff00;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ff00' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'#0f0' => test_zci(
'Hex: #00ff00 ~ rgb(0, 255, 0) ~ rgb(0%, 100%, 0%) ~ hsl(120, 100%, 50%) ~ cmyb(100%, 0%, 100%, 0%)',
html => qq(<div style="background:#00ff00;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ff00 ~ rgb(0, 255, 0) ~ rgb(0%, 100%, 0%) ~ hsl(120, 100%, 50%) ~ cmyb(100%, 0%, 100%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=00ff00;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ff00' title='Tints, information and similar colors on color-hex.com'>Info</a>])
html => qq(<div style="background:#00ff00;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ff00 &middot; rgb(0, 255, 0) &middot; rgb(0%, 100%, 0%) <br> hsl(120, 100%, 50%) &middot; cmyb(100%, 0%, 100%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=00ff00;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ff00' title='Tints, information and similar colors on color-hex.com'>Info</a>])
),
'inverse of the color red' => test_zci(
'Hex: #00ffff ~ rgb(0, 255, 255) ~ rgb(0%, 100%, 100%) ~ hsl(180, 100%, 50%) ~ cmyb(100%, 0%, 0%, 0%)',
html => qq(<div style="background:#00ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ffff ~ rgb(0, 255, 255) ~ rgb(0%, 100%, 100%) ~ hsl(180, 100%, 50%) ~ cmyb(100%, 0%, 0%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=00ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
html => qq(<div style="background:#00ffff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #00ffff &middot; rgb(0, 255, 255) &middot; rgb(0%, 100%, 100%) <br> hsl(180, 100%, 50%) &middot; cmyb(100%, 0%, 0%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=00ffff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/00ffff' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
),
'rgb(0 255 0)\'s inverse' => test_zci(
'Hex: #ff00ff ~ rgb(255, 0, 255) ~ rgb(100%, 0%, 100%) ~ hsl(300, 100%, 50%) ~ cmyb(0%, 100%, 0%, 0%)',
html => qq(<div style="background:#ff00ff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #ff00ff ~ rgb(255, 0, 255) ~ rgb(100%, 0%, 100%) ~ hsl(300, 100%, 50%) ~ cmyb(0%, 100%, 0%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=ff00ff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/ff00ff' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
html => qq(<div style="background:#ff00ff;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #ff00ff &middot; rgb(255, 0, 255) &middot; rgb(100%, 0%, 100%) <br> hsl(300, 100%, 50%) &middot; cmyb(0%, 100%, 0%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=ff00ff;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/ff00ff' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
),
'html bluishblack' => test_zci(
'Hex: #202428 ~ rgb(32, 36, 40) ~ rgb(13%, 14%, 16%) ~ hsl(210, 11%, 14%) ~ cmyb(20%, 10%, 0%, 84%)',
html => qq(<div style="background:#202428;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #202428 ~ rgb(32, 36, 40) ~ rgb(13%, 14%, 16%) ~ hsl(210, 11%, 14%) ~ cmyb(20%, 10%, 0%, 84%) [<a href='http://labs.tineye.com/multicolr#colors=202428;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/202428' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
html => qq(<div style="background:#202428;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #202428 &middot; rgb(32, 36, 40) &middot; rgb(13%, 14%, 16%) <br> hsl(210, 11%, 14%) &middot; cmyb(20%, 10%, 0%, 84%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=202428;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/202428' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
),
'red html code' => test_zci(
'Hex: #ff0000 ~ rgb(255, 0, 0) ~ rgb(100%, 0%, 0%) ~ hsl(0, 100%, 50%) ~ cmyb(0%, 100%, 100%, 0%)',
html => qq(<div style="background:#ff0000;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #ff0000 ~ rgb(255, 0, 0) ~ rgb(100%, 0%, 0%) ~ hsl(0, 100%, 50%) ~ cmyb(0%, 100%, 100%, 0%) [<a href='http://labs.tineye.com/multicolr#colors=ff0000;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/ff0000' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
html => qq(<div style="background:#ff0000;border:2px solid #999;height:30px;width:30px;margin:5px;margin-right:10px;margin-top:3px;float:left;"></div>Hex: #ff0000 &middot; rgb(255, 0, 0) &middot; rgb(100%, 0%, 0%) <br> hsl(0, 100%, 50%) &middot; cmyb(0%, 100%, 100%, 0%) &middot; [<a href='http://labs.tineye.com/multicolr#colors=ff0000;weights=100;'>Images</a>] [<a href='http://www.color-hex.com/color/ff0000' title='Tints, information and similar colors on color-hex.com'>Info</a>]),
),
'bluishblack html' => undef,
'HTML email' => undef,