Skip to content

Instantly share code, notes, and snippets.

@lxfly2000
Last active August 5, 2020 03:12
Show Gist options
  • Save lxfly2000/5269c436f548e704cac5e751ea0889d3 to your computer and use it in GitHub Desktop.
Save lxfly2000/5269c436f548e704cac5e751ea0889d3 to your computer and use it in GitHub Desktop.
#include<Windows.h>
#include<vector>
#include<algorithm>
struct SpectrumPoint
{
int p;
union{
struct
{
BYTE r, g, b, a;
}rgba;
COLORREF color;
};
};
template<size_t count>COLORREF GetSpectrumColor(const SpectrumPoint(&points)[count],int v)
{
std::vector<SpectrumPoint>pointList(points, points + count);
std::sort(pointList.begin(), pointList.end(), [](SpectrumPoint a, SpectrumPoint b){return a.p < b.p; });
if (pointList.begin()->p > v)
return pointList.begin()->color;
for (std::vector<SpectrumPoint>::iterator itPointList = pointList.begin(); itPointList != pointList.end(); itPointList++)
{
if (itPointList->p > v)
{
std::vector<SpectrumPoint>::iterator itPointListStart = itPointList - 1;
return RGB(itPointListStart->rgba.r + (itPointList->rgba.r - itPointListStart->rgba.r)*(v - itPointListStart->p) / (itPointList->p - itPointListStart->p),
itPointListStart->rgba.g + (itPointList->rgba.g - itPointListStart->rgba.g)*(v - itPointListStart->p) / (itPointList->p - itPointListStart->p),
itPointListStart->rgba.b + (itPointList->rgba.b - itPointListStart->rgba.b)*(v - itPointListStart->p) / (itPointList->p - itPointListStart->p));
}
}
return pointList.back().color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment