Vastly increase number of supported (named) colors

From about ~100-200 to upwards of 10,000 (at the cost of a little
speed).
master
Ben Moon 2016-07-05 18:55:32 +01:00
parent 5c00057dd3
commit ee5e5e7e51
2 changed files with 55 additions and 13 deletions

View File

@ -27,12 +27,21 @@ triggers any => @color_words, @mix_words, @opposite_words;
# Color Constants #
#####################
my %colors = map { $_ => '#' . lc Color::Library->color($_)->hex }
Color::Library->WWW->names;
# Favored dictionaries should be closer to the end (those with preferable
# names).
my @dicts = Color::Library->dictionaries(qw(
x11 nbs_iscc nbs_iscc::a nbs_iscc::b nbs_iscc::f nbs_iscc::h
nbs_iscc::m nbs_iscc::p nbs_iscc::r nbs_iscc::rc nbs_iscc::s
nbs_iscc::sc nbs_iscc::tc netscape windows ie vaccc html mozilla svg
www
));
my @dict_colors = map { $_->colors } @dicts;
my %colors = map { lc $_->title => $_, lc $_->name => $_ } @dict_colors;
my @color_descs = sort { length $b <=> length $a } keys %colors;
my @color_names = sort keys %colors;
my %hex_to_name = map { $colors{$_} => $_ } @color_names;
my $color_name_re = '(?:' . (join '|', @color_names) . ')';
my %hex_to_color = map { $_->html => $_ } @dict_colors;
my $color_name_re = '(?:' .
(join '|', map { quotemeta $_ } @color_descs) . ')';
my $scolor = 'colou?rs?';
my $color_re = "(?:$color_name_re|#?\\p{XDigit}{6})";
@ -41,6 +50,9 @@ my %stops = (%StopWords, %{getStopWords('en')});
my @stopwords = keys %stops;
my $stop_re = '(?:' . (join '|', map { quotemeta $_ } @stopwords) . ')';
my $black = Color::Library->color('black');
my $white = Color::Library->color('white');
#############
# Helpers #
#############
@ -53,15 +65,16 @@ sub normalize_color {
}
sub common_name {
return $hex_to_name{$_[0]} // '';
my $color = $hex_to_color{$_[0]} or return '';
return $color->name;
}
sub normalize_colors_for_template {
my @colors = @_;
map { {
hex => $_,
name => common_name($_),
} } map { normalize_color($_) } @colors;
map { ref $_ eq 'Color::Library::Color' ? {
hex => $_->html,
name => $_->name,
} : { hex => $_, name => common_name($_) } } map { normalize_color($_) } @colors;
}
sub normalize_color_for_template {
@ -114,8 +127,8 @@ 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 $c1 = normalize_color($cap{c1} // $black);
my $c2 = normalize_color($cap{c2} // $white);
my %data = (
subtitle_prefix => 'Random color between ',
input_colors => [normalize_colors_for_template($c1, $c2)],

View File

@ -95,7 +95,7 @@ my $white = {
my $grey = {
hex => '#7f7f7f',
name => '',
name => 'grey50',
};
my $pink = {
@ -108,6 +108,11 @@ my $blue = {
name => 'blue',
};
my $blue1 = {
hex => '#0000ff',
name => 'blue1',
};
my $orange = {
hex => '#ffa500',
name => 'orange',
@ -128,6 +133,16 @@ my $yellow = {
name => 'yellow',
};
my $dark_spring_yellow = {
hex => '#669900',
name => 'darkspringyellow',
};
my $light_violet_blue = {
hex => '#9966ff',
name => 'lightvioletblue',
};
my $tc_mix_black_white = build_test('mix',
input_colors => [$black, $white],
result_color => $grey,
@ -161,6 +176,16 @@ my $tc_opp_blue = build_test('reverse',
result_color => $yellow,
);
my $tc_opp_blue1 = build_test('reverse',
input_colors => [$blue1],
result_color => $yellow,
);
my $tc_opp_dsy = build_test('reverse',
input_colors => [$dark_spring_yellow],
result_color => $light_violet_blue,
);
ddg_goodie_test(
[qw( DDG::Goodie::RgbColor )],
# Random colors
@ -188,6 +213,10 @@ ddg_goodie_test(
'complementary color of white' => $tc_opp_white,
'complement white' => $tc_opp_white,
'opposite color for white' => $tc_opp_white,
# Advanced colors (non-WWW)
'opposite of darkspringyellow' => $tc_opp_dsy,
'opposite of dark spring-yellow' => $tc_opp_dsy,
'opposite of blue1' => $tc_opp_blue1,
# 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,