17#include <vtkObjectFactory.h>
31 for (
auto& it :
_dict)
41 return static_cast<unsigned char>(a * (1 - p) + b * p);
49 assert(gamma > 0 && gamma < 4);
50 return static_cast<unsigned char>((b - a) * pow(p, gamma) + a);
56 this->GetTableRange(range);
57 const double interval = range[1] - range[0];
58 this->SetNumberOfTableValues(
static_cast<vtkIdType
>(ceil(interval)) + 1);
64 unsigned char startcolor[4] = {0, 0, 0, 0};
65 std::pair<std::size_t, unsigned char*> lastValue(0, startcolor);
67 for (
auto& it :
_dict)
69 double val = (it.first < range[0])
71 : ((it.first > range[1]) ? range[1] : it.first);
72 auto const nextIndex =
73 static_cast<std::size_t
>(std::floor(val - range[0]));
77 if (nextIndex - lastValue.first > 0)
79 for (std::size_t i = lastValue.first + 1; i < nextIndex; i++)
81 unsigned char int_rgba[4];
83 (i - lastValue.first) /
84 (
static_cast<double>(nextIndex - lastValue.first));
88 for (std::size_t j = 0; j < 4; j++)
91 (lastValue.second)[j], (it.second)[j], pos);
96 for (std::size_t j = 0; j < 4; j++)
100 (it.second)[j], 0.2, pos);
105 for (std::size_t j = 0; j < 4; j++)
107 int_rgba[j] = (lastValue.second)[j];
115 lastValue.first = nextIndex;
116 lastValue.second = it.second;
121 vtkLookupTable::Build();
128 std::size_t
const n_colors(lut.
size());
129 for (std::size_t i = 0; i < n_colors; ++i)
131 setColor(std::get<0>(lut[i]), std::get<1>(lut[i]));
135 SetTableRange(range.first, range.second);
141 std::stringstream strout;
142 strout <<
"Writing color table to " << filename <<
" ... ";
143 std::ofstream out(filename.c_str(), std::ios::out);
145 std::size_t nColors = this->GetNumberOfTableValues();
146 for (std::size_t i = 0; i < nColors; i++)
148 unsigned char rgba[4];
150 out << i <<
"\t" << rgba[0] <<
"\t" << rgba[1] <<
"\t" << rgba[2]
154 strout <<
" done." << std::endl;
155 INFO(
"{:s}", strout.str());
160 unsigned char rgba[4])
164 for (
unsigned i = 0; i < 4; ++i)
166 value[i] = rgba[i] / 255.0;
168 vtkLookupTable::SetTableValue(idx, value);
174 vtkLookupTable::GetTableValue(idx, value);
176 for (
unsigned i = 0; i < 4; ++i)
178 rgba[i] =
static_cast<unsigned char>(value[i] * 255.0);
185 auto* dict_rgba =
new unsigned char[4];
186 for (std::size_t i = 0; i < 4; i++)
188 dict_rgba[i] = color[i];
190 _dict.insert(std::pair<double, unsigned char*>(pos, dict_rgba));
195 indx = ((indx < this->TableRange[0])
196 ?
static_cast<vtkIdType
>(this->TableRange[0])
197 : (indx >= this->TableRange[1]
198 ?
static_cast<vtkIdType
>(this->TableRange[1]) - 1
200 indx =
static_cast<std::size_t
>(std::floor(
201 (indx - this->TableRange[0]) *
202 (this->NumberOfColors / (this->TableRange[1] - this->TableRange[0]))));
204 unsigned char* _rgba;
205 _rgba = this->Table->GetPointer(indx * 4);
206 for (std::size_t i = 0; i < 4; i++)
Definition of the Color class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
vtkStandardNewMacro(VtkColorLookupTable)
Definition of the VtkColorLookupTable class.
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 indx, unsigned char rgba[4]) const
void setLookupTable(DataHolderLib::ColorLookupTable const &lut)
Imports settings of OGS lookup table class.
std::array< unsigned char, 4 > Color