Skip to content

Commit 129e2eb

Browse files
authored
Merge pull request #344 from GLVis/palette-cli
Palette cli
2 parents 5801460 + 9bab0ab commit 129e2eb

File tree

12 files changed

+348
-146
lines changed

12 files changed

+348
-146
lines changed

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ Unlike previous GLVis releases, this version requires a C++17 compiler.
2929
* Fixed setting of padding digits for data collections.
3030
* Fixed cycling of the vector-to-scalar function in 2D with raw data.
3131

32+
- Added the option to choose palettes by name using the command line argument
33+
'-pname' or '--palette-name'. This option is also available in scripts and
34+
streams using the keyword 'palette_name'.
35+
36+
- Color palettes defined in an external file can now also be specified in
37+
scripts and streams using the keyword 'palette_file'.
38+
3239
Version 4.4 released on May 1, 2025
3340
===================================
3441

glvis.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ int main (int argc, char *argv[])
377377
const char *stream_file = string_none;
378378
const char *script_file = string_none;
379379
const char *palette_file = string_none;
380+
const char *palette_name = string_none;
380381
const char *window_title = string_default;
381382
const char *font_name = string_default;
382383
int portnum = 19916;
@@ -431,6 +432,8 @@ int main (int argc, char *argv[])
431432
"Run a GLVis script file.");
432433
args.AddOption(&palette_file, "-pal", "--palettes",
433434
"Palette file.");
435+
args.AddOption(&palette_name, "-pname", "--palette-name",
436+
"Palette name.");
434437
args.AddOption(&arg_keys, "-k", "--keys",
435438
"Execute key shortcut commands in the GLVis window.");
436439
args.AddOption(&win.data_state.fix_elem_orient, "-fo", "--fix-orientations",
@@ -592,6 +595,11 @@ int main (int argc, char *argv[])
592595
BasePalettes.Load(palette_file);
593596
}
594597

598+
if (palette_name != string_none)
599+
{
600+
BasePalettes.SetDefault(palette_name);
601+
}
602+
595603
GLVisGeometryRefiner.SetType(geom_ref_type);
596604

597605
string data_type;

lib/palettes.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,41 @@ void PaletteState::GenerateAlphaTexture(float matAlpha, float matAlphaCenter)
165165

166166
void PaletteState::SetIndex(int num)
167167
{
168-
curr_palette = num;
169-
cout << "Palette: " << num << ") " << Palettes->Get(curr_palette)->name << endl;
168+
if ((num >= 0) && (num < Palettes->NumPalettes()))
169+
{
170+
curr_palette = num;
171+
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
172+
}
173+
else
174+
{
175+
cout << "Palette index " << num+1 << " is out of bounds." << endl;
176+
}
177+
}
178+
179+
void PaletteState::SetByName(const std::string& palette_name)
180+
{
181+
int num = Palettes->GetIndexByName(palette_name);
182+
if ((num >= 0) && (num < Palettes->NumPalettes()))
183+
{
184+
curr_palette = num;
185+
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
186+
}
187+
else
188+
{
189+
cout << "Palette " << palette_name << " is not defined." << endl;
190+
}
191+
}
192+
193+
bool PaletteState::UseDefaultIndex()
194+
{
195+
int num = Palettes->GetDefault();
196+
if ((num >= 0) && (num < Palettes->NumPalettes()))
197+
{
198+
curr_palette = num;
199+
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
200+
return true;
201+
}
202+
return false;
170203
}
171204

172205
void PaletteState::NextIndex()

lib/palettes.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class PaletteState
2929
int GetSmoothSetting() { return use_smooth; }
3030
/// Sets the palette texture to bind.
3131
void SetIndex(int num);
32+
void SetByName(const std::string& palette_name);
33+
/// Use the default index if set
34+
bool UseDefaultIndex();
35+
/// If default index is not set, sets to this index.
36+
/// Used for setting recommend palettes
37+
void SetFallbackIndex(int idx) { if (!UseDefaultIndex()) { SetIndex(idx); } };
3238
int GetCurrIndex() const { return curr_palette; }
3339
void NextIndex();
3440
void PrevIndex();

lib/palettes_base.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void RGBAf::Print(ostream& os) const
2727
}
2828

2929
template <size_t N>
30-
Palette::Palette(const string& name,
30+
Palette::Palette(const std::string& name,
3131
const array<array<float,3>,N>& arr) : name(name)
3232
{
3333
colors.resize(N);
@@ -38,7 +38,7 @@ Palette::Palette(const string& name,
3838
}
3939

4040
template <size_t N>
41-
Palette::Palette(const string& name,
41+
Palette::Palette(const std::string& name,
4242
const array<array<float,4>,N>& arr) : name(name)
4343
{
4444
colors.resize(N);
@@ -320,7 +320,7 @@ void Texture::Generate()
320320
}
321321
}
322322

323-
int PaletteRegistry::GetIndexByName(const string& name) const
323+
int PaletteRegistry::GetIndexByName(const std::string& name) const
324324
{
325325
for (int i = 0; i < NumPalettes(); i++)
326326
{
@@ -348,15 +348,15 @@ void PaletteRegistry::AddPalette(Palette& palette)
348348
}
349349
}
350350

351-
void PaletteRegistry::AddPalette(const string& name)
351+
void PaletteRegistry::AddPalette(const std::string& name)
352352
{
353353
if (IsNameUnique(name))
354354
{
355355
palettes.push_back(as_unique<Palette>(name));
356356
}
357357
}
358358

359-
bool PaletteRegistry::IsNameUnique(const string& name) const
359+
bool PaletteRegistry::IsNameUnique(const std::string& name) const
360360
{
361361
// palette name is unique || container is empty
362362
if (GetIndexByName(name) == -1 || palettes.empty())
@@ -382,7 +382,7 @@ Palette* PaletteRegistry::Get(int index) const
382382
return palettes.back().get();
383383
}
384384

385-
Palette* PaletteRegistry::Get(const string& name) const
385+
Palette* PaletteRegistry::Get(const std::string& name) const
386386
{
387387
int idx = GetIndexByName(name);
388388
if (idx != -1)
@@ -395,6 +395,23 @@ Palette* PaletteRegistry::Get(const string& name) const
395395
return palettes.back().get();
396396
}
397397

398+
void PaletteRegistry::SetDefault(const std::string& name)
399+
{
400+
const int idx = GetIndexByName(name);
401+
if (idx < 0)
402+
{
403+
cout << "Palette (name = " << name << ") not found. Available palettes:"
404+
<< endl;
405+
PrintSummary();
406+
}
407+
else
408+
{
409+
default_palette = idx;
410+
cout << "Default palette set to: " << default_palette << ") "
411+
<< Get(default_palette)->name << endl;
412+
}
413+
}
414+
398415
void PaletteRegistry::PrintSummary(ostream& os) const
399416
{
400417
for (int i = 0; i < NumPalettes(); i++)
@@ -417,15 +434,15 @@ void PaletteRegistry::PrintAll(ostream& os) const
417434
}
418435
}
419436

420-
void PaletteRegistry::Load(const string& palette_filename)
437+
void PaletteRegistry::Load(const std::string& palette_filename)
421438
{
422439
ifstream pfile(palette_filename);
423440
if (!pfile)
424441
{
425442
cout << "Could not open palette file: " << palette_filename << endl;
426443
return;
427444
}
428-
string word, palname, channeltype;
445+
std::string word, palname, channeltype;
429446
int idx = -1;
430447

431448
// read initializing commands

lib/palettes_base.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ class PaletteRegistry
171171
private:
172172
std::vector<std::unique_ptr<Palette>> palettes;
173173

174-
/// Find the index of a palette by name
175-
int GetIndexByName(const std::string& name) const;
174+
int default_palette = -1; // index of the default palette
176175

177176
public:
178177
/// Empty constructor
@@ -197,6 +196,15 @@ class PaletteRegistry
197196
/// Get a palette pointer by name; if not found, returns last palette
198197
Palette* Get(const std::string& name) const;
199198

199+
/// Find the index of a palette by name
200+
int GetIndexByName(const std::string& name) const;
201+
202+
/// Set the index of the default palette by name
203+
void SetDefault(const std::string& name);
204+
205+
/// Get the index of the default palette
206+
int GetDefault() const { return default_palette; };
207+
200208
/// Prints a summary (index + name) of all palettes
201209
void PrintSummary(std::ostream& os = std::cout) const;
202210

lib/script_controller.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ enum class Command
5050
Window,
5151
Keys,
5252
Palette,
53+
PaletteFile,
54+
PaletteName,
5355
PaletteRepeat,
5456
ToggleAttributes,
5557
Rotmat,
@@ -111,6 +113,8 @@ ScriptCommands::ScriptCommands()
111113
(*this)[Command::Window] = {"window", "<x> <y> <w> <h>", "Set the position and size of the window."};
112114
(*this)[Command::Keys] = {"keys", "<keys>", "Send the control key sequence."};
113115
(*this)[Command::Palette] = {"palette", "<index>", "Set the palette index."};
116+
(*this)[Command::PaletteFile] = {"palette_file", "<filename>", "Load in a palette file."};
117+
(*this)[Command::PaletteName] = {"palette_name", "<palette_name>", "Use palette with given name."};
114118
(*this)[Command::PaletteRepeat] = {"palette_repeat", "<times>", "Set the repetition of the palette."};
115119
(*this)[Command::ToggleAttributes] = {"toggle_attributes", "<1/0> [[<1/0>] ...];", "Toggle visibility of the attributes."};
116120
(*this)[Command::Rotmat] = {"rotmat", "<[0,0]> <[1,0]> ... <[3,3]>", "Set the rotation matrix."};
@@ -725,6 +729,27 @@ void ScriptController::ExecuteScriptCommand()
725729
win.vs->palette.SetIndex(pal-1);
726730
MyExpose();
727731
}
732+
break;
733+
case Command::PaletteFile:
734+
{
735+
std::string palette_file;
736+
scr >> ws;
737+
std::getline(scr, palette_file);
738+
cout << "Script: palette_file: " << palette_file << endl;
739+
BasePalettes.Load(palette_file);
740+
win.vs->palette.GenerateTextures(true); // need to reinitialize
741+
MyExpose();
742+
}
743+
break;
744+
case Command::PaletteName:
745+
{
746+
std::string palette_name;
747+
scr >> palette_name;
748+
cout << "Script: palette_name: " << palette_name << endl;
749+
win.vs->palette.SetByName(palette_name);
750+
MyExpose();
751+
}
752+
break;
728753
case Command::PaletteRepeat:
729754
{
730755
int rpt_times;

0 commit comments

Comments
 (0)