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   1042 use base 'PDF::Builder::Resource::ColorSpace';
  2         4  
  2         508  
4              
5 2     2   13 use strict;
  2         4  
  2         40  
6 2     2   10 use warnings;
  2         4  
  2         193  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10              
11 2     2   13 use PDF::Builder::Basic::PDF::Utils;
  2         3  
  2         202  
12 2     2   14 use PDF::Builder::Util;
  2         3  
  2         415  
13 2     2   14 use Scalar::Util qw(weaken);
  2         3  
  2         1155  
14              
15             =head1 NAME
16              
17             PDF::Builder::Resource::ColorSpace::Indexed - Base colorspace support for indexed color models
18              
19             Inherits from L<PDF::Builder::Resource::ColorSpace>
20              
21             =head2 new
22              
23             PDF::Builder::Resource::ColorSpace::Indexed->new($pdf, $key, %opts)
24              
25             =over
26              
27             Create a new Indexed ColorSpace object.
28              
29             =back
30              
31             =cut
32              
33             sub new {
34 1     1 1 3 my ($class, $pdf, $key, %opts) = @_;
35              
36 1 50       5 $class = ref($class) if ref($class);
37 1         7 my $self = $class->SUPER::new($pdf, $key, %opts);
38 1 50       4 $pdf->new_obj($self) unless $self->is_obj($pdf);
39 1         4 $self->{' apipdf'} = $pdf;
40 1         2 weaken $self->{' apipdf'};
41              
42 1         6 $self->add_elements(PDFName('Indexed'));
43 1         6 $self->type('Indexed');
44              
45 1         6 return $self;
46             }
47              
48             # unknown -- not used anywhere
49              
50             sub enumColors {
51 0     0 0   my $self = shift;
52              
53 0           my %col;
54 0           my $stream = $self->{' csd'}->{' stream'};
55 0           foreach my $n (0..255) {
56 0           my $k = '#' . uc(unpack('H*', substr($stream, $n*3, 3)));
57 0   0       $col{$k} //= $n;
58             }
59 0           return %col;
60             }
61              
62             # unknown -- not used anywhere
63              
64             sub nameColor {
65 0     0 0   my ($self, $n) = @_;
66              
67 0           my %col;
68 0           my $stream = $self->{' csd'}->{' stream'};
69 0           my $k = '#' . uc(unpack('H*', substr($stream, $n*3, 3)));
70 0           return $k;
71             }
72              
73             # unknown -- not used anywhere
74              
75             sub resolveNearestRGB {
76 0     0 0   my $self = shift;
77 0           my ($r, $g, $b) = @_; # need to be in 0-255
78              
79 0           my $c = 0;
80 0           my $w = 768**2;
81 0           my $stream = $self->{' csd'}->{' stream'};
82 0           foreach my $n (0..255) {
83 0           my @e = unpack('C*', substr($stream, $n*3, 3));
84 0           my $d = ($e[0]-$r)**2 + ($e[1]-$g)**2 + ($e[2]-$b)**2;
85 0 0         if ($d < $w) {
86 0           $c = $n;
87 0           $w = $d;
88             }
89             }
90 0           return $c;
91             }
92              
93             1;