获取渐变给定颜色,其中颜色位于渐变的中间

时间:2013-08-28 23:23:49

标签: algorithm math graphics colors hsv

假设我有一个随机的RGB值,如240 23 123.好吗? 所以我想转换HSV中的RGB值,我需要RGB颜色正好在渐变的中间。所以渐变的所有其他值应该具有更多或更少的饱和度。 理想情况下,渐变应始终倾向于白色,与初始RGB值无关。 是否有公式或算法来实现这一目标? 结果应该是1个梯度的HSV值数组。

1 个答案:

答案 0 :(得分:1)

如果您希望渐变以白色开始,请转到您的颜色,然后转到黑色,您可以通过多种方式进行操作。这是一种方式:

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);
}