]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gen-color-table.pl
8b02fe583659b4a46948dc0c8bd4623e983d5592
[~andy/gtk] / gdk-pixbuf / gen-color-table.pl
1 #!/usr/bin/perl -w
2
3 if (@ARGV != 1) {
4     die "Usage: gen-color-table.pl rgb.txt > xpm-color-table.h\n";
5 }
6
7 open IN, $ARGV[0] || die "Cannot open $ARGV[0]: $!\n";
8
9 @colors = ();
10 while (defined($_ = <IN>)) {
11     next if /^!/;
12     if (!/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+(.*\S)\s+$/) {
13         die "Cannot parse line $_";
14     }
15
16     push @colors, [$1, $2, $3, $4];
17 }
18
19 @colors = sort { lc($a->[3]) cmp lc($b->[3]) } @colors;
20
21 $offset = 0;
22
23 $date = gmtime;
24
25 print <<EOT;
26 /* xpm-color-table.h: Generated by gen-color-table.pl from rgb.txt
27  *
28  *  Date: $date
29  *
30  * Do not edit.   
31  */
32 static const char color_names[] =
33 EOT
34
35 for $color (@colors) {
36     $name = $color->[3];
37
38     if ($offset != 0) {
39         print qq(\n);
40     }
41     print qq(  "$name\\0");
42
43     $color->[4] = $offset;
44     $offset += length($name) + 1;
45 }
46
47 print ";\n\n";
48
49 print <<EOT;
50 typedef struct {
51     guint16 name_offset;
52     guchar red;
53     guchar green;
54     guchar blue;
55 } XPMColorEntry;
56
57 static const XPMColorEntry xColors[] = {
58 EOT
59
60 $i = 0;
61 for $color (@colors) {
62     $red = $color->[0];
63     $green = $color->[1];
64     $blue = $color->[2];
65     $offset = $color->[4];
66
67     if ($i != 0) {
68         print ",\n";
69     }
70     print "  { $offset, $red, $green, $blue }";
71     $i++;
72 }
73
74 print "\n};\n";