Loading...
Searching...
No Matches
Palette.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_PALETTE
32#define DRAIN_PALETTE
33
34#include "drain/Dictionary.h"
35#include "drain/util/JSON.h"
36#include "drain/util/StringMapper.h"
37
38#include "Geometry.h"
39#include "TreeSVG.h"
40#include "TreeSLD.h"
41#include "PaletteEntry.h"
42
43
44namespace drain
45{
46
47
48namespace image
49{
50
51
55class Palette : public ImageCodeMap<PaletteEntry> {
56
57public:
58
59 static
60 const SprinterLayout cppLayout2;
61
63 inline
64 Palette() : ImageCodeMap<PaletteEntry>(), refinement(0) {
65 };
66
68
72 Palette(std::initializer_list<std::pair<Variable, PaletteEntry> > inits);
73
89 const value_type & getEntryByCode(const std::string & code, bool lenient=true) const;
90
92 inline
93 const key_type getValueByCode(const std::string & code, bool lenient=true) const {
94 const value_type & entry = getEntryByCode(code, lenient);
95 return entry.first;
96 }
97
98 void reset();
99
101 void addEntry(double min, double red=0.0, double green=0.0, double blue=0.0, const std::string &id="", const std::string & label =""); // alpha?
102
104
107 void load(const std::string & filename, bool flexible = false);
108
110 void loadTXT(std::istream & ifstr);
111
113 void loadJSON(std::istream & ifstr);
114
115 void write(const std::string & filename) const;
116
117 void exportTXT(std::ostream & ostr, char separator='\t', char separator2=0) const;
118
119 //void exportJSON(drain::JSONtree::tree_t & json) const;
120 void exportJSON(drain::JSONtree & json) const;
121
122 // Export formatted
123 void exportFMT(std::ostream & ostr, const std::string & format) const;
124
126 void exportSVGLegend(TreeSVG & svg, bool up = true) const;
127
129 void exportSLD(TreeSLD & sld) const;
130
132
138
141 // void convertJSON(const drain::JSONtree::tree_t & json);
142
143 const ChannelGeometry & getChannels() const {
144 update();
145 return channels;
146 }
147
149 std::string title;
150
152 // std::string id; ?
153
155 typedef std::map<std::string,PaletteEntry> spec_t;
156 spec_t specialCodes; // To be read from palette
157
159 void refine(size_t n=0);
160
161 // typedef drain::Dictionary<key_t, std::string> dict_t;
162
164
166
169 size_t refinement;
170
171 // Metadata - for example, the file path of a loaded palette.
172 std::string comment;
173
174protected:
175
177 // dict_t dictionary; // deprecating!
178
179
180 void update() const;
181
183 void importJSON(const drain::JSONtree & json);
184
185 mutable
186 ChannelGeometry channels;
187
188
189};
190
191
192inline
193std::ostream & operator<<(std::ostream &ostr, const Palette & p){
194 p.exportTXT(ostr);
195 return ostr;
196}
197
198
199
200
201class PaletteMap : public std::map<std::string,drain::image::Palette> {
202
203public:
204
205 inline
206 Palette & get(const std::string & key){
207 if (aliases.hasKey(key)){
208 return (*this)[aliases.getValue(key)];
209 }
210 else {
211 return (*this)[key];
212 }
213 }
214
215 inline
216 void addAlias(const std::string & key, const std::string & aliasKey){
217 aliases.add(aliasKey,key);
218 }
219
220// protected:
221
223 // std::map<std::string,std::string> aliasMap;
224
225};
226
227} // image::
228
229template <>
230std::ostream & drain::Sprinter::toStream(std::ostream & ostr, const drain::image::Palette & map, const drain::SprinterLayout & layout);
231
232} // drain::
233
234
235#endif
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
Definition DataSelector.cpp:1277
Definition Sprinter.h:136