| 1 | package Imager::Color::Float; | ||||||||||||||||||||||||||||
| 2 | 79 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 79 | ▶ | use Imager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 79 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 79 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 79 | our $VERSION = "1.008"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # It's just a front end to the XS creation functions. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 19 | ▶ | sub _rgb_alpha { | ||||||||||||||||||||||||||
|
Subroutine
_rgb_alpha: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 12 | 19 | my ($alpha) = @_; | |||||||||||||||||||||||||||
| 13 | 19 | ▶ | if ($alpha =~ /^(.*)%\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | return $1 / 100; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | else { | ||||||||||||||||||||||||||||
| 17 | 16 | return $alpha; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 79 | my $rgb_key = qr/rgba?/; | |||||||||||||||||||||||||||
| 22 | 79 | my $rgb_samp = qr/(\d+(?:\.\d*)?)/; | |||||||||||||||||||||||||||
| 23 | 79 | my $rgb_pc = qr/(\d+(?:\.\d*)?)%/; | |||||||||||||||||||||||||||
| 24 | 79 | my $rgb_sep = qr/ *[, ] */; | |||||||||||||||||||||||||||
| 25 | 79 | my $rgb_rgb = qr/$rgb_samp $rgb_sep $rgb_samp $rgb_sep $rgb_samp/x; | |||||||||||||||||||||||||||
| 26 | 79 | my $rgb_rgb_pc = qr/$rgb_pc $rgb_sep $rgb_pc $rgb_sep $rgb_pc/x; | |||||||||||||||||||||||||||
| 27 | 79 | my $rgb_alpha_sep = qr/ *[\/,] */; | |||||||||||||||||||||||||||
| 28 | 79 | my $rgb_alpha = qr/((?:\.\d+|\d+(?:\.\d*)?)%?)/; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Parse color spec into an a set of 4 colors | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 169 | ▶ | sub _pspec { | ||||||||||||||||||||||||||
|
Subroutine
_pspec: called CC=14 SCAR=26.5
| |||||||||||||||||||||||||||||
| 33 | 169 | ▶ | if (@_ == 1 && Scalar::Util::blessed($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and builtin::blessed($_[0])
MC/DC100%2/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1
builtin::blessed($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if ($_[0]->isa("Imager::Color::Float")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | return $_[0]->rgba; | |||||||||||||||||||||||||||
| 36 | } elsif ($_[0]->isa("Imager::Color")) { | ||||||||||||||||||||||||||||
| 37 | 4 | return $_[0]->as_float->rgba; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 165 | ▶ | return (@_,1) if @_ == 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 109 | ▶ | return (@_ ) if @_ == 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 46 | ▶ | if ($_[0] =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { | ||||||||||||||||||||||||||||
| 44 | 1 | return (hex($1)/255,hex($2)/255,hex($3)/255,hex($4)/255); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 45 | ▶ | if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 3 | return (hex($1)/255,hex($2)/255,hex($3)/255,1); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 42 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | # CSS Color 4 says that color values are rounded to +Inf | ||||||||||||||||||||||||||||
| 51 | 42 | ▶ | if ($_[0] =~ /\A$rgb_key\( *$rgb_rgb *\)\z/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 52 | 9 | return ( $1 / 255, $2 / 255, $3 / 255, 1.0 ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc *\)\z/) { | ||||||||||||||||||||||||||||
| 55 | 12 | return ( $1 / 100, $2 / 100, $3 / 100, 1.0 ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb$rgb_alpha_sep$rgb_alpha *\)\z/) { | ||||||||||||||||||||||||||||
| 58 | 9 | return ( $1 / 255, $2 / 255, $3 / 255, _rgb_alpha($4) ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc$rgb_alpha_sep$rgb_alpha *\)\z/) { | ||||||||||||||||||||||||||||
| 61 | 10 | return ( $1 / 100, $2 / 100, $3 / 100, _rgb_alpha($4) ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | return (); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 167 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 167 | shift; # get rid of class name. | |||||||||||||||||||||||||||
| 70 | 167 | my @arg = _pspec(@_); | |||||||||||||||||||||||||||
| 71 | 167 | ▶ | return @arg ? new_internal($arg[0],$arg[1],$arg[2],$arg[3]) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 2 | my @arg = _pspec(@_); | |||||||||||||||||||||||||||
| 77 | 2 | ▶ | return @arg ? set_internal($self, $arg[0],$arg[1],$arg[2],$arg[3]) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub CLONE_SKIP { 1 } | ||||||||||||||||||||||||||
|
Subroutine
CLONE_SKIP: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub as_8bit { | ||||||||||||||||||||||||||
|
Subroutine
as_8bit: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | my @out; | |||||||||||||||||||||||||||
| 86 | 4 | for my $s ($self->rgba) { | |||||||||||||||||||||||||||
| 87 | 16 | my $result = 0+sprintf("%.f", $s * 255); | |||||||||||||||||||||||||||
| 88 | 16 | ▶ | $result = $result < 0 ? 0 : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | $result > 255 ? 255 : | ||||||||||||||||||||||||||||
| 90 | $result; | ||||||||||||||||||||||||||||
| 91 | 16 | push @out, $result; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | return Imager::Color->new(@out); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 24 | ▶ | sub as_css_rgb { | ||||||||||||||||||||||||||
|
Subroutine
as_css_rgb: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 24 | my (@rgb) = $self->rgba; | |||||||||||||||||||||||||||
| 101 | 24 | my $alpha = pop @rgb; | |||||||||||||||||||||||||||
| 102 | # check if they're all representable as byte type samples | ||||||||||||||||||||||||||||
| 103 | 24 | my $can_byte = 1; | |||||||||||||||||||||||||||
| 104 | 24 | for my $s (@rgb) { | |||||||||||||||||||||||||||
| 105 | 46 | ▶ | if (abs(sprintf("%.0f", $s * 255) - $s*255) > 0.0001) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 16 | $can_byte = 0; | |||||||||||||||||||||||||||
| 107 | 16 | last; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 24 | ▶ | if ($alpha == 1.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 13 | ▶ | if ($can_byte) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 4 | return sprintf("rgb(%.0f, %.0f, %.0f)", map { 255 * $_ } @rgb); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | # avoid outputting 2 decimals unless the precision is needed | ||||||||||||||||||||||||||||
| 117 | 9 | my ($rpc, $gpc, $bpc) = map { 0 + sprintf("%.2f", 100 * $_) } @rgb; | |||||||||||||||||||||||||||
| 118 | 9 | return "rgb($rpc% $gpc% $bpc%)"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||
| 122 | 11 | my $apf = 0+sprintf("%.4f", $alpha); | |||||||||||||||||||||||||||
| 123 | 11 | ▶ | if ($can_byte) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 4 | return sprintf("rgba(%.0f, %.0f, %.0f, %s)", ( map { 255 * $_ } @rgb ), $apf); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||
| 127 | # avoid outputting 2 decimals unless the precision is needed | ||||||||||||||||||||||||||||
| 128 | 7 | my ($rpc, $gpc, $bpc) = map { 0 + sprintf("%.2f", 100 * $_) } @rgb; | |||||||||||||||||||||||||||
| 129 | 7 | return "rgba($rpc% $gpc% $bpc% / $apf)"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 79 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||