diff --git a/lib/DDG/Goodie/RgbColor.pm b/lib/DDG/Goodie/RgbColor.pm index 64b474ef1..96b26bb98 100644 --- a/lib/DDG/Goodie/RgbColor.pm +++ b/lib/DDG/Goodie/RgbColor.pm @@ -4,7 +4,7 @@ use DDG::Goodie; use strict; use warnings; -use List::Util qw(first); +use List::Util qw(first sum); use Color::Library; use Color::RGB::Util qw( mix_2_rgb_colors @@ -106,6 +106,17 @@ sub right_decimal_from_amount { } } +sub normalize_amounts_for_template { + my ($type, @amounts) = @_; + if ($type eq 'part') { + my $tot = sum @amounts; + @amounts = map { ($_ / $tot) } @amounts; + } elsif ($type eq 'percent') { + @amounts = map { $_ / 100 } @amounts; + } + return @amounts; +} + ############### # Relevancy # ############### @@ -186,15 +197,14 @@ sub mix_colors { my $c2 = normalize_color($+{c}); my $a2 = $+{a}; my $pct = 0.5; - my $amount_type = 'percent'; - my $amt1 = 50; - my $amt2 = 50; + my $amt1 = 0.5; + my $amt2 = 0.5; if ($a1 // $a2) { ($amt1, my $t1) = amount_type_from_text($a1); ($amt2, my $t2) = amount_type_from_text($a2); return unless $t1 eq $t2; - $amount_type = $t1; $pct = right_decimal_from_amount($amt1, $amt2, $t1) or return; + ($amt1, $amt2) = normalize_amounts_for_template($t1, $amt1, $amt2); } my %data = ( subtitle_prefix => 'Mix ', @@ -202,7 +212,6 @@ sub mix_colors { { color => $c1, amount => $amt1, }, { color => $c2, amount => $amt2, }, )], - amount_type => $amount_type, ); my %result; my $color = normalize_color_for_template(mix_2_rgb_colors($c1, $c2, $pct)); diff --git a/t/RgbColor.t b/t/RgbColor.t index 88a3cc903..2b4764330 100644 --- a/t/RgbColor.t +++ b/t/RgbColor.t @@ -40,11 +40,10 @@ sub build_answer_mix { my $inps = $params{input_colors}; do { my $i = 0; - map { $inps->[$i] = { amount => 50, %{$_} }; $i++ } + map { $inps->[$i] = { amount => 0.5, %{$_} }; $i++ } @{$params{input_colors}} }; $params{input_colors} = $inps; - $params{amount_type} //= 'percent'; build_standard_builder('Mix ')->(%params); } @@ -98,14 +97,14 @@ my $black = { name => 'black', }; -my $black_9prt = { %$black, amount => 9 }; +my $black_9prt = { %$black, amount => 0.9 }; my $white = { hex => '#ffffff', name => 'white', }; -my $white_1prt = { %$white, amount => 1 }; +my $white_1prt = { %$white, amount => 0.1 }; my $grey = { hex => '#7f7f7f', @@ -170,7 +169,6 @@ my $tc_mix_black_white = build_test('mix', my $tc_mix_black_white_prt_9_1 = build_test('mix', input_colors => [$black_9prt, $white_1prt], result_color => $black_9_white_1, - amount_type => 'part', ); my $tc_mix_pink_blue = build_test('mix',