| 1 | |||||||||||||||||||||||||||||||||||||||||||
| 2 | # translate color names to values and vice versa | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Graphics::Toolkit::Color::Name; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use v5.12; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Graphics::Toolkit::Color::Name::Scheme; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Graphics::Toolkit::Color::Space::Util qw/uniq round_decimals/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | #### public API ######################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub all { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | my (@scheme_names) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | push @scheme_names, 'default' unless @scheme_names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my @names = (); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | for my $scheme_name (@scheme_names) { | |||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my $scheme = try_get_scheme( $scheme_name ); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | next unless ref $scheme; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | push @names, $scheme->all_names; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | return uniq( @names ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 77 | ▶ | sub get_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_values: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 77 | my ($color_name, $scheme_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 77 | ▶ | ($scheme_name, $color_name) = split(':', $color_name, 2) if index($color_name, ':') > -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 77 | my $scheme = try_get_scheme( $scheme_name ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 77 | ▶ | return $scheme unless ref $scheme; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 72 | return $scheme->values_from_name( $color_name ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 547 | ▶ | sub from_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_values: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 547 | my ($values, $scheme_name, $all_names, $full_name, $distance) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 547 | my @return_names = (); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 547 | ▶ | my @scheme_names = (ref $scheme_name eq 'ARRAY') ? (@$scheme_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | : (defined $scheme_name) ? $scheme_name : 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 547 | for my $scheme_name (@scheme_names) { | |||||||||||||||||||||||||||||||||||||||||
| 37 | 551 | my $scheme = try_get_scheme( $scheme_name ); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 551 | ▶ | next unless ref $scheme; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 551 | ▶ | my $names = $distance ? $scheme->names_in_range( $values, $distance ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | : $scheme->names_from_values( $values ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 551 | ▶ | next unless ref $names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 160 | ▶ | $names = [ map { uc($scheme_name).':'.$_} @$names] if $full_name and uc($scheme_name) ne 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$full_name and uc $scheme_name ne "DEFAULT"
MC/DC100%2/2
$full_name and uc $scheme_name ne "DEFAULT"
$full_name
uc $scheme_name ne "DEFAULT"
Truth table100%3/3
A and B
A= $full_nameB= uc $scheme_name ne "DEFAULT"
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 160 | push @return_names, @$names; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 547 | ▶ | push @return_names, '' unless @return_names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 547 | @return_names = uniq( @return_names ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 547 | ▶ | return (defined $all_names and $all_names) ? @return_names : $return_names[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $all_names and $all_names
MC/DC100%2/2
defined $all_names and $all_names
defined $all_names
$all_names
Truth table100%3/3
A and B
A= defined $all_namesB= $all_names
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | ▶ | sub closest_from_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
closest_from_values: called CC=15 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 17 | my ($values, $scheme_name, $all_names, $full_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | # exact search first | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 17 | my @return_names = from_values( $values, $scheme_name, $all_names, $full_name ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 17 | ▶ | return ((@return_names == 1) ? $return_names[0] : \@return_names, 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@return_names == 1 and $return_names[0] eq ""
MC/DC100%2/2
@return_names == 1 and $return_names[0] eq ""
@return_names == 1
$return_names[0] eq ""
Truth table100%3/3
A and B
A= @return_names == 1B= $return_names[0] eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | unless @return_names == 1 and $return_names[0] eq ''; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | my @scheme_names = (ref $scheme_name eq 'ARRAY') ? (@$scheme_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | : (defined $scheme_name) ? $scheme_name : 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | @return_names = (); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | my $distance = 'Inf'; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | for my $scheme_name (@scheme_names) { | |||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | my $scheme = try_get_scheme( $scheme_name ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | next unless ref $scheme; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | my ($names, $d) = $scheme->closest_names_from_values( $values ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | $d = round_decimals($d, 5); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | next unless ref $names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | ▶ | next unless $d <= $distance; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | $distance = $d; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $names = [ map { uc($scheme_name).':'.$_} @$names] if $full_name and uc($scheme_name) ne 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$full_name and uc $scheme_name ne "DEFAULT"
MC/DC0%0/2
$full_name and uc $scheme_name ne "DEFAULT"
$full_name
uc $scheme_name ne "DEFAULT"
Truth table33%1/3
A and B
A= $full_nameB= uc $scheme_name ne "DEFAULT"
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | @return_names = ($distance == $d) ? (@return_names, @$names) : (@$names); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | @return_names = uniq( @return_names ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | my $name = (defined $all_names and $all_names) ? \@return_names : $return_names[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $all_names and $all_names
MC/DC100%2/2
defined $all_names and $all_names
defined $all_names
$all_names
Truth table100%3/3
A and B
A= defined $all_namesB= $all_names
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | return ($name, $distance); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | #### color scheme API ################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # load default scheme on RUNTIME | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 11 | my %color_scheme = (DEFAULT => Graphics::Toolkit::Color::Name::Scheme->new()); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 11 | my $default_names = require Graphics::Toolkit::Color::Name::Constant; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 11 | for my $color_block (@$default_names){ | |||||||||||||||||||||||||||||||||||||||||
| 82 | 44 | $color_scheme{'DEFAULT'}->add_color( $_, [ @{$color_block->{$_}}[0,1,2] ] ) for keys %$color_block; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 641 | ▶ | sub try_get_scheme { # auto loader | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
try_get_scheme: called CC=7 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 641 | ▶ | my $scheme_name = shift // 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // "DEFAULT"
MC/DC100%1/1
shift() // "DEFAULT"
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 641 | $scheme_name = uc $scheme_name; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 641 | ▶ | unless (exists $color_scheme{ $scheme_name }){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | my $module_base = 'Graphics::ColorNames'; | |||||||||||||||||||||||||||||||||||||||||
| 90 | # eval "use $module_base"; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # return "$module_base is not installed, but it's needed to load external color schemes!" if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 6 | my $module = $module_base.'::'.$scheme_name; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | eval "use $module"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | ▶ | return "Perl module $module is not installed, but needed to load color scheme '$scheme_name'" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $palette = eval $module.'::NamesRgbTable();'; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return "Could not use Perl module $module , it seems to be damaged!" if $@ or ref $palette ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ or ref $palette ne "HASH"
MC/DC0%0/2
$@ or ref $palette ne "HASH"
$@
ref $palette ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $palette ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $scheme = Graphics::Toolkit::Color::Name::Scheme->new(); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $scheme->add_color( $_, from_hex_to_rgb_tuple( $palette->{$_} ) ) for keys %$palette; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | add_scheme( $scheme, $scheme_name ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 635 | return $color_scheme{ $scheme_name }; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub add_scheme { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_scheme: called CC=4 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | my ($scheme, $scheme_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | return if ref $scheme ne 'Graphics::Toolkit::Color::Name::Scheme' | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | or not defined $scheme_name or exists $color_scheme{ $scheme_name }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name or exists $color_scheme{$scheme_name}
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name
MC/DC0%0/3
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name or exists $color_scheme{$scheme_name}
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme"
not defined $scheme_name
exists $color_scheme{$scheme_name}
Truth table25%1/4
A or not(B) or C
A= ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme"B= not defined $scheme_nameC= exists $color_scheme{$scheme_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | $color_scheme{ uc $scheme_name } = $scheme; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 11 | my $rgb_max = 256; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub from_hex_to_rgb_tuple { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_hex_to_rgb_tuple: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $hex = shift; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $rg = int $hex / $rgb_max; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return [ int $rg / $rgb_max, $rg % $rgb_max, $hex % $rgb_max]; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||