]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/map-sRGBconvert.pl
rename all the configs
[xonotic/xonotic.git] / misc / tools / map-sRGBconvert.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my @defaultkeys = qw/_color _floodlight/;
7
8 my @colorkeys = @ARGV;
9 @colorkeys = '+'
10         if @colorkeys == 0;
11 @colorkeys = map { $_ eq '-' ? map { "-$_" } @defaultkeys : $_ eq '+' ? @defaultkeys : $_ } @colorkeys;
12 print STDERR "Remapping keys: @colorkeys\n";
13
14 sub Image_LinearFloatFromsRGBFloat($) {($_[0] <= 0.04045) ? $_[0] * (1.0 / 12.92) : (($_[0] + 0.055)*(1.0/1.055)) ** 2.4}
15 sub Image_sRGBFloatFromLinearFloat($) {($_[0] < 0.0031308) ? $_[0] * 12.92 : 1.055 * ($_[0] ** (1.0/2.4)) - 0.055}
16
17 while(<STDIN>)
18 {
19         chomp;
20         my $line = $_;
21         if(/^\s*"([^"]*)"\s+"\s*([-+0-9.]+)\s*([-+0-9.]+)\s*([-+0-9.]+)\s*"\s*$/i)
22         {
23                 my $key = $1;
24                 my $r = $2;
25                 my $g = $3;
26                 my $b = $4;
27                 if(grep { /^\+?\Q$key\E$/i } @colorkeys)
28                 {
29                         $r = Image_LinearFloatFromsRGBFloat $r;
30                         $g = Image_LinearFloatFromsRGBFloat $g;
31                         $b = Image_LinearFloatFromsRGBFloat $b;
32                         $line = "\"$key\" \"$r $g $b\"";
33                 }
34                 elsif(grep { /^-\Q$key\E$/i } @colorkeys)
35                 {
36                         $r = Image_sRGBFloatFromLinearFloat $r;
37                         $g = Image_sRGBFloatFromLinearFloat $g;
38                         $b = Image_sRGBFloatFromLinearFloat $b;
39                         $line = "\"$key\" \"$r $g $b\"";
40                 }
41         }
42         elsif(/^\s*"_floodlight"\s+"\s*([-+0-9.]+)\s*([-+0-9.]+)\s*([-+0-9.]+)(\s*.*)"\s*$/i)
43         {
44                 my $r = $1;
45                 my $g = $2;
46                 my $b = $3;
47                 my $rest = $4;
48                 if(grep { /^\+?_floodlight$/i } @colorkeys)
49                 {
50                         $r = 255.0*Image_LinearFloatFromsRGBFloat($r/255.0);
51                         $g = 255.0*Image_LinearFloatFromsRGBFloat($g/255.0);
52                         $b = 255.0*Image_LinearFloatFromsRGBFloat($b/255.0);
53                         $line = "\"_floodlight\" \"$r $g $b$rest\"";
54                 }
55                 elsif(grep { /^-_floodlight$/i } @colorkeys)
56                 {
57                         $r = 255.0*Image_sRGBFloatFromLinearFloat($r/255.0);
58                         $g = 255.0*Image_sRGBFloatFromLinearFloat($g/255.0);
59                         $b = 255.0*Image_sRGBFloatFromLinearFloat($b/255.0);
60                         $line = "\"_floodlight\" \"$r $g $b$rest\"";
61                 }
62         }
63         if($line ne $_)
64         {
65                 print STDERR "Converting: $_ -> $line\n";
66                 $_ = $line;
67         }
68         print "$_\n";
69 }