package DDG::Goodie::ColorCodes; # ABSTRACT: Copious information about various ways of encoding colors. use strict; use DDG::Goodie; use Color::Library; use Color::Mix; use Convert::Color; use Convert::Color::Library; use Convert::Color::RGB8; use Math::Round; use Try::Tiny; my %types = ( # hash of keyword => Convert::Color prefix rgb => 'rgb8', hex => 'rgb8', html => 'rgb8', css => 'rgb8', color => 'rgb8', hsl => 'hsl', hsv => 'hsv', cmy => 'cmy', cmyk => 'cmyk', cmyb => 'cmyk', ); # Eliminate NBS_ISCC sub-dictionaries from our lookups. # They contain "idiosyncratic" color names (including 'email' in NBS_ISCC::M) which will # otherwise cause this to return answers for which no one was looking. my $color_dictionaries = join(',', grep { $_ !~ /^nbs-iscc-/ } map { $_->id } Color::Library->dictionaries); my $typestr = join '|', sort { length $b <=> length $a } keys %types; $typestr =~ s/([#\^\$\*\+\?])/\\$1/g; triggers query_raw => qr/^ (?:what(?:\si|'?)s \s* (?:the)? \s+)? # what's the, whats the, what is the, what's, what is, whats (?:(inverse|negative|opposite)\s+(?:of)?)? (?: (.*?)\s*(.+?)\bcolou?r(?:\s+code)?| # handles "rgb red color code", "red rgb color code", etc (.*?)\s*(.+?)\brgb(?:\s+code)?| # handles "red rgb code", etc (.*?)\s*colou?r(?:\s+code)?(?:\s+for)?\s+(.+?)| # handles "rgb color code for red", "red color code for html", etc (.*?)(rgba)\s*:?\s*\(?\s*(.+?)\s*\)?| # handles "rgba( red )", "rgba:255,0,0", "rgba(255 0 0)", etc ([^\s]*?)\s*($typestr)\s*:?\s*\(?\s*(.+?)\s*\)?| # handles "rgb( red )", "rgb:255,0,0", "rgb(255 0 0)", etc \#?([0-9a-f]{6})|\#([0-9a-f]{3}) # handles #00f, #0000ff, etc ) (?:(?:'?s)?\s+(inverse|negative|opposite))? (?:\sto\s(?:$typestr))? $/ix; zci is_cached => 1; zci answer_type => 'color_code'; primary_example_queries 'hex color code for cyan'; secondary_example_queries 'rgb(173,216,230)', 'hsl 194 0.53 0.79', 'cmyk(0.12, 0, 0, 0)', '#00ff00'; description 'get hex, RGB, HSL and CMYB values for a color'; name 'ColorCodes'; code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ColorCodes.pm'; category 'conversions'; topics 'programming'; attribution cpan => 'CRZEDPSYC', github => ['http://github.com/mintsoft', 'Rob Emery']; my %trigger_invert = map { $_ => 1 } (qw( inverse negative opposite )); my %trigger_filler = map { $_ => 1 } (qw( code )); my $color_mix = Color::Mix->new; sub percentify { my @out; push @out, ($_ <= 1 ? round(($_ * 100))."%" : round($_)) for @_; return @out; } sub create_output { my (%input) = @_; my ($text, $html) = ("",""); (my $hex_for_links = $input{'hex'}) =~ s/^#//; my $hex = "Hex: ".uc($input{'hex'}); my $rgb = "RGBA(" . join(", ", @{$input{'rgb'}}) . ", ".$input{'alpha'}.")"; my $rgb_pct = "RGB(" . join(", ", @{$input{'rgb_percentage'}}) . ")"; my $hsl = "HSL(" . join(", ", @{$input{'hsl'}}) . ")"; my $cmyb = "CMYB(" . join(", ", @{$input{'cmyb'}}) . ")"; my @analogous_colors = @{$input{'analogous'}}; my $complementary = uc $input{'complementary'}; $text = "$hex ~ $rgb ~ $rgb_pct ~ $hsl ~ $cmyb"."\n" . "Complementary: #$complementary\n" . "Analogous: ".(join ", ", map { "#".uc $_ } @analogous_colors); my $comps = "
Complementary #:
" . qq[$complementary] . "
Analogous #:
" . (join ", ", map { qq[].(uc $_).'' } @analogous_colors) . "