#1222 Interpolate category colors and assign well color based on sorted order

This commit is contained in:
Magne Sjaastad
2017-02-15 13:26:59 +01:00
parent 4fc0839a33
commit 9a7f187765
4 changed files with 59 additions and 20 deletions

View File

@@ -112,4 +112,50 @@ cvf::Color3ub ColorTable::fromQColor(const QColor& color)
return cvf::Color3ub(color.red(), color.green(), color.blue());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color3ubArray ColorTable::interpolateColorArray(const cvf::Color3ubArray& colorArray, size_t targetColorCount)
{
size_t inputColorCount = colorArray.size();
CVF_ASSERT(inputColorCount > 1);
CVF_ASSERT(targetColorCount > 1);
cvf::Color3ubArray colors;
colors.reserve(targetColorCount);
const size_t inputColorsMaxIdx = inputColorCount - 1;
const size_t outputColorsMaxIdx = targetColorCount - 1;
for (size_t outputLevelIdx = 0; outputLevelIdx < outputColorsMaxIdx; outputLevelIdx++)
{
double dblInputLevelIndex = inputColorsMaxIdx * (outputLevelIdx / static_cast<double>(outputColorsMaxIdx));
const size_t inputLevelIndex = static_cast<size_t>(dblInputLevelIndex);
CVF_ASSERT(inputLevelIndex < inputColorsMaxIdx);
double t = dblInputLevelIndex - inputLevelIndex;
CVF_ASSERT(t >= 0 && t <= 1.0);
cvf::Color3ub c1 = colorArray[inputLevelIndex];
cvf::Color3ub c2 = colorArray[inputLevelIndex + 1];
int r = static_cast<int>(c1.r() + t*(c2.r() - c1.r()) + 0.5);
int g = static_cast<int>(c1.g() + t*(c2.g() - c1.g()) + 0.5);
int b = static_cast<int>(c1.b() + t*(c2.b() - c1.b()) + 0.5);
r = cvf::Math::clamp(r, 0, 255);
g = cvf::Math::clamp(g, 0, 255);
b = cvf::Math::clamp(b, 0, 255);
cvf::Color3ub col((cvf::ubyte)r, (cvf::ubyte)g, (cvf::ubyte)b);
colors.add(col);
}
colors.add(colorArray[colorArray.size() - 1]);
return colors;
}
} // namespace caf

View File

@@ -65,8 +65,8 @@ public:
cvf::Color3ubArray color3ubArray() const;
cvf::Color3fArray color3fArray() const;
static cvf::Color3ub fromQColor(const QColor& color);
static cvf::Color3ub fromQColor(const QColor& color);
static cvf::Color3ubArray interpolateColorArray(const cvf::Color3ubArray& colorArray, size_t targetColorCount);
private:
const std::vector<cvf::Color3ub> m_colors;
};