@@ -6,11 +6,9 @@ use warnings;
6
6
package Graphics::Toolkit::Color::Space::Format ;
7
7
8
8
sub new {
9
- my ($pkg , $basis , $shape , $ suffix ) = @_ ;
9
+ my ($pkg , $basis , $suffix ) = @_ ;
10
10
return ' first argument has to be an Color::Space::Basis reference'
11
11
unless ref $basis eq ' Graphics::Toolkit::Color::Space::Basis' ;
12
- return ' second argument (axis names) has to be an Color::Space::Shape reference'
13
- unless ref $shape eq ' Graphics::Toolkit::Color::Space::Shape' ;
14
12
15
13
my $count = $basis -> count;
16
14
$suffix = [(' ' ) x $count ] unless defined $suffix ;
@@ -21,23 +19,22 @@ sub new {
21
19
# format --> tuple
22
20
my %deformats = ( hash => sub { $basis -> tuple_from_hash(@_ ) if $basis -> is_hash(@_ ) },
23
21
named_array => sub { [ @{$_ [0]}[1 .. $# {$_ [0]}] ] if is_named_array(@_ ) },
24
- string => sub { tuple_from_string(@_ ) if is_string (@_ ) },
22
+ string => sub { tuple_from_string(@_ ) if is_named_string (@_ ) },
25
23
css_string => sub { tuple_from_css(@_ ) if is_css_string(@_ ) },
26
24
);
27
25
# tuple --> format
28
26
my %formats = (list => sub { @$_ }, # 1, 2, 3
29
27
hash => sub { $basis -> long_hash_from_tuple(@_ ) }, # { red => 1, green => 2, blue => 3 }
30
28
char_hash => sub { $basis -> short_hash_from_tuple(@_ ) }, # { r =>1, g => 2, b => 3 }
31
29
array => sub { [$basis -> space_name, @$_ ] }, # ['rgb',1,2,3]
32
- string => sub { named_string_from_list (@_ ) }, # 'rgb: 1, 2, 3'
33
- css_string => sub { css_string_from_list (@_ ) }, # 'rgb(1,2,3)'
30
+ string => sub { named_string_from_tuple (@_ ) }, # 'rgb: 1, 2, 3'
31
+ css_string => sub { css_string_from_tuple (@_ ) }, # 'rgb(1,2,3)'
34
32
);
35
- bless { basis => $basis , shape => $shape , suffix => $suffix , format => \%formats , deformat => \%deformats , }
33
+ bless { basis => $basis , suffix => $suffix , format => \%formats , deformat => \%deformats , }
36
34
}
37
35
38
36
# #######################################################################
39
37
sub basis { $_ [0]{' basis' }}
40
-
41
38
sub has_format { (defined $_ [1] and exists $_ [0]{' format' }{ lc $_ [1] }) ? 1 : 0 }
42
39
sub has_deformat { (defined $_ [1] and exists $_ [0]{' deformat' }{ lc $_ [1] }) ? 1 : 0 }
43
40
sub add_formatter {
@@ -55,17 +52,18 @@ sub add_deformatter {
55
52
# #######################################################################
56
53
57
54
sub format {
58
- my ($self , $values , $format ) = @_ ;
55
+ my ($self , $values , $format , $suffix ) = @_ ;
59
56
return unless $self -> basis-> is_value_tuple( $values );
57
+ $values = self-> add_suffix($values , $suffix );
60
58
$self -> {' format' }{ lc $format }-> (@$values ) if $self -> has_format( $format );
61
59
}
62
60
63
61
sub deformat {
64
- my ($self , $color ) = @_ ;
62
+ my ($self , $color , $suffix ) = @_ ;
65
63
return undef unless defined $color ;
66
64
for my $deformatter (values %{$self -> {' deformat' }}){
67
65
my $values = $deformatter -> ( $color );
68
- return $values if $self -> basis-> is_value_tuple( $values );
66
+ return self -> remove_suffix( $values , $suffix ) if $self -> basis-> is_value_tuple( $values );
69
67
}
70
68
return undef ;
71
69
}
@@ -86,99 +84,65 @@ sub remove_suffix {
86
84
$suffix = [($suffix ) x $self -> basis-> count] unless ref $suffix ;
87
85
[ map { ($self -> {' suffix' }[$_ ] and
88
86
substr ( $values -> [$_ ], - length ($self -> {' suffix' }[$_ ])) eq $self -> {' suffix' }[$_ ])
89
- ? (substr ( $values -> [$_ ], 0, length ($values -> [$_ ]) - length ($self -> {' suffix' }[$_ ]))) : $values -> [$_ ] } $self -> iterator ];
87
+ ? (substr ( $values -> [$_ ], 0, length ($values -> [$_ ]) - length ($self -> {' suffix' }[$_ ]))) : $values -> [$_ ] } $self -> basis -> iterator ];
90
88
}
91
89
92
90
# #######################################################################
93
91
94
- # #######################################################################
95
-
96
- sub is_string { #
92
+ sub is_named_string { #
97
93
my ($self , $string ) = @_ ;
98
94
return 0 unless defined $string and not ref $string ;
99
95
$string = lc $string ;
100
- my $name = lc $self -> name ;
96
+ my $name = lc $self -> basis -> space_name ;
101
97
return 0 unless index ($string , $name .' :' ) == 0;
102
98
my $nr = ' \s*-?\d+(?:\.\d+)?\s*' ;
103
- my $nrs = join (' ,' , (' \s*-?\d+(?:\.\d+)?\s*' ) x $self -> count);
99
+ my $nrs = join (' ,' , (' \s*-?\d+(?:\.\d+)?\s*' ) x $self -> basis -> count);
104
100
($string =~ / ^$name :$nrs $ / ) ? 1 : 0;
105
101
}
106
102
sub is_css_string {
107
103
my ($self , $string ) = @_ ;
108
104
return 0 unless defined $string and not ref $string ;
109
105
$string = lc $string ;
110
- my $name = lc $self -> name ;
106
+ my $name = lc $self -> basis -> space_name ;
111
107
return 0 unless index ($string , $name .' (' ) == 0;
112
108
my $nr = ' \s*-?\d+(?:\.\d+)?\s*' ;
113
- my $nrs = join (' ,' , (' \s*-?\d+(?:\.\d+)?\s*' ) x $self -> count);
109
+ my $nrs = join (' ,' , (' \s*-?\d+(?:\.\d+)?\s*' ) x $self -> basis -> count);
114
110
($string =~ / ^$name \( $nrs \) $ / ) ? 1 : 0;
115
111
}
116
112
sub is_named_array {
117
113
my ($self , $value_array ) = @_ ;
118
- (ref $value_array eq ' ARRAY' and @$value_array == ($self -> {' count' }+1)
119
- and uc $value_array -> [0] eq uc $self -> name) ? 1 : 0;
120
- }
121
-
122
- # #######################################################################
123
-
124
- # #######################################################################
125
-
126
- sub list_from_hash {
127
- my ($self , $value_hash ) = @_ ;
128
- return undef unless ref $value_hash eq ' HASH' and CORE::keys %$value_hash == $self -> {' count' };
129
- my @values = (0) x $self -> {' count' };
130
- for my $value_key (CORE::keys %$value_hash ) {
131
- if ($self -> is_key( $value_key )) { $values [ $self -> {' key_order' }{ lc $value_key } ] = $value_hash -> { $value_key } }
132
- elsif ($self -> is_shortcut( $value_key )) { $values [ $self -> {' shortcut_order' }{ lc $value_key } ] = $value_hash -> { $value_key } }
133
- else { return }
134
- }
135
- return @values ;
136
- }
137
-
138
- sub deformat_partial_hash {
139
- my ($self , $value_hash ) = @_ ;
140
- return unless ref $value_hash eq ' HASH' ;
141
- my @keys_got = CORE::keys %$value_hash ;
142
- return unless @keys_got and @keys_got <= $self -> {' count' };
143
- my $result = {};
144
- for my $key (@keys_got ) {
145
- if ($self -> is_key( $key )) { $result -> { int $self -> key_pos( $key ) } = $value_hash -> { $key } }
146
- elsif ($self -> is_shortcut( $key )){ $result -> { int $self -> shortcut_pos( $key ) } = $value_hash -> { $key } }
147
- else { return undef }
148
- }
149
- return $result ;
114
+ (ref $value_array eq ' ARRAY' and @$value_array == ($self -> basis-> count+1)
115
+ and uc $value_array -> [0] eq uc $self -> basis-> space_name) ? 1 : 0;
150
116
}
151
117
152
118
# #######################################################################
153
119
154
- sub list_from_string {
120
+ sub tuple_from_string {
155
121
my ($self , $string ) = @_ ;
156
122
my @parts = split (/ :/ , $string );
157
- return split (/ ,/ , $parts [1]);
123
+ return [ split (/ ,/ , $parts [1])] ;
158
124
}
159
125
160
- sub list_from_css {
126
+ sub tuple_from_css {
161
127
my ($self , $string ) = @_ ;
162
128
1 until chop ($string ) eq ' )' ;
163
129
my @parts = split (/ \( / , $string );
164
- return split (/ ,/ , $parts [1]);
130
+ return [ split (/ ,/ , $parts [1])] ;
165
131
}
166
132
167
- sub named_array_from_list {
168
- my ($self , @ values ) = @_ ;
169
- return [lc $self -> name , @values ] if @values == $self -> { ' count ' } ;
133
+ sub named_array_from_tuple {
134
+ my ($self , $ values ) = @_ ;
135
+ return [$self -> basis -> space_name , @$ values ] unless $self -> basis -> is_value_tuple( $values ) ;
170
136
}
171
137
172
- sub named_string_from_list {
173
- my ($self , @values ) = @_ ;
174
- return unless @values == $self -> {' count' };
175
- lc ( $self -> name).' : ' .join (' , ' , @values );
138
+ sub named_string_from_tuple {
139
+ my ($self , $values ) = @_ ;
140
+ lc ( $self -> basis-> space_name).' : ' .join (' , ' , @$values );
176
141
}
177
142
178
- sub css_string_from_list {
179
- my ($self , @values ) = @_ ;
180
- return unless @values == $self -> {' count' };
181
- lc ( $self -> name).' (' .join (' ,' , @values ).' )' ;
143
+ sub css_string_from_tuple {
144
+ my ($self , $values ) = @_ ;
145
+ lc ( $self -> basis-> space_name).' (' .join (' ,' , @$values ).' )' ;
182
146
}
183
147
184
148
1;
0 commit comments