Add support for opposite colors

master
Ben Moon 2016-07-05 14:42:12 +01:00
parent be81fa4df2
commit 65b293005a
2 changed files with 83 additions and 23 deletions

View File

@ -9,13 +9,14 @@ use Color::Library;
use Color::RGB::Util qw(
mix_2_rgb_colors
rand_rgb_color
reverse_rgb_color
);
zci answer_type => 'rgb_color';
zci is_cached => 0;
triggers any => 'color', 'colour', 'mix';
triggers any => 'color', 'colour', 'mix', 'opposite';
#####################
# Color Constants #
@ -51,7 +52,7 @@ sub normalize_colors_for_template {
map { {
hex => $_,
name => common_name($_),
} } @colors;
} } map { normalize_color($_) } @colors;
}
sub normalize_color_for_template {
@ -59,15 +60,28 @@ sub normalize_color_for_template {
return $colors[0];
}
sub probably_relevant {
my $text = shift;
return 1 if $text =~ /$scolor/;
return 0;
}
####################
# Query Handlers #
####################
my %query_forms = (
"rand(om)? $scolor( between (?<c1>$color_re)( and)? " .
"(?<c2>$color_re))?\$" => \&random_color,
"mix (?<c1>$color_re)( and)? (?<c2>$color_re)" => \&mix_colors,
my %query_cat = (
random => "rand(om)? $scolor( between (?<c1>$color_re)( and)? " .
"(?<c2>$color_re))?\$",
mix => "mix (?<c1>$color_re)( and)? (?<c2>$color_re)",
reverse => "opposite( of)? (?<c>$color_re)",
);
my %query_forms = (
$query_cat{mix} => \&mix_colors,
$query_cat{random} => \&random_color,
$query_cat{reverse} => \&reverse_color,
);
my @query_forms = keys %query_forms;
sub random_color {
@ -80,8 +94,8 @@ sub random_color {
input_colors => [normalize_colors_for_template($c1, $c2)],
);
my %result;
my $color = normalize_color(rand_rgb_color($c1, $c2));
$data{result_color} = normalize_color_for_template($color);
my $color = normalize_color_for_template(rand_rgb_color($c1, $c2));
$data{result_color} = $color;
$result{data} = \%data;
return %result;
}
@ -95,14 +109,31 @@ sub mix_colors {
input_colors => [normalize_colors_for_template($c1, $c2)],
);
my %result;
my $color = normalize_color(mix_2_rgb_colors($c1, $c2));
$data{result_color} = normalize_color_for_template($color);
my $color = normalize_color_for_template(mix_2_rgb_colors($c1, $c2));
$data{result_color} = $color;
$result{data} = \%data;
return %result;
}
my $rev_form = $query_cat{reverse};
sub reverse_color {
my %cap = @_;
my $c = normalize_color($cap{c});
my $color = normalize_color_for_template(reverse_rgb_color($c));
if ($cap{query} =~ /$rev_form(?<remr>.+)/) {
return unless probably_relevant($+{remr});
}
return (
data => {
subtitle_prefix => 'Opposite color of ',
input_colors => [normalize_color_for_template($c)],
result_color => $color,
},
);
}
sub normalize_result {
my %result = @_;
my %result = @_ or return;
$result{text_answer} = $result{data}->{result_color}{hex};
return %result;
}
@ -117,7 +148,8 @@ handle query_lc => sub {
$match;
} @query_forms or return;
my $action = $query_forms{$form};
my %result = normalize_result($action->(%cap));
$cap{query} = $query;
my %result = normalize_result($action->(%cap)) or return;
return $result{text_answer},
structured_answer => {

View File

@ -16,19 +16,27 @@ zci is_cached => 0;
my %test_builders = (
mix => \&build_answer_mix,
random => \&build_answer_random,
reverse => \&build_answer_reverse,
);
my $color_re = qr/^#\p{XDigit}{6}$/i;
sub build_standard_builder {
my $subtitle_prefix = shift;
return sub {
my %params = @_;
return (
text_answer => $params{result_color}->{hex},
data => {
subtitle_prefix => $subtitle_prefix,
%params,
}
);
};
}
sub build_answer_mix {
my %params = @_;
return (
text_answer => $params{result_color}->{hex},
data => {
subtitle_prefix => 'Mix ',
%params,
}
);
build_standard_builder('Mix ')->(@_);
}
sub build_answer_random {
@ -45,6 +53,10 @@ sub build_answer_random {
);
}
sub build_answer_reverse {
build_standard_builder('Opposite color of ')->(@_);
}
sub build_structured_answer {
my ($type, %test_params) = @_;
my $builder = $test_builders{$type};
@ -111,6 +123,11 @@ my $pinkish_blue = {
name => '',
};
my $yellow = {
hex => '#ffff00',
name => 'yellow',
};
my $tc_mix_black_white = build_test('mix',
input_colors => [$black, $white],
result_color => $grey,
@ -134,6 +151,16 @@ my $tc_random_white_black = build_test('random',
input_colors => [$white, $black],
);
my $tc_opp_white = build_test('reverse',
input_colors => [$white],
result_color => $black,
);
my $tc_opp_blue = build_test('reverse',
input_colors => [$blue],
result_color => $yellow,
);
ddg_goodie_test(
[qw( DDG::Goodie::RgbColor )],
# Random colors
@ -153,9 +180,12 @@ ddg_goodie_test(
'mix 000000 and ffffff' => $tc_mix_black_white,
# # Using names
'mix black and white' => $tc_mix_black_white,
# Reversing colors
'opposite of white' => $tc_opp_white,
# Sample queries (from checking query suggestions)
'mix pink and blue what color do you get' => $tc_mix_pink_blue,
'what do you get if you mix blue and orange' => $tc_mix_blue_orange,
"what's opposite of blue on the color wheel" => $tc_opp_blue,
# Invalid queries
'color' => undef,
'color ffffff' => undef,
@ -171,9 +201,7 @@ ddg_goodie_test(
'pictures of blue rain' => undef,
'blue hex color' => undef,
# # With potential to trigger in the future
'blue and gold' => undef,
'opposite of blue' => undef,
"what's opposite of blue on the color wheel" => undef,
'blue and gold' => undef,
);