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.
Asked
Active
Viewed 922 times
0
-
Most languages have a colorspace mapping algorithm available already, sometimes in their stdlib. I recommend using that/those. – Ignacio Vazquez-Abrams Aug 28 '13 at 23:26
-
color space mapping. Don't know what are you talking about. It's a new word for me, colorspace mapping. I'm using a 2D game engine. My problem is basically that a random RGB value has a different S which may vary from 0 to 100 and an S of 90 is not exactly in the middle of the range so I can get a resulting array of no more then 20 values which do not tend to white but sometimes to gray. – Claudio Ferraro Aug 28 '13 at 23:38
-
Ideally a should involve V too but how to interpolate it with S I don't know. – Claudio Ferraro Aug 28 '13 at 23:42
1 Answers
1
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);
}

user1118321
- 25,567
- 4
- 55
- 86
-
What i/10.0 stand for ? What is 10.0 . If I want to encrease the number of colors per gradient? – Claudio Ferraro Aug 29 '13 at 10:58
-
10 is half the number of entries. i/10 is how far along you are in the first half (or second half) of the gradient. I've updated the code to be more clear. – user1118321 Aug 29 '13 at 16:35