Skip to content

Commit

Permalink
Add support for three-digit hex codes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiltyDolphin committed Aug 13, 2016
1 parent 31c3477 commit 6e31737
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/DDG/Goodie/RgbColor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ my $color_name_re = '(?:' .
$color_name_re =~ s/\\ /[ -]?/g;

my $scolor = 'colou?rs?';
my $color_re = qr/(?:$color_name_re|#?\p{XDigit}{6})/;
my $color_re = qr/(?:$color_name_re|#?(\p{XDigit}{6}|\p{XDigit}{3}))/;

# Some stop words relevant to color queries.
my @custom_stops = (
Expand All @@ -80,7 +80,10 @@ sub normalize_color {
return $colors{$color} if exists $colors{$color};
$color =~ s/ //g;
return $colors{$color} if exists $colors{$color};
$color =~ s/^#//gr;
my $hex = $color =~ s/^#//gr;
$hex =~ /^.{3}$/
? join '', map { "$_$_" } split '', $hex
: $hex;
}

sub common_name {
Expand Down
4 changes: 4 additions & 0 deletions t/RgbColor.t
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ ddg_goodie_test(
'complementary color of white' => $tc_opp_white,
'complement white' => $tc_opp_white,
'opposite color for white' => $tc_opp_white,
# # Using three-digit hex
'opposite of #690' => $tc_opp_dsy,
# # # Must be three or six digits
'opposite of #6901' => undef,
# Tinting colors
'tint black with white' => $tc_tint_black_white,
'tint white with black' => $tc_tint_white_black,
Expand Down

0 comments on commit 6e31737

Please sign in to comment.