Color-Library

 view release on metacpan or  search on metacpan

lib/Color/Library.pm  view on Meta::CPAN


use warnings;
use strict;

use Module::Pluggable search_path => 'Color::Library::Dictionary', sub_name => '_load_dictionaries', require => 1;
use Color::Library::Dictionary;
__PACKAGE__->_load_dictionaries;

my %dictionary;
sub _register_dictionary {
    my $self = shift;
    my $dictionary = shift;
    $dictionary{$dictionary->id} = $dictionary;
}


sub dictionary {
    my $self = shift;
    return ($self->dictionaries(shift))[0];
}


sub dictionaries {
    my $self = shift;
    local @_ = keys %dictionary unless @_;
    @_ = map { Color::Library::Dictionary::_parse_id $_ } @_;
    if (wantarray) {
        return map { $_->_singleton } @dictionary{@_};
    }
    else {
        my %_dictionary;
        @_dictionary{@_} = map { $_->_singleton } @dictionary{@_};
        return \%_dictionary;
    }
}


# FUTURE Make this better
my @dictionary_search_order = (qw/svg x11 html ie mozilla netscape windows vaccc nbs-iscc/,
        map { "nbs-iscc-$_" } qw/a b f h m p r rc s sc tc/);

sub color {
    my $self = shift;

    my @colors;

    # Default dictionaries to search, in order
    my @dictionaries = @dictionary_search_order;

    # Can also pass in a default array of dictionary ids to search
    @dictionaries = @{ shift() } if ref $_[0] eq "ARRAY";

    my $query_;
    for my $query (@_) {
        $query_ = $query;
        my @dictionaries = @dictionaries;

        if ($query =~ m/:/) {
            # Looks like the query contains at least one dictionary id

            my ($dictionaries, $name) = split m/:/, $query, 2;
            unless (defined $name) {
                $name = $dictionaries;
                undef $dictionaries
            }
            @dictionaries = split m/,/, $dictionaries if defined $dictionaries;
            $query_ = $name;
        }

        my $color;
        for my $dictionary_id (@dictionaries) {
            next unless my $dictionary = $self->dictionary($dictionary_id);
            last if $color = $dictionary->color($query_);
        }
        push @colors, $color;
    }

    return wantarray ? @colors : $colors[0];
}
*colors = \&color;
*colour = \&color;
*colours = \&color;


1;

__END__
=pod

=head1 NAME

Color::Library - An easy-to-use and comprehensive named-color library

=head1 VERSION

version 0.021

=head1 SYNOPSIS

    use Color::Library;

    # Search for a sea blue color 
    my $seablue = Color::Library->color("seablue");

    # Search for a grey73 in the 'svg' and 'x11' dictionaries only
    my $grey73 = Color::Library->colour([qw/svg x11/] => "grey73");

    # Find a bunch of colors at the same time
    my ($red, $green, $blue) = Color::Library->colors(qw/red green blue/);

    # Fetch the named color "aliceblue" from the SVG dictionary
    my $color = Color::Library->SVG->color("aliceblue");

    # Prints out "aliceblue is #ff08ff"
    print $color->name, "is ", $color, "\n";

    # Get a list of names in the svg dictionary
    my @names = Color::Library->SVG->names;

    # Get a list of colors in the x11 dictionary
    my @colors = Color::Library->dictionary('x11')->colors;

=head1 DESCRIPTION

Color::Library is an easy-to-use and comprehensive named-color dictionary. Currently provides coverage for www (svg, html, css) colors, x11 colors, and more.



( run in 2.419 seconds using v1.01-cache-2.11-cpan-483215c6ad5 )