#include "ColorGradient.h" ColorGradient::ColorGradient(float min, float max, vector colorSteps) { _minValue = min; _maxValue = max; _colorSteps = colorSteps; } ColorGradient::~ColorGradient() { _colorSteps.clear(); } // Get the RGBA value at the give point RGBA ColorGradient::GetRGBAValue(float inputValue) { if (inputValue <= _minValue) { return _colorSteps.front(); } else if (inputValue >= _maxValue) { return _colorSteps.back(); } float range = _maxValue - _minValue; float value = inputValue - _minValue; float steps = range / (float)(_colorSteps.size() - 1); int colorStepInside = (int)(value / steps); float normalisedValue = (value - (colorStepInside * steps)) / steps; return Interpolate(_colorSteps[colorStepInside], _colorSteps[colorStepInside + 1], normalisedValue); } // Method for interpolating the color between each step RGBA ColorGradient::Interpolate(RGBA a, RGBA b, float pointValue) { if (pointValue <= 0.0f) { return a; } else if (pointValue >= 1.0f) { return b; } unsigned int currentRed = (unsigned int)((1.0f - pointValue) * a.red + pointValue * b.red); unsigned int currentGreen = (unsigned int)((1.0f - pointValue) * a.green + pointValue * b.green); unsigned int currentBlue = (unsigned int)((1.0f - pointValue) * a.blue + pointValue * b.blue); unsigned int currentAlpha = (unsigned int)((1.0f - pointValue) * a.alpha + pointValue * b.alpha); return RGBA{ currentRed, currentGreen, currentBlue, currentAlpha }; }