| 1 | |||
| 2 | package Text::Editor::Vip::Color::Color ; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings ; | ||
| 6 | |||
| 7 | BEGIN | ||
| 8 | { | ||
| 9 | use Exporter (); | ||
| 10 | use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 11 | $VERSION = '0.01'; | ||
| 12 | @ISA = qw (Exporter); | ||
| 13 | @EXPORT = qw (GetKeywordColors); | ||
| 14 | @EXPORT_OK = qw (); | ||
| 15 | %EXPORT_TAGS = (); | ||
| 16 | } | ||
| 17 | |||
| 18 | =TODO | ||
| 19 | |||
| 20 | tests | ||
| 21 | |||
| 22 | do not use print for output but some Vip error or login func | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | Text::Editor::Vip::Color::Color - Manipulates color files | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Loads color definition files. | ||
| 33 | |||
| 34 | =head1 MEMBER FUNCTIONS | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | #~ use Data::TreeDumper ; | ||
| 39 | |||
| 40 | #------------------------------------------------------------------------------- | ||
| 41 | |||
| 42 | sub GetKeywordColors | ||
| 43 | { | ||
| 44 | =head2 GetKeywordColors | ||
| 45 | |||
| 46 | This function evaluate, possibly multiple, perl files containing one of the following: | ||
| 47 | |||
| 48 | =over 2 | ||
| 49 | |||
| 50 | =item * color and color class definitions | ||
| 51 | |||
| 52 | =item * color, color class and keywords color definitions | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | Color, color classes and keyword colors are defined as hash references. | ||
| 57 | |||
| 58 | my $colors = | ||
| 59 | { | ||
| 60 | # color name color | ||
| 61 | default_fg => 'black' # forward declaration | ||
| 62 | , default_bg => 'white' | ||
| 63 | , margin => [235, 235, 235] # RGB components | ||
| 64 | , dark_red => [110, 0, 0] | ||
| 65 | } ; | ||
| 66 | |||
| 67 | my $color_class = | ||
| 68 | { | ||
| 69 | # colors must be defined (possibly in another file) | ||
| 70 | |||
| 71 | # class color tuple | ||
| 72 | default => ['default_bg','default_fg'] | ||
| 73 | , selection => ['dark_blue', 'white'] | ||
| 74 | , DynaLoader => ['red', 'yellow'] | ||
| 75 | } ; | ||
| 76 | |||
| 77 | my $keyword_color_class = | ||
| 78 | { | ||
| 79 | # keyword color class | ||
| 80 | DynaLoader =>'DynaLoader', | ||
| 81 | , carp =>'Carp', | ||
| 82 | } ; | ||
| 83 | |||
| 84 | # return the color data to Vip | ||
| 85 | return ($color, $color_class, $keyword_color_class) ; | ||
| 86 | |||
| 87 | =head2 Argument | ||
| 88 | |||
| 89 | B<GetKeywordColors> uses named arguments: | ||
| 90 | |||
| 91 | =over 2 | ||
| 92 | |||
| 93 | =item * COLORS, a list of files to load (color, color class) | ||
| 94 | |||
| 95 | =item * COLORS_AND_KEYWORDS, a list of files to load (color, color class, keywords) | ||
| 96 | |||
| 97 | =item * KEYWORDS, a list of files to load (keywords) | ||
| 98 | |||
| 99 | =item * USE_X_COLORS, uses Graphics::ColorNames to define standard names.Spaces are replaced with '_'. | ||
| 100 | |||
| 101 | =item * MAPPING, transform color classes (only PANGO supported) | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | my (%args) = @_ ; | ||
| 108 | |||
| 109 | my (%colors, %color_class, %keywords) ; | ||
| 110 | |||
| 111 | for my $file (@{$args{COLORS}}) | ||
| 112 | { | ||
| 113 | my ($colors, $color_class) = do $file or die ("Couldn't evaluate '$file'\nFile error: $!\nCompilation error: $@\n") ; | ||
| 114 | |||
| 115 | die "color definition invalid (expect hash ref) in file '$file'" unless 'HASH' eq ref $colors ; | ||
| 116 | die "color class definition invalid (expect hash ref) in file '$file'" unless 'HASH' eq ref $color_class ; | ||
| 117 | |||
| 118 | %colors = (%colors, %$colors) ; | ||
| 119 | %color_class = (%color_class, %$color_class); | ||
| 120 | } | ||
| 121 | |||
| 122 | for my $file (@{$args{COLORS_AND_KEYWORDS}}) | ||
| 123 | { | ||
| 124 | my ($colors, $color_class, $keywords) = do $file or die ("Couldn't evaluate '$file'\nFile error: $!\nCompilation error: $@\n") ; | ||
| 125 | |||
| 126 | die "color definition invalid (expect hash ref) in file '$file'" unless 'HASH' eq ref $colors ; | ||
| 127 | die "color class definition invalid (expect hash ref) in file '$file'" unless 'HASH' eq ref $color_class ; | ||
| 128 | die "keywords definition invalid (expect hash ref) in file '$file'" unless 'HASH' eq ref $keywords ; | ||
| 129 | |||
| 130 | %colors = (%colors, %$colors) ; | ||
| 131 | %color_class = (%color_class, %$color_class); | ||
| 132 | %keywords = (%keywords, %$keywords); | ||
| 133 | } | ||
| 134 | |||
| 135 | for my $file (@{$args{KEYWORDS}}) | ||
| 136 | { | ||
| 137 | my ($keywords) = do $file or die ("Couldn't evaluate '$file'\nFile error: $!\nCompilation error: $@\n") ; | ||
| 138 | |||
| 139 | %keywords = (%keywords, %$keywords); | ||
| 140 | } | ||
| 141 | |||
| 142 | if($args{USE_X_COLORS}) | ||
| 143 | { | ||
| 144 | # we replace spaces in color names with '_' | ||
| 145 | |||
| 146 | use Graphics::ColorNames qw(hex2tuple tuple2hex) ; | ||
| 147 | tie my %std_colors, 'Graphics::ColorNames' ; | ||
| 148 | |||
| 149 | # go through std colors and define rgb array if necessary | ||
| 150 | for my $color (keys %std_colors) | ||
| 151 | { | ||
| 152 | (my $color_no_space = $color) =~ s/\s/_/g ; | ||
| 153 | |||
| 154 | if(exists $colors{$color_no_space}) | ||
| 155 | { | ||
| 156 | print "standard color '$color' was overridden!\n" ; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | $colors{$color_no_space} = [hex2tuple($std_colors{$color})] ; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | # handle aliases | ||
| 166 | my $found_alias = 0 ; | ||
| 167 | do | ||
| 168 | { | ||
| 169 | $found_alias = 0 ; | ||
| 170 | for my $color (keys %colors) | ||
| 171 | { | ||
| 172 | if(ref $colors{$color} eq '') | ||
| 173 | { | ||
| 174 | print "color alias $color => $colors{$color}\n" ; | ||
| 175 | if($color eq $colors{$color}) | ||
| 176 | { | ||
| 177 | print "self referencing colors\n" ; | ||
| 178 | $colors{$color} = [0, 0, 0] ; | ||
| 179 | } | ||
| 180 | |||
| 181 | if(exists $colors{$colors{$color}}) | ||
| 182 | { | ||
| 183 | $colors{$color} = $colors{$colors{$color}} ; | ||
| 184 | $found_alias++; | ||
| 185 | } | ||
| 186 | else | ||
| 187 | { | ||
| 188 | print "undefined color alias '$color' => $colors{$color}\n" ; | ||
| 189 | $colors{$color} = [0, 0, 0] ; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | while($found_alias) ; | ||
| 195 | |||
| 196 | #~ print DumpTree \%colors; | ||
| 197 | |||
| 198 | my %missing ; | ||
| 199 | |||
| 200 | for my $class_name (sort keys %color_class) | ||
| 201 | { | ||
| 202 | if(ref $color_class{$class_name} eq 'ARRAY') | ||
| 203 | { | ||
| 204 | for my $color ($color_class{$class_name}[0], $color_class{$class_name}[1]) | ||
| 205 | { | ||
| 206 | if(ref $color eq '') | ||
| 207 | { | ||
| 208 | if(! exists $colors{$color}) | ||
| 209 | { | ||
| 210 | print "color '$color' is not defined\n" unless exists $missing{$color}; | ||
| 211 | $missing{$color}++ ; | ||
| 212 | |||
| 213 | $color = [0, 0, 0] ; | ||
| 214 | } | ||
| 215 | else | ||
| 216 | { | ||
| 217 | $color = $colors{$color} ; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | elsif(ref $color eq 'ARRAY') | ||
| 221 | { | ||
| 222 | # keep and pray it's good :) | ||
| 223 | } | ||
| 224 | else | ||
| 225 | { | ||
| 226 | die "color class '$class_name' definition invalid! use [class_name, class_name]\n" ; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | else | ||
| 231 | { | ||
| 232 | die "color class '$class_name' definition invalid! use [class_name, class_name]\n" ; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | #~ print DumpTree \%color_class; | ||
| 237 | |||
| 238 | if(exists $args{MAPPING}) | ||
| 239 | { | ||
| 240 | if('PANGO' eq uc $args{MAPPING}) | ||
| 241 | { | ||
| 242 | for my $class_name (keys %color_class) | ||
| 243 | { | ||
| 244 | my $pango_color = "<span background='#" ; | ||
| 245 | $pango_color .= tuple2hex(@{$color_class{$class_name}[0]}) ; | ||
| 246 | $pango_color .= "' foreground='#" ; | ||
| 247 | $pango_color .= tuple2hex( @{$color_class{$class_name}[1]}) ; | ||
| 248 | $pango_color .= "'>" ; | ||
| 249 | |||
| 250 | $color_class{$class_name} = $pango_color ; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | for my $keyword (keys %keywords) | ||
| 256 | { | ||
| 257 | if(ref $keywords{$keyword} eq '') | ||
| 258 | { | ||
| 259 | my $color_class_name = $keywords{$keyword} ; | ||
| 260 | |||
| 261 | if(exists $color_class{$color_class_name}) | ||
| 262 | { | ||
| 263 | $keywords{$keyword} = $color_class{$color_class_name} ; | ||
| 264 | } | ||
| 265 | else | ||
| 266 | { | ||
| 267 | print "color class '$color_class_name' for keyword '$keyword' is not defined\n" ; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | else | ||
| 271 | { | ||
| 272 | die "color class for keyword '$keyword' is not valid! keyword => 'color_class'\n" ; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | #~ print DumpTree \%keywords, "keywords" ; | ||
| 277 | |||
| 278 | return(\%colors, \%color_class, \%keywords) ; | ||
| 279 | |||
| 280 | } | ||
| 281 | |||
| 282 | =head1 AUTHOR | ||
| 283 | |||
| 284 | Nadim iibn Hamouda El Khemir | ||
| 285 | <nadim@khemir.net> | ||
| 286 | |||
| 287 | =head1 COPYRIGHT | ||
| 288 | |||
| 289 | This program is free software; you can redistribute | ||
| 290 | it and/or modify it under the same terms as Perl itself. | ||
| 291 | |||
| 292 | The full text of the license can be found in the | ||
| 293 | LICENSE file included with this module. | ||
| 294 | |||
| 295 | =head1 SEE ALSO | ||
| 296 | |||
| 297 | perl(1). | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | #------------------------------------------------------------------------------- | ||
| 302 | 1 ; |