6#include <vtkObjectFactory.h>
20 for (
auto& it :
_dict)
30 return static_cast<unsigned char>(a * (1 - p) + b * p);
38 assert(gamma > 0 && gamma < 4);
39 return static_cast<unsigned char>((b - a) * pow(p, gamma) + a);
45 this->GetTableRange(range);
46 const double interval = range[1] - range[0];
47 this->SetNumberOfTableValues(
static_cast<vtkIdType
>(ceil(interval)) + 1);
53 unsigned char startcolor[4] = {0, 0, 0, 0};
54 std::pair<std::size_t, unsigned char*> lastValue(0, startcolor);
56 for (
auto& it :
_dict)
58 double val = (it.first < range[0])
60 : ((it.first > range[1]) ? range[1] : it.first);
61 auto const nextIndex =
62 static_cast<std::size_t
>(std::floor(val - range[0]));
66 if (nextIndex - lastValue.first > 0)
68 for (std::size_t i = lastValue.first + 1; i < nextIndex; i++)
70 unsigned char int_rgba[4];
72 (i - lastValue.first) /
73 (
static_cast<double>(nextIndex - lastValue.first));
77 for (std::size_t j = 0; j < 4; j++)
80 (lastValue.second)[j], (it.second)[j], pos);
85 for (std::size_t j = 0; j < 4; j++)
89 (it.second)[j], 0.2, pos);
94 for (std::size_t j = 0; j < 4; j++)
96 int_rgba[j] = (lastValue.second)[j];
104 lastValue.first = nextIndex;
105 lastValue.second = it.second;
110 vtkLookupTable::Build();
117 std::size_t
const n_colors(lut.
size());
118 for (std::size_t i = 0; i < n_colors; ++i)
120 setColor(std::get<0>(lut[i]), std::get<1>(lut[i]));
124 SetTableRange(range.first, range.second);
130 std::stringstream strout;
131 strout <<
"Writing color table to " << filename <<
" ... ";
132 std::ofstream out(filename.c_str(), std::ios::out);
134 std::size_t nColors = this->GetNumberOfTableValues();
135 for (std::size_t i = 0; i < nColors; i++)
137 unsigned char rgba[4];
139 out << i <<
"\t" << rgba[0] <<
"\t" << rgba[1] <<
"\t" << rgba[2]
143 strout <<
" done." << std::endl;
144 INFO(
"{:s}", strout.str());
149 unsigned char rgba[4])
153 for (
unsigned i = 0; i < 4; ++i)
155 value[i] = rgba[i] / 255.0;
157 vtkLookupTable::SetTableValue(idx, value);
163 vtkLookupTable::GetTableValue(idx, value);
165 for (
unsigned i = 0; i < 4; ++i)
167 rgba[i] =
static_cast<unsigned char>(value[i] * 255.0);
174 auto* dict_rgba =
new unsigned char[4];
175 for (std::size_t i = 0; i < 4; i++)
177 dict_rgba[i] = color[i];
179 _dict.insert(std::pair<double, unsigned char*>(pos, dict_rgba));
184 index = ((index < this->TableRange[0])
185 ?
static_cast<vtkIdType
>(this->TableRange[0])
186 : (index >= this->TableRange[1]
187 ?
static_cast<vtkIdType
>(this->TableRange[1]) - 1
189 index =
static_cast<std::size_t
>(std::floor(
190 (index - this->TableRange[0]) *
191 (this->NumberOfColors / (this->TableRange[1] - this->TableRange[0]))));
193 unsigned char* _rgba;
194 _rgba = this->Table->GetPointer(index * 4);
195 for (std::size_t i = 0; i < 4; i++)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
vtkStandardNewMacro(VtkColorLookupTable)
DataHolderLib::LUTType getInterpolationType() const
std::pair< double, double > getTableRange() const
Calculates and stores a colour lookup table.
~VtkColorLookupTable() override
Destructor.
DataHolderLib::LUTType _type
void setColor(double pos, DataHolderLib::Color const &color)
unsigned char expInterpolation(unsigned char a, unsigned char b, double gamma, double p) const
Interpolates values exponentially. gamma should roughly be in [0,4), for gamma=1 interpolation is lin...
void writeToFile(const std::string &filename)
Exports a color table to a file.
std::map< double, unsigned char * > _dict
void SetTableValueRGBA(vtkIdType idx, unsigned char rgba[4])
Set a value within the LUT.
void Build() override
Builds the colour table based on the previously set parameters. This method should only be called aft...
unsigned char linInterpolation(unsigned char a, unsigned char b, double p) const
Interpolates values linearly.
void GetTableValue(vtkIdType idx, unsigned char rgba[4])
Get a value from the LUT.
VtkColorLookupTable()
Constructor.
void setInterpolationType(DataHolderLib::LUTType type)
Sets the type of interpolation.
void getColor(vtkIdType index, unsigned char rgba[4]) const
void setLookupTable(DataHolderLib::ColorLookupTable const &lut)
Imports settings of OGS lookup table class.
std::array< unsigned char, 4 > Color