0
|
1 |
#======================================================================
|
|
2 |
# L I B C P T . P L
|
|
3 |
# doc: Wed Nov 15 12:28:49 2000
|
36
|
4 |
# dlm: Mon May 14 21:29:00 2018
|
0
|
5 |
# (c) 2000 A.M. Thurnherr
|
36
|
6 |
# uE-Info: 75 58 NIL 0 0 72 2 2 4 NIL ofnI
|
0
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# HISTORY:
|
|
10 |
# Nov 15, 2000: - created
|
|
11 |
# May 29, 2001: - made bg/fg numeric
|
|
12 |
# May 31, 2001: - removed dummy bg val from all arrays
|
|
13 |
# Dec 12, 2001: - clarified format errors
|
|
14 |
# Jun 21, 2004: - relaxed cpt file format restrictions
|
|
15 |
# - made cpt into a hash
|
|
16 |
# - totally re-written
|
|
17 |
# Jun 25, 2004: - return good value if $z equal upper cpt table limit
|
|
18 |
# Jun 28, 2004: - added default color model
|
|
19 |
# Jun 30, 2004: - renamed from libGMT.pl to libCPT.pl
|
|
20 |
# Dec 1, 2005: - BUG: roundoff error
|
|
21 |
# Jul 1, 2006: - Version 3.3 [HISTORY]
|
|
22 |
# Jul 24, 2006: - modified to use $PRACTICALLY_ZERONY
|
|
23 |
# Aug 16, 2006: - BUG: last level was returned on value < first level
|
|
24 |
# May 9, 2008: - adapted to GMT 4.3 (see also IMPLEMENTATION NOTES
|
|
25 |
# in [mkCPT])
|
36
|
26 |
# Mar 26, 2018: - BUG: fg colors could not be set?!?!?!?! (both F and B set bg color)
|
|
27 |
# - implemented color scaling for input files with rgb vals 0-1
|
|
28 |
# May 14, 2016: - added input file check
|
0
|
29 |
|
|
30 |
#----------------------------------------------------------------------
|
|
31 |
# CPT File Parsing
|
|
32 |
#----------------------------------------------------------------------
|
|
33 |
|
|
34 |
# NB: %CPT structure assumes RGB --- if the color model is really HSV,
|
|
35 |
# field names are wrong.
|
|
36 |
|
|
37 |
# %CPT
|
|
38 |
# levels number of different color levels
|
|
39 |
# color_model RGB or HSV
|
|
40 |
# @from_z from values (z, RGB) for each level
|
|
41 |
# @from_R
|
|
42 |
# @from_G
|
|
43 |
# @from_B
|
|
44 |
# @to_z to values (z, RGB) for each level
|
|
45 |
# @to_R
|
|
46 |
# @to_G
|
|
47 |
# @to_B
|
|
48 |
# bg_R background vals
|
|
49 |
# bg_G
|
|
50 |
# bg_B
|
|
51 |
# fg_R foreground vals
|
|
52 |
# fg_G
|
|
53 |
# fg_B
|
|
54 |
# nan_R nan vals
|
|
55 |
# nan_G
|
|
56 |
# nan_B
|
|
57 |
|
|
58 |
sub readCPT($)
|
|
59 |
{
|
|
60 |
my($f) = @_;
|
|
61 |
my($flag,%CPT);
|
|
62 |
|
|
63 |
for ($CPT{levels}=0; <$f>;) {
|
|
64 |
$CPT{color_model} = $' if /^# COLOR_MODEL = /; chomp($CPT{color_model});
|
|
65 |
s/#.*//;
|
|
66 |
next if /^\s*$/;
|
|
67 |
my(@f) = split;
|
|
68 |
if ($f[0] eq 'B') {
|
|
69 |
$CPT{bg_R} = $f[1]; $CPT{bg_G} = $f[2]; $CPT{bg_B} = $f[3];
|
|
70 |
} elsif ($f[0] eq 'F') {
|
36
|
71 |
$CPT{fg_R} = $f[1]; $CPT{fg_G} = $f[2]; $CPT{fg_B} = $f[3];
|
0
|
72 |
} elsif ($f[0] eq 'N') {
|
|
73 |
$CPT{nan_R} = $f[1]; $CPT{nan_G} = $f[2]; $CPT{nan_B} = $f[3];
|
|
74 |
} else {
|
36
|
75 |
croak("$0: format error - 7 fields expected on line: $_") unless ($#f >= 7);
|
0
|
76 |
$CPT{from_z}[$CPT{levels}] = $f[0];
|
|
77 |
$CPT{from_R}[$CPT{levels}] = $f[1];
|
|
78 |
$CPT{from_G}[$CPT{levels}] = $f[2];
|
|
79 |
$CPT{from_B}[$CPT{levels}] = $f[3];
|
|
80 |
$CPT{to_z}[$CPT{levels}] = $f[4];
|
|
81 |
$CPT{to_R}[$CPT{levels}] = $f[5];
|
|
82 |
$CPT{to_G}[$CPT{levels}] = $f[6];
|
|
83 |
$CPT{to_B}[$CPT{levels}] = $f[7];
|
|
84 |
$CPT{levels}++;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
$CPT{color_model} = 'RGB' unless defined($CPT{color_model});
|
|
88 |
croak("$0: color model $CPT{color_model} not implemented\n")
|
36
|
89 |
unless ($CPT{color_model} =~ '\+?[Rr][Gg][Bb]' || $CPT{color_model} =~ '\+?[Hh][Ss][Vv]');
|
|
90 |
|
|
91 |
if ($CPT{from_R}[0]>=0 && $CPT{from_R}[0]<=1 && # colors in 0-1 range
|
|
92 |
$CPT{from_G}[0]>=0 && $CPT{from_G}[0]<=1 &&
|
|
93 |
$CPT{from_B}[0]>=0 && $CPT{from_B}[0]<=1 &&
|
|
94 |
$CPT{from_R}[$CPT{levels}-1]>=0 && $CPT{from_R}[$CPT{levels}-1]<=1 &&
|
|
95 |
$CPT{from_G}[$CPT{levels}-1]>=0 && $CPT{from_G}[$CPT{levels}-1]<=1 &&
|
|
96 |
$CPT{from_B}[$CPT{levels}-1]>=0 && $CPT{from_B}[$CPT{levels}-1]<=1) {
|
|
97 |
$CPT{bg_R} = round(255 * $CPT{bg_R}); $CPT{bg_G} = round(255 * $CPT{bg_G}); $CPT{bg_B} = round(255 * $CPT{bg_B});
|
|
98 |
$CPT{fg_R} = round(255 * $CPT{fg_R}); $CPT{fg_G} = round(255 * $CPT{fg_G}); $CPT{fg_B} = round(255 * $CPT{fg_B});
|
|
99 |
$CPT{nan_R} = round(255 * $CPT{nan_R}); $CPT{nan_G} = round(255 * $CPT{nan_G}); $CPT{nan_B} = round(255 * $CPT{nan_B});
|
|
100 |
for (my($i)=0; $i<$CPT{levels}; $i++) {
|
|
101 |
$CPT{from_R}[$i] = round(255 * $CPT{from_R}[$i]);
|
|
102 |
$CPT{from_G}[$i] = round(255 * $CPT{from_G}[$i]);
|
|
103 |
$CPT{from_B}[$i] = round(255 * $CPT{from_B}[$i]);
|
|
104 |
$CPT{to_R}[$i] = round(255 * $CPT{to_R}[$i]);
|
|
105 |
$CPT{to_G}[$i] = round(255 * $CPT{to_G}[$i]);
|
|
106 |
$CPT{to_B}[$i] = round(255 * $CPT{to_B}[$i]);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
0
|
110 |
return %CPT;
|
|
111 |
}
|
|
112 |
|
|
113 |
sub CPTlvl($%)
|
|
114 |
{
|
|
115 |
my($z,%CPT) = @_;
|
|
116 |
my($l);
|
|
117 |
|
|
118 |
croak("$0: no valid CPT info\n")
|
|
119 |
unless ($CPT{levels} > 0);
|
|
120 |
|
|
121 |
return nan if isnan($z);
|
|
122 |
|
|
123 |
for ($l=0; $l<$CPT{levels}; $l++) {
|
|
124 |
return $l if ($CPT{from_z}[$l] <= $z && $z < $CPT{to_z}[$l]);
|
|
125 |
}
|
|
126 |
return $CPT{levels}-1
|
|
127 |
if (abs($z-$CPT{to_z}[$CPT{levels}-1]) < $PRACTICALLY_ZERO);
|
|
128 |
return -1 if ($z < $CPT{from_z}[0]);
|
|
129 |
return $CPT{levels};
|
|
130 |
}
|
|
131 |
|
|
132 |
1;
|