假设我有一个随机的 RGB 值,例如 240 23 123。好吗?所以我想在 HSV 中转换 RGB 值,我需要 RGB 颜色正好在渐变的中间。所以渐变的所有其他值应该有更多的饱和度或更少。理想情况下,渐变应该总是倾向于白色,而与初始 RGB 值无关。有没有公式或算法来实现这一点?结果应该是 1 个梯度的 HSV 值数组。
问问题
897 次
1 回答
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);
}
于 2013-08-29T03:40:57.180 回答