Question

Suppose I have a random RGB value like 240 23 123. Ok ? So I wanna to convert the RGB value in HSV and I need the RGB color be exactly in the middle of a gradient ..So all the other values of the gradient should have more saturation or less. Ideally the gradient should always tend to white independent of what the initial RGB value is. Is there a formula or algorithm to achieve this ? The result should be an array of HSV value for 1 gradient.

Was it helpful?

Solution

If you want the gradient to start at white, go to your color, and then go to black, you can do it a number of ways. Here's one way:

const int numEntries = 20;
const int halfNumEntries = numEntries / 2;
RGBColor gradient[numEntries];
HSVColor hsv = RGBToHSV (rgb); // Where rgb is the RGB color you start with

// Gradient from white to your color
for (int i = 0; i < halfNumEntries; i++)
{
    float newHue = hsv.hue;
    float newSat = (i / halfNumEntries) * hsv.sat;
    float newVal = 1.0 + (i / halfNumEntries) * (hsv.val - 1.0);
    gradient [ i ] = HSVToRGB (newHue, newSat, newVal);
}

gradient [ halfNumEntries ] = rgb;

// Gradient from your color to black
for (int i = (halfNumEntries + 1); i < numEntries; i++)
{
    float newHue = hsv.hue;
    float newSat = hsv.sat + ((i - halfNumEntries) / (halfNumEntries - 1)) * (0.0 - hsv.sat);
    float newVal = hsv.val + ((i - halfNumEntries) / (halfNumEntries - 1)) * (0.0 - hsv.val);
    gradient [ i ] = HSVToRGB (newHue, newSat, newVal);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top