zeroclickinfo-goodies/lib/DDG/Goodie/ColorPicker.pm

57 lines
1.8 KiB
Perl
Raw Normal View History

2016-07-22 12:53:58 -07:00
package DDG::Goodie::ColorPicker;
2016-07-08 06:13:08 -07:00
# ABSTRACT: Presents a color picker that allows the user to select a color or build a color palette.
use DDG::Goodie;
2016-07-12 08:21:44 -07:00
use Color::Library;
2016-07-08 06:13:08 -07:00
use strict;
use warnings;
2016-07-22 12:53:58 -07:00
zci answer_type => 'color_picker';
2016-07-08 06:13:08 -07:00
zci is_cached => 1;
2016-07-09 06:11:35 -07:00
triggers start => ['color picker', 'colour picker', 'colorpicker', 'colourpicker'];
2016-07-08 12:48:51 -07:00
2016-07-08 06:13:08 -07:00
my $goodie_version = $DDG::GoodieBundle::OpenSourceDuckDuckGo::VERSION // 999;
handle remainder => sub {
2016-07-08 12:48:51 -07:00
my $remainder = $_;
2016-07-08 11:05:51 -07:00
my $color = undef;
2016-07-22 12:53:58 -07:00
my $path = "/share/goodie/color_picker/$goodie_version/";
2016-07-08 11:05:51 -07:00
if($remainder =~ /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/) {
2016-07-09 06:16:56 -07:00
$color = join(',', 'rgb', $1, $2, $3);
2016-07-08 11:05:51 -07:00
}
elsif($remainder =~ /hsv\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/) {
2016-07-09 06:16:56 -07:00
$color = join(',', 'hsv', $1, $2, $3);
2016-07-08 11:05:51 -07:00
}
elsif($remainder =~ /cmyk\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/) {
2016-07-09 06:16:56 -07:00
$color = join(',', 'cmyk', $1, $2, $3, $4);
2016-07-08 11:05:51 -07:00
}
elsif($remainder =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) {
$color = $remainder;
}
2016-07-22 12:53:58 -07:00
elsif($remainder =~ /[a-zA-Z ]+/) {
2016-07-08 13:47:52 -07:00
$remainder =~ s/[ \t]+//g;
2016-07-08 13:51:02 -07:00
$remainder = lc $remainder;
2016-07-12 08:21:44 -07:00
if(defined Color::Library->SVG->color($remainder)) {
$color = Color::Library->SVG->color($remainder)->html;
2016-07-08 13:51:02 -07:00
}
2016-07-08 13:47:52 -07:00
}
2016-07-09 06:16:56 -07:00
return 'Color Picker',
2016-07-08 06:13:08 -07:00
structured_answer => {
data => {
2016-07-08 07:54:56 -07:00
color => $color,
2016-07-09 06:16:56 -07:00
saturation_value_path => "${path}saturation_value_gradient.png",
hue_path => "${path}hue_gradient.png"
2016-07-08 06:13:08 -07:00
},
templates => {
2016-07-19 23:19:18 -07:00
group => 'text',
options => {
2016-07-22 12:53:58 -07:00
content => "DDH.color_picker.content"
2016-07-19 23:19:18 -07:00
}
2016-07-08 06:13:08 -07:00
}
};
};
1;