Add a preset enum to ColorTable

You can still select presets through strings (and we leave ourselves
open to add more presets through strings than enumerating with the
enum), but this provides a way to select a preset that is verified by
the compiler.
This commit is contained in:
Kenneth Moreland 2018-04-02 11:08:43 -06:00
parent 97a245597f
commit cb8a05c71b
4 changed files with 95 additions and 16 deletions

@ -31,10 +31,26 @@ namespace cont
namespace detail
{
bool loadColorTablePreset(vtkm::cont::ColorTable::Preset preset, vtkm::cont::ColorTable& table);
std::set<std::string> GetPresetNames();
bool loadColorTablePreset(std::string name, vtkm::cont::ColorTable& table);
}
//----------------------------------------------------------------------------
ColorTable::ColorTable(vtkm::cont::ColorTable::Preset preset)
: Impl(std::make_shared<detail::ColorTableInternals>())
{
const bool loaded = this->LoadPreset(preset);
if (!loaded)
{ //if we failed to load the requested color table, call SetColorSpace
//so that the internal host side cache is constructed and we leave
//the constructor in a valid state. We use RGB as it is the default
//when the no parameter constructor is called
this->SetColorSpace(ColorSpace::LAB);
}
this->AddSegmentAlpha(this->Impl->TableRange.Min, 1.0f, this->Impl->TableRange.Max, 1.0f);
}
//----------------------------------------------------------------------------
ColorTable::ColorTable(const std::string& name)
: Impl(std::make_shared<detail::ColorTableInternals>())
@ -88,6 +104,12 @@ ColorTable::~ColorTable()
{
}
//----------------------------------------------------------------------------
bool ColorTable::LoadPreset(vtkm::cont::ColorTable::Preset preset)
{
return detail::loadColorTablePreset(preset, *this);
}
//----------------------------------------------------------------------------
std::set<std::string> ColorTable::GetPresets() const
{

@ -108,6 +108,35 @@ class VTKM_CONT_EXPORT ColorTable
std::shared_ptr<detail::ColorTableInternals> Impl;
public:
// Note: these are in flux and will change soon.
enum struct Preset
{
DEFAULT,
COOL_TO_WARM,
BLACK_BODY_RADIATION,
SAMSEL_FIRE,
INFERNO,
LINEAR_YGB,
COLD_AND_HOT,
RAINBOW_DESATURATED,
COOL_TO_WARN_EXTENDED,
X_RAY,
BLACK_BLUE_AND_WHITE,
VIRDIS,
LINEAR_GREEN,
JET,
RAINBOW
};
/// \brief Construct a color table from a preset
///
/// Constructs a color table from a given preset, which might include a NaN color.
/// The alpha table will have 2 entries of alpha = 1.0 with linear interpolation
///
/// Note: these are a select set of the presets you can get by providing a string identifier.
///
ColorTable(vtkm::cont::ColorTable::Preset preset);
/// \brief Construct a color table from a preset color table
///
/// Constructs a color table from a given preset, which might include a NaN color.
@ -166,8 +195,13 @@ public:
~ColorTable();
bool LoadPreset(vtkm::cont::ColorTable::Preset preset);
/// Returns the name of all preset color tables
///
/// This list will include all presets defined in vtkm::cont::ColorTable::Preset and could
/// include extras as well.
///
std::set<std::string> GetPresets() const;
/// Load a preset color table

@ -548,26 +548,34 @@ struct LoadColorTablePresetCommand
{
using FunctionType = void (*)(vtkm::cont::ColorTable& table);
vtkm::cont::ColorTable::Preset id;
std::string name;
FunctionType function;
};
constexpr int numberOfPresets = 14;
constexpr int numberOfPresets = 15;
struct LoadColorTablePresetCommand presets[numberOfPresets] = {
{ "cool to warm", loadCoolToWarm },
{ "black-body radiation", loadBlackBody },
{ "samsel fire", loadSamselFire },
{ "inferno", loadInferno },
{ "linear ygb", loadLinearYGB },
{ "cold and hot", loadColdAndHot },
{ "rainbow desaturated", loadRainbowDesaturated },
{ "cool to warm (extended)", loadCoolToWarmExtended },
{ "x ray", loadXRay },
{ "black, blue and white", loadBlackBlueWhite },
{ "virdis", loadVirdis },
{ "linear green", loadLinearGreen },
{ "jet", loadJet },
{ "rainbow", loadRainbow },
{ vtkm::cont::ColorTable::Preset::DEFAULT, "default", loadCoolToWarm },
{ vtkm::cont::ColorTable::Preset::COOL_TO_WARM, "cool to warm", loadCoolToWarm },
{ vtkm::cont::ColorTable::Preset::BLACK_BODY_RADIATION, "black-body radiation", loadBlackBody },
{ vtkm::cont::ColorTable::Preset::SAMSEL_FIRE, "samsel fire", loadSamselFire },
{ vtkm::cont::ColorTable::Preset::INFERNO, "inferno", loadInferno },
{ vtkm::cont::ColorTable::Preset::LINEAR_YGB, "linear ygb", loadLinearYGB },
{ vtkm::cont::ColorTable::Preset::COLD_AND_HOT, "cold and hot", loadColdAndHot },
{ vtkm::cont::ColorTable::Preset::RAINBOW_DESATURATED,
"rainbow desaturated",
loadRainbowDesaturated },
{ vtkm::cont::ColorTable::Preset::COOL_TO_WARN_EXTENDED,
"cool to warm (extended)",
loadCoolToWarmExtended },
{ vtkm::cont::ColorTable::Preset::X_RAY, "x ray", loadXRay },
{ vtkm::cont::ColorTable::Preset::BLACK_BLUE_AND_WHITE,
"black, blue and white",
loadBlackBlueWhite },
{ vtkm::cont::ColorTable::Preset::VIRDIS, "virdis", loadVirdis },
{ vtkm::cont::ColorTable::Preset::LINEAR_GREEN, "linear green", loadLinearGreen },
{ vtkm::cont::ColorTable::Preset::JET, "jet", loadJet },
{ vtkm::cont::ColorTable::Preset::RAINBOW, "rainbow", loadRainbow },
};
}
@ -577,6 +585,21 @@ namespace cont
{
namespace detail
{
VTKM_CONT_EXPORT
bool loadColorTablePreset(vtkm::cont::ColorTable::Preset preset, vtkm::cont::ColorTable& table)
{
for (int i = 0; i < numberOfPresets; ++i)
{
if (preset == presets[i].id)
{
presets[i].function(table);
return true;
}
}
return false;
}
VTKM_CONT_EXPORT std::set<std::string> GetPresetNames()
{
std::set<std::string> names;

@ -33,7 +33,7 @@ void TestFieldToColors()
//build a color table with clamping off and verify that sampling works
vtkm::Range range{ 0.0, 50.0 };
vtkm::cont::ColorTable table;
table.LoadPreset("Cool to Warm");
table.LoadPreset(vtkm::cont::ColorTable::Preset::COOL_TO_WARM);
table.RescaleToRange(range);
table.SetClampingOff();
table.SetAboveRangeColor(vtkm::Vec<float, 3>{ 1.0f, 0.0f, 0.0f }); //red