File Coverage

blib/lib/PDF/Builder/Resource/ColorSpace/Indexed.pm
Criterion Covered Total %
statement 27 51 52.9
branch 2 6 33.3
condition 0 3 0.0
subroutine 7 10 70.0
pod 1 4 25.0
total 37 74 50.0


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::ColorSpace::Indexed;
2              
3 2     2   1119 use base 'PDF::Builder::Resource::ColorSpace';
  2         4  
  2         612  
4              
5 2     2   16 use strict;
  2         6  
  2         46  
6 2     2   12 use warnings;
  2         6  
  2         140  
7              
8             our $VERSION = '3.025'; # VERSION
9             our $LAST_UPDATE = '3.024'; # manually update whenever code is changed
10              
11 2     2   19 use PDF::Builder::Basic::PDF::Utils;
  2         6  
  2         186  
12 2     2   13 use PDF::Builder::Util;
  2         8  
  2         291  
13 2     2   16 use Scalar::Util qw(weaken);
  2         7  
  2         1120  
14              
15             =head1 NAME
16              
17             PDF::Builder::Resource::ColorSpace::Indexed - base colorspace support for indexed color models. Inherits from L
18              
19             =cut
20              
21             sub new {
22 1     1 1 3 my ($class, $pdf, $key, %opts) = @_;
23              
24 1 50       4 $class = ref($class) if ref($class);
25 1         5 my $self = $class->SUPER::new($pdf, $key, %opts);
26 1 50       3 $pdf->new_obj($self) unless $self->is_obj($pdf);
27 1         3 $self->{' apipdf'} = $pdf;
28 1         4 weaken $self->{' apipdf'};
29              
30 1         2 $self->add_elements(PDFName('Indexed'));
31 1         5 $self->type('Indexed');
32              
33 1         2 return $self;
34             }
35              
36             sub enumColors {
37 0     0 0   my $self = shift;
38              
39 0           my %col;
40 0           my $stream = $self->{' csd'}->{' stream'};
41 0           foreach my $n (0..255) {
42 0           my $k = '#' . uc(unpack('H*', substr($stream, $n*3, 3)));
43 0   0       $col{$k} //= $n;
44             }
45 0           return %col;
46             }
47              
48             sub nameColor {
49 0     0 0   my ($self, $n) = @_;
50              
51 0           my %col;
52 0           my $stream = $self->{' csd'}->{' stream'};
53 0           my $k = '#' . uc(unpack('H*', substr($stream, $n*3, 3)));
54 0           return $k;
55             }
56              
57             sub resolveNearestRGB {
58 0     0 0   my $self = shift;
59 0           my ($r, $g, $b) = @_; # need to be in 0-255
60              
61 0           my $c = 0;
62 0           my $w = 768**2;
63 0           my $stream = $self->{' csd'}->{' stream'};
64 0           foreach my $n (0..255) {
65 0           my @e = unpack('C*', substr($stream, $n*3, 3));
66 0           my $d = ($e[0]-$r)**2 + ($e[1]-$g)**2 + ($e[2]-$b)**2;
67 0 0         if ($d < $w) {
68 0           $c = $n;
69 0           $w = $d;
70             }
71             }
72 0           return $c;
73             }
74              
75             1;