Add support for bounds in random colors
E.g., `random color between blue and green`.master
parent
09d622130e
commit
8316a69f73
|
@ -46,18 +46,23 @@ sub normalize_color {
|
|||
####################
|
||||
|
||||
my %query_forms = (
|
||||
"rand(om)? $scolor" => \&random_color,
|
||||
"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_forms = keys %query_forms;
|
||||
|
||||
sub random_color {
|
||||
my %cap = @_;
|
||||
srand;
|
||||
my $c1 = normalize_color($cap{c1} // '#000000');
|
||||
my $c2 = normalize_color($cap{c2} // '#ffffff');
|
||||
my %data = (
|
||||
subtitle => 'Random color',
|
||||
subtitle_prefix => 'Random color between ',
|
||||
input_colors => [$c1, $c2],
|
||||
);
|
||||
my %result;
|
||||
my $color = normalize_color(rand_rgb_color());
|
||||
my $color = normalize_color(rand_rgb_color($c1, $c2));
|
||||
$data{result_color} = $color;
|
||||
$result{data} = \%data;
|
||||
return %result;
|
||||
|
@ -68,13 +73,10 @@ sub mix_colors {
|
|||
my $c1 = normalize_color($caps{c1});
|
||||
my $c2 = normalize_color($caps{c2});
|
||||
my %data = (
|
||||
subtitle_prefix => 'Mix ',
|
||||
input_colors => [$c1, $c2],
|
||||
);
|
||||
my %result = (
|
||||
options => {
|
||||
subtitle_content => 'DDH.rgb_color.mix',
|
||||
},
|
||||
);
|
||||
my %result;
|
||||
my $color = normalize_color(mix_2_rgb_colors($c1, $c2));
|
||||
$data{result_color} = $color;
|
||||
$result{data} = \%data;
|
||||
|
@ -84,7 +86,6 @@ sub mix_colors {
|
|||
sub normalize_result {
|
||||
my %result = @_;
|
||||
$result{text_answer} = $result{data}->{result_color};
|
||||
$result{options} //= {};
|
||||
return %result;
|
||||
}
|
||||
|
||||
|
@ -109,7 +110,7 @@ handle query_lc => sub {
|
|||
group => "text",
|
||||
options => {
|
||||
title_content => 'DDH.rgb_color.title_content',
|
||||
%{$result{options}},
|
||||
subtitle_content => 'DDH.rgb_color.sub_list',
|
||||
},
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<h4 class="c-base__sub">Mix
|
||||
<h4 class="c-base__sub">{{subtitle_prefix}}
|
||||
{{#concat input_colors sep="," conj=" and"}}
|
||||
{{{color this}}}
|
||||
{{/concat}}
|
39
t/RgbColor.t
39
t/RgbColor.t
|
@ -24,19 +24,21 @@ sub build_answer_mix {
|
|||
my %params = @_;
|
||||
return (
|
||||
text_answer => $params{result_color},
|
||||
data => \%params,
|
||||
options => {
|
||||
subtitle_content => 'DDH.rgb_color.mix',
|
||||
},
|
||||
data => {
|
||||
subtitle_prefix => 'Mix ',
|
||||
%params,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
sub build_answer_random {
|
||||
my %params = @_;
|
||||
return (
|
||||
text_answer => re($color_re),
|
||||
data => {
|
||||
result_color => re($color_re),
|
||||
subtitle => 'Random color',
|
||||
data => {
|
||||
result_color => re($color_re),
|
||||
subtitle_prefix => 'Random color between ',
|
||||
%params,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -46,7 +48,6 @@ sub build_structured_answer {
|
|||
my $builder = $test_builders{$type};
|
||||
my %answer = $builder->(%test_params);
|
||||
|
||||
$answer{options} //= {};
|
||||
return $answer{text_answer},
|
||||
structured_answer => {
|
||||
|
||||
|
@ -55,8 +56,8 @@ sub build_structured_answer {
|
|||
templates => {
|
||||
group => "text",
|
||||
options => {
|
||||
title_content => 'DDH.rgb_color.title_content',
|
||||
%{$answer{options}},
|
||||
title_content => 'DDH.rgb_color.title_content',
|
||||
subtitle_content => 'DDH.rgb_color.sub_list',
|
||||
},
|
||||
}
|
||||
};
|
||||
|
@ -73,13 +74,25 @@ my $tc_mix_black_white = build_test('mix',
|
|||
result_color => '#7f7f7f',
|
||||
);
|
||||
|
||||
my $tc_random_black_white = build_test('random',
|
||||
input_colors => ['#000000', '#ffffff'],
|
||||
);
|
||||
|
||||
my $tc_random_white_black = build_test('random',
|
||||
input_colors => ['#ffffff', '#000000'],
|
||||
);
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw( DDG::Goodie::RgbColor )],
|
||||
# Random colors
|
||||
'random color' => build_test('random'),
|
||||
'rand color' => build_test('random'),
|
||||
'random color' => $tc_random_black_white,
|
||||
'rand color' => $tc_random_black_white,
|
||||
# # With bounds
|
||||
'random color between white and black' => $tc_random_white_black,
|
||||
# # # W/o 'and'
|
||||
'random color between black white' => $tc_random_black_white,
|
||||
# Using 'colour'
|
||||
'random colour' => build_test('random'),
|
||||
'random colour' => $tc_random_black_white,
|
||||
# Mixing colors
|
||||
'mix 000000 ffffff' => $tc_mix_black_white,
|
||||
# # With leading '#'
|
||||
|
|
Loading…
Reference in New Issue