Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ Unlike previous GLVis releases, this version requires a C++17 compiler.
* Fixed setting of padding digits for data collections.
* Fixed cycling of the vector-to-scalar function in 2D with raw data.

- Added the option to choose palettes by name using the command line argument
'-pname' or '--palette-name'. This option is also available in scripts and
streams using the keyword 'palette_name'.

- Color palettes defined in an external file can now also be specified in
scripts and streams using the keyword 'palette_file'.

Version 4.4 released on May 1, 2025
===================================

Expand Down
8 changes: 8 additions & 0 deletions glvis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ int main (int argc, char *argv[])
const char *stream_file = string_none;
const char *script_file = string_none;
const char *palette_file = string_none;
const char *palette_name = string_none;
const char *window_title = string_default;
const char *font_name = string_default;
int portnum = 19916;
Expand Down Expand Up @@ -428,6 +429,8 @@ int main (int argc, char *argv[])
"Run a GLVis script file.");
args.AddOption(&palette_file, "-pal", "--palettes",
"Palette file.");
args.AddOption(&palette_name, "-pname", "--palette-name",
"Palette name.");
args.AddOption(&arg_keys, "-k", "--keys",
"Execute key shortcut commands in the GLVis window.");
args.AddOption(&win.data_state.fix_elem_orient, "-fo", "--fix-orientations",
Expand Down Expand Up @@ -589,6 +592,11 @@ int main (int argc, char *argv[])
BasePalettes.Load(palette_file);
}

if (palette_name != string_none)
{
BasePalettes.SetDefault(palette_name);
}

GLVisGeometryRefiner.SetType(geom_ref_type);

string data_type;
Expand Down
37 changes: 35 additions & 2 deletions lib/palettes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,41 @@ void PaletteState::GenerateAlphaTexture(float matAlpha, float matAlphaCenter)

void PaletteState::SetIndex(int num)
{
curr_palette = num;
cout << "Palette: " << num << ") " << Palettes->Get(curr_palette)->name << endl;
if ((num >= 0) && (num < Palettes->NumPalettes()))
{
curr_palette = num;
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
}
else
{
cout << "Palette index " << num+1 << " is out of bounds." << endl;
}
}

void PaletteState::SetByName(const std::string& palette_name)
{
int num = Palettes->GetIndexByName(palette_name);
if ((num >= 0) && (num < Palettes->NumPalettes()))
{
curr_palette = num;
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
}
else
{
cout << "Palette " << palette_name << " is not defined." << endl;
}
}

bool PaletteState::UseDefaultIndex()
{
int num = Palettes->GetDefault();
if ((num >= 0) && (num < Palettes->NumPalettes()))
{
curr_palette = num;
cout << "Palette: " << num+1 << ") " << Palettes->Get(curr_palette)->name << endl;
return true;
}
return false;
}

void PaletteState::NextIndex()
Expand Down
6 changes: 6 additions & 0 deletions lib/palettes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class PaletteState
int GetSmoothSetting() { return use_smooth; }
/// Sets the palette texture to bind.
void SetIndex(int num);
void SetByName(const std::string& palette_name);
/// Use the default index if set
bool UseDefaultIndex();
/// If default index is not set, sets to this index.
/// Used for setting recommend palettes
void SetFallbackIndex(int idx) { if (!UseDefaultIndex()) { SetIndex(idx); } };
int GetCurrIndex() const { return curr_palette; }
void NextIndex();
void PrevIndex();
Expand Down
33 changes: 25 additions & 8 deletions lib/palettes_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void RGBAf::Print(ostream& os) const
}

template <size_t N>
Palette::Palette(const string& name,
Palette::Palette(const std::string& name,
const array<array<float,3>,N>& arr) : name(name)
{
colors.resize(N);
Expand All @@ -35,7 +35,7 @@ Palette::Palette(const string& name,
}

template <size_t N>
Palette::Palette(const string& name,
Palette::Palette(const std::string& name,
const array<array<float,4>,N>& arr) : name(name)
{
colors.resize(N);
Expand Down Expand Up @@ -317,7 +317,7 @@ void Texture::Generate()
}
}

int PaletteRegistry::GetIndexByName(const string& name) const
int PaletteRegistry::GetIndexByName(const std::string& name) const
{
for (int i = 0; i < NumPalettes(); i++)
{
Expand Down Expand Up @@ -345,15 +345,15 @@ void PaletteRegistry::AddPalette(Palette& palette)
}
}

void PaletteRegistry::AddPalette(const string& name)
void PaletteRegistry::AddPalette(const std::string& name)
{
if (IsNameUnique(name))
{
palettes.push_back(as_unique<Palette>(name));
}
}

bool PaletteRegistry::IsNameUnique(const string& name) const
bool PaletteRegistry::IsNameUnique(const std::string& name) const
{
// palette name is unique || container is empty
if (GetIndexByName(name) == -1 || palettes.empty())
Expand All @@ -379,7 +379,7 @@ Palette* PaletteRegistry::Get(int index) const
return palettes.back().get();
}

Palette* PaletteRegistry::Get(const string& name) const
Palette* PaletteRegistry::Get(const std::string& name) const
{
int idx = GetIndexByName(name);
if (idx != -1)
Expand All @@ -392,6 +392,23 @@ Palette* PaletteRegistry::Get(const string& name) const
return palettes.back().get();
}

void PaletteRegistry::SetDefault(const std::string& name)
{
const int idx = GetIndexByName(name);
if (idx < 0)
{
cout << "Palette (name = " << name << ") not found. Available palettes:"
<< endl;
PrintSummary();
}
else
{
default_palette = idx;
cout << "Default palette set to: " << default_palette << ") "
<< Get(default_palette)->name << endl;
}
}

void PaletteRegistry::PrintSummary(ostream& os) const
{
for (int i = 0; i < NumPalettes(); i++)
Expand All @@ -414,15 +431,15 @@ void PaletteRegistry::PrintAll(ostream& os) const
}
}

void PaletteRegistry::Load(const string& palette_filename)
void PaletteRegistry::Load(const std::string& palette_filename)
{
ifstream pfile(palette_filename);
if (!pfile)
{
cout << "Could not open palette file: " << palette_filename << endl;
return;
}
string word, palname, channeltype;
std::string word, palname, channeltype;
int idx = -1;

// read initializing commands
Expand Down
12 changes: 10 additions & 2 deletions lib/palettes_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ class PaletteRegistry
private:
std::vector<std::unique_ptr<Palette>> palettes;

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

public:
/// Empty constructor
Expand All @@ -199,6 +198,15 @@ class PaletteRegistry
/// Get a palette pointer by name; if not found, returns last palette
Palette* Get(const std::string& name) const;

/// Find the index of a palette by name
int GetIndexByName(const std::string& name) const;

/// Set the index of the default palette by name
void SetDefault(const std::string& name);

/// Get the index of the default palette
int GetDefault() const { return default_palette; };

/// Prints a summary (index + name) of all palettes
void PrintSummary(std::ostream& os = std::cout) const;

Expand Down
25 changes: 25 additions & 0 deletions lib/script_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum class Command
Window,
Keys,
Palette,
PaletteFile,
PaletteName,
PaletteRepeat,
ToggleAttributes,
Rotmat,
Expand Down Expand Up @@ -111,6 +113,8 @@ ScriptCommands::ScriptCommands()
(*this)[Command::Window] = {"window", "<x> <y> <w> <h>", "Set the position and size of the window."};
(*this)[Command::Keys] = {"keys", "<keys>", "Send the control key sequence."};
(*this)[Command::Palette] = {"palette", "<index>", "Set the palette index."};
(*this)[Command::PaletteFile] = {"palette_file", "<filename>", "Load in a palette file."};
(*this)[Command::PaletteName] = {"palette_name", "<palette_name>", "Use palette with given name."};
(*this)[Command::PaletteRepeat] = {"palette_repeat", "<times>", "Set the repetition of the palette."};
(*this)[Command::ToggleAttributes] = {"toggle_attributes", "<1/0> [[<1/0>] ...];", "Toggle visibility of the attributes."};
(*this)[Command::Rotmat] = {"rotmat", "<[0,0]> <[1,0]> ... <[3,3]>", "Set the rotation matrix."};
Expand Down Expand Up @@ -725,6 +729,27 @@ void ScriptController::ExecuteScriptCommand()
win.vs->palette.SetIndex(pal-1);
MyExpose();
}
break;
case Command::PaletteFile:
{
std::string palette_file;
scr >> ws;
std::getline(scr, palette_file);
cout << "Script: palette_file: " << palette_file << endl;
BasePalettes.Load(palette_file);
win.vs->palette.GenerateTextures(true); // need to reinitialize
MyExpose();
}
break;
case Command::PaletteName:
{
std::string palette_name;
scr >> palette_name;
cout << "Script: palette_name: " << palette_name << endl;
win.vs->palette.SetByName(palette_name);
MyExpose();
}
break;
case Command::PaletteRepeat:
{
int rpt_times;
Expand Down
Loading
Loading