1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package PDF::API2::Resource::ColorSpace::Indexed;
use base 'PDF::API2::Resource::ColorSpace';
use strict;
use warnings;
our $VERSION = '2.047'; # VERSION
use PDF::API2::Basic::PDF::Utils;
use PDF::API2::Util;
use Scalar::Util qw(weaken);
sub new {
my ($class, $pdf, $key, %opts) = @_;
$class = ref($class) if ref($class);
my $self = $class->SUPER::new($pdf, $key, %opts);
$pdf->new_obj($self) unless $self->is_obj($pdf);
$self->{' apipdf'} = $pdf;
weaken $self->{' apipdf'};
$self->add_elements(PDFName('Indexed'));
$self->type('Indexed');
return $self;
}
sub enumColors {
my $self = shift();
my %col;
my $stream = $self->{' csd'}->{' stream'};
foreach my $n (0..255) {
my $k = '#' . uc(unpack('H*', substr($stream, $n * 3, 3)));
$col{$k} //= $n;
}
return %col;
}
sub nameColor {
my ($self, $n) = @_;
my %col;
my $stream = $self->{' csd'}->{' stream'};
my $k = '#' . uc(unpack('H*', substr($stream, $n * 3, 3)));
return $k;
}
# r, g, b need to be 0-255
sub resolveNearestRGB {
my ($self, $r, $g, $b) = @_;
my $c = 0;
my $w = 768 ** 2;
my $stream = $self->{' csd'}->{' stream'};
foreach my $n (0..255) {
my @e = unpack('C*', substr($stream, $n * 3, 3));
my $d = ($e[0] - $r) ** 2 + ($e[1] - $g) ** 2 + ($e[2] - $b) ** 2;
if ($d < $w) {
$c = $n;
$w = $d;
}
}
return $c;
}
1;
|