File Coverage

lib/Data/URIID/Colour.pm
Criterion Covered Total %
statement 37 61 60.6
branch 7 30 23.3
condition 3 24 12.5
subroutine 9 13 69.2
pod 5 6 83.3
total 61 134 45.5


line stmt bran cond sub pod time code
1             # Copyright (c) 2023-2024 Philipp Schafft
2              
3             # licensed under Artistic License 2.0 (see LICENSE file)
4              
5             # ABSTRACT: Extractor for identifiers from URIs
6              
7             package Data::URIID::Colour;
8              
9 5     5   304855 use strict;
  5         10  
  5         207  
10 5     5   24 use warnings;
  5         8  
  5         344  
11              
12 5     5   30 use overload '""' => \&rgb;
  5         7  
  5         46  
13              
14 5     5   371 use Carp;
  5         24  
  5         461  
15 5     5   31 use Scalar::Util qw(weaken blessed);
  5         9  
  5         347  
16              
17             our $VERSION = v0.20;
18              
19 5     5   29 use parent qw(Data::URIID::Base Data::Identifier::Interface::Known);
  5         8  
  5         57  
20              
21             my %_registered;
22              
23              
24              
25             sub new {
26 218     218 1 1997 my ($pkg, %opts) = @_;
27 218         351 my __PACKAGE__ $self;
28              
29 218 50       570 if (defined(my $from = delete($opts{from}))) {
30 0 0       0 if (blessed $from) {
31 0 0       0 if ($from->isa('Data::URIID::Base')) {
32 0   0     0 $opts{extractor} //= $from->extractor(default => undef);
33             }
34              
35 0 0       0 if ($from->isa(__PACKAGE__)) {
    0          
36 0   0     0 $opts{rgb} //= $from->rgb;
37             } elsif ($from->isa('Data::Identifier')) {
38 0 0 0     0 if (!defined($opts{rgb}) && eval {$from->generator->eq('55febcc4-6655-4397-ae3d-2353b5856b34')}) {
  0         0  
39 0 0       0 if (defined(my $v = $from->request)) {
40 0 0       0 if ($v =~ /^#[0-9a-fA-F]{6}$/) {
41 0   0     0 $opts{rgb} //= $v;
42             }
43             }
44             }
45 0         0 $from = $from->ise;
46             } else {
47 0         0 $from = $from->ise;
48             }
49             }
50              
51 0   0     0 $opts{rgb} //= $_registered{$from};
52             }
53              
54 218 50       458 croak 'No RGB value given' unless defined $opts{rgb};
55              
56 218         517 $opts{rgb} = uc($opts{rgb});
57 218 50       1100 $opts{rgb} =~ /^#[0-9A-F]{6}$/ or die 'Bad format';
58              
59 218         595 weaken($opts{extractor});
60              
61 218         574 $self = bless \%opts, $pkg;
62              
63 218 100       492 if (delete $opts{register}) { # not (yet) part of public API
64 216   66     447 $_registered{$self->ise} //= $opts{rgb};
65 216         678 Data::Identifier::Generate->colour($opts{rgb})->register;
66             }
67              
68 218         129288 return $self;
69             }
70              
71              
72             sub rgb {
73 220     220 1 1341 my ($self) = @_;
74 220   33     1080 return $self->{rgb} // croak 'No RGB value';
75             }
76              
77              
78             sub known {
79 0     0 1 0 my ($pkg, $class, %opts) = @_;
80 0 0 0     0 $opts{extractor} //= $pkg->extractor(default => undef) if ref $pkg;
81 0         0 return $pkg->SUPER::known($class, %opts);
82             }
83              
84             # --- Overrides for Data::URIID::Base ---
85              
86             sub ise {
87 508     508 1 872 my ($self, %opts) = @_;
88              
89 508 100       1344 unless (defined $self->{ise}) {
90 218         2394 require Data::Identifier::Generate;
91 218         6868 $self->{ise} = Data::Identifier::Generate->colour($self->rgb)->ise;
92             }
93              
94 508         54758 return $self->SUPER::ise(%opts);
95             }
96              
97             sub displayname {
98 0     0 1   my ($self, %opts) = @_;
99 0           return $self->SUPER::displayname(%opts, _fallback => $self->rgb);
100             }
101              
102             # --- Overrides for Data::URIID::Base ---
103             sub _known_provider {
104 0     0     my ($pkg, $class, %opts) = @_;
105 0 0         croak 'Unsupported options passed' if scalar(keys %opts);
106 0 0         return ([keys %_registered], rawtype => 'ise') if $class eq ':all';
107 0           croak 'Unsupported class';
108             }
109              
110             # ---- Private helpers ----
111              
112             # Private for now.
113             sub displaycolour {
114 0     0 0   my ($self) = @_;
115 0           return $self;
116             }
117              
118             1;
119              
120             __END__
121              
122             =pod
123              
124             =encoding UTF-8
125              
126             =head1 NAME
127              
128             Data::URIID::Colour - Extractor for identifiers from URIs
129              
130             =head1 VERSION
131              
132             version v0.20
133              
134             =head1 SYNOPSIS
135              
136             use Data::URIID::Colour;
137              
138             my $colour = Data::URIID::Colour->new(rgb => '#FF0000');
139              
140             This module represents a single colour.
141              
142             This package inherits from L<Data::URIID::Base>, and L<Data::Identifier::Interface::Known> (experimental).
143              
144             =head1 METHODS
145              
146             =head2 new
147              
148             my $colour = Data::URIID::Colour->new( option => value, ... );
149              
150             Returns a new object for the given colour.
151             The following options are defined:
152              
153             =over
154              
155             =item C<rgb>
156              
157             The RGB value in hex notation. E.g. C<#FF0000>.
158              
159             =item C<extractor>
160              
161             optionally, an instance of L<Data::URIID>.
162              
163             =item C<from>
164              
165             optionally, an instance of any colour provider.
166             The provider might be used to fill defaults for the other options (such as C<rgb> or C<extractor>).
167              
168             Currently the value must be one of
169             L<Data::URIID::Colour>,
170             L<Data::URIID::Result>, or
171             L<Data::Identifier> (only supported for some objects, including those generated with L<Data::Identifier::Generate/colour>).
172             But other types might also be supported.
173              
174             If using L<Data::URIID::Result> this might not be what you want. See also L<Data::URIID::Result/displaycolour>.
175              
176             =back
177              
178             =head2 rgb
179              
180             my $rgb = $colour->rgb;
181              
182             Returns the colour in six digit hex notation with prepended pound (C<#>) if successful or C<die> otherwise.
183             The returned value is suitable for use in CSS.
184              
185             =head2 known
186              
187             my @list = Data::URIID::Colour->known($class [, %opts ]);
188             # or:
189             my @list = $colour->known($class [, %opts ]);
190              
191             (B<experimental>, since v0.17)
192              
193             Returns the list of known objects for the given C<$class>.
194             Currently no specific classes are supported, so the only valid value is C<:all>.
195             See L<Data::Identifier::Interface::Known/known> for details.
196              
197             If called on a instance of this package C<extractor> is filled in automatically if one is known by this instance.
198              
199             B<Note:>
200             This is an experimental feature! It may be removed or altered at any future version!
201              
202             =head1 AUTHOR
203              
204             Philipp Schafft <lion@cpan.org>
205              
206             =head1 COPYRIGHT AND LICENSE
207              
208             This software is Copyright (c) 2023-2025 by Philipp Schafft <lion@cpan.org>.
209              
210             This is free software, licensed under:
211              
212             The Artistic License 2.0 (GPL Compatible)
213              
214             =cut