在WPF中使用HSV而不是RGB

时间:2013-09-16 06:34:49

标签: c# wpf brush

我正在使用Color.FromArgb来确定画笔颜色。使用这个例如Pen p= new Pen(new SolidColorBrush(Color.FromArgb(95, 255, 0, 0)), 6);这个问题是我想使用4种颜色的画笔(红色,橙色,黄色和绿色)。我有一个条件,根据这种情况下的值,选择具有一定透明度的颜色。这个问题是两种不同颜色之间的过渡,例如从绿色到黄色或从橙色到红色,颜色突然变化,我不想要这样。我希望过渡到不同颜色之间的平滑,例如从橙色到红色,我希望它采取不同程度的橙色,直到它变成红色直到它变成红色。 下面是我的代码的快照,试图澄清我做了什么,所以如果有人可以请提供建议,如果需要进一步澄清,请告诉我。因此,如下所示,取决于X的值,颜色是以一定强度确定的,但问题在于X的最后一个值是10,这意味着绿色和当前是11,这意味着颜色为黄色,这使得视图不平滑,因为颜色从绿色变为黄色而没有任何光滑程度。而且我认为可能使用HSV可以解决这个问题。所以如果有人可以请指教。

for(X=0; X<=40; X++)
//this is green phase
if (X == 0)
    P = new Pen(new SolidColorBrush(Color.FromArgb(75, 0, 255, 0)), 6);
else if (X == 1)
    P = new Pen(new SolidColorBrush(Color.FromArgb(77, 0, 255, 0)), 6);
else if (X == 2)
    P = new Pen(new SolidColorBrush(Color.FromArgb(79, 0, 255, 0)), 6);
else if (X == 3)
    P = new Pen(new SolidColorBrush(Color.FromArgb(81, 0, 255, 0)), 6);
else if (X == 4)
    P = new Pen(new SolidColorBrush(Color.FromArgb(83, 0, 255, 0)), 6);
else if (X == 5)
    P = new Pen(new SolidColorBrush(Color.FromArgb(85, 0, 255, 0)), 6);
else if (X == 6)
    P = new Pen(new SolidColorBrush(Color.FromArgb(87, 0, 255, 0)), 6);
else if (X == 7)
    P = new Pen(new SolidColorBrush(Color.FromArgb(89, 0, 255, 0)), 6);
else if (X == 8)
    P = new Pen(new SolidColorBrush(Color.FromArgb(91, 0, 255, 0)), 6);
else if (X == 9)
    P = new Pen(new SolidColorBrush(Color.FromArgb(93, 0, 255, 0)), 6);
else if (X == 10)
    P = new Pen(new SolidColorBrush(Color.FromArgb(95, 0, 255, 0)), 6);

    // this is yellow phase
else if (X == 11)
    P = new Pen(new SolidColorBrush(Color.FromArgb(75, 255, 255, 0)), 6);
else if (X == 12)
    P = new Pen(new SolidColorBrush(Color.FromArgb(77, 255, 255, 0)), 6);
else if (X == 13)
    P = new Pen(new SolidColorBrush(Color.FromArgb(79, 255, 255, 0)), 6);
else if (X == 14)
    P = new Pen(new SolidColorBrush(Color.FromArgb(81, 255, 255, 0)), 6);
else if (X == 15)
    P = new Pen(new SolidColorBrush(Color.FromArgb(83, 255, 255, 0)), 6);
else if (X == 16)
    P = new Pen(new SolidColorBrush(Color.FromArgb(85, 255, 255, 0)), 6);
else if (X == 17)
    P = new Pen(new SolidColorBrush(Color.FromArgb(87, 255, 255, 0)), 6);
else if (X == 18)
    P = new Pen(new SolidColorBrush(Color.FromArgb(89, 255, 255, 0)), 6);
else if (X == 19)
    P = new Pen(new SolidColorBrush(Color.FromArgb(91, 255, 255, 0)), 6);
else if (X == 20)
    P = new Pen(new SolidColorBrush(Color.FromArgb(93, 255, 255, 0)), 6);

    // this is orange phase
else if (X == 21)
    P = new Pen(new SolidColorBrush(Color.FromArgb(75, 255, 127, 0)), 6);
else if (X == 22)
    P = new Pen(new SolidColorBrush(Color.FromArgb(77, 255, 127, 0)), 6);
else if (X == 23)
    P = new Pen(new SolidColorBrush(Color.FromArgb(79, 255, 127, 0)), 6);
else if (X == 24)
    P = new Pen(new SolidColorBrush(Color.FromArgb(81, 255, 127, 0)), 6);
else if (X == 25)
    P = new Pen(new SolidColorBrush(Color.FromArgb(83, 255, 127, 0)), 6);
else if (X == 26)
    P = new Pen(new SolidColorBrush(Color.FromArgb(85, 255, 127, 0)), 6);
else if (X == 27)
    P = new Pen(new SolidColorBrush(Color.FromArgb(87, 255, 127, 0)), 6);
else if (X == 28)
    P = new Pen(new SolidColorBrush(Color.FromArgb(89, 255, 127, 0)), 6);
else if (X == 29)
    P = new Pen(new SolidColorBrush(Color.FromArgb(91, 255, 127, 0)), 6);
else if (X == 30)
    P = new Pen(new SolidColorBrush(Color.FromArgb(93, 255, 127, 0)), 6);

    //this is red phase
else if (X == 31)
    P = new Pen(new SolidColorBrush(Color.FromArgb(75, 255, 0, 0)), 6);
else if (X == 32)
    P = new Pen(new SolidColorBrush(Color.FromArgb(77, 255, 0, 0)), 6);
else if (X == 33)
    P = new Pen(new SolidColorBrush(Color.FromArgb(79, 255, 0, 0)), 6);
else if (X == 34)
    P = new Pen(new SolidColorBrush(Color.FromArgb(81, 255, 0, 0)), 6);
else if (X == 35)
    P = new Pen(new SolidColorBrush(Color.FromArgb(83, 255, 0, 0)), 6);
else if (X == 36)
    P = new Pen(new SolidColorBrush(Color.FromArgb(85, 255, 0, 0)), 6);
else if (X == 37)
    P = new Pen(new SolidColorBrush(Color.FromArgb(87, 255, 0, 0)), 6);
else if (X == 38)
    P = new Pen(new SolidColorBrush(Color.FromArgb(89, 255, 0, 0)), 6);
else if (X == 39)
    P = new Pen(new SolidColorBrush(Color.FromArgb(91, 255, 0, 0)), 6);
else if (X == 40)
    P = new Pen(new SolidColorBrush(Color.FromArgb(93, 255, 0, 0)), 6);

1 个答案:

答案 0 :(得分:3)

也许你应该看看this answer。它可以帮助您使用颜色类中的方法GetHue()GetSaturation()GetBrightness(),并根据这些参数创建新颜色。

通过使用它,您可以简单地获得开始和结束颜色的色调,并根据您的步长获得所需的中间颜色。

有一个方法不是很好:

int numberOfIntermediateColors = 8;
IEnumerable<Colors> colorPalette = Color.Red.Range(Color.Green, numberOfIntermediateColors);

这就是:

public static IEnumerable<Color> Range(this Color firstColor, Color lastColor, int count)
{
    float stepHueClockwise = GetStepping(firstColor.GetHue(), lastColor.GetHue(), count, Direction.Clockwise);
    float stepHueCounterClockwise = GetStepping(firstColor.GetHue(), lastColor.GetHue(), count, Direction.CounterClockwise);

    if (Math.Abs(stepHueClockwise) >= Math.Abs(stepHueCounterClockwise))
        return Range(firstColor, lastColor, count, Direction.Clockwise);
    else
        return Range(firstColor, lastColor, count, Direction.CounterClockwise);
}

public static IEnumerable<Color> Range(this Color firstColor, Color lastColor, int count, Direction hueDirection)
{
    var color = firstColor;

    if (count <= 0)
        yield break;

    if (count == 1)
        yield return firstColor;

    float startingHue = color.GetHue();
    float stepHue = GetStepping(firstColor.GetHue(), lastColor.GetHue(), count - 1, hueDirection);
    var stepSaturation = (lastColor.GetSaturation() - firstColor.GetSaturation()) / (count - 1);
    var stepBrightness = (lastColor.GetBrightness() - firstColor.GetBrightness()) / (count - 1);
    var stepAlpha = (lastColor.A - firstColor.A) / (count - 1.0);

    for (int i = 1; i < count; i++)
    {
        yield return color;

        var hueValue = startingHue + stepHue * i;

        if (hueValue > 360)
            hueValue -= 360;

        if (hueValue < 0)
            hueValue = 360 + hueValue;

        color = FromAhsb(
                    Clamp((int)(color.A + stepAlpha), 0, 255),
                         hueValue,
                         Clamp(color.GetSaturation() + stepSaturation, 0, 1),
                         Clamp(color.GetBrightness() + stepBrightness, 0, 1));
    }

    yield return lastColor;
}

    public enum Direction
    {
        Clockwise = 0,
        CounterClockwise = 1
    }

private static float GetStepping(float start, float end, int count, Direction direction)
{
    var hueDiff = end - start;

    switch (direction)
    {
        case Direction.CounterClockwise:
            if (hueDiff >= 0)
                hueDiff = (360 - hueDiff) * -1;
            break;

        default:
            if (hueDiff <= 0)
                hueDiff = 360 + hueDiff;
            break;
    }

    return hueDiff / count;
}

private static int Clamp(int value, int min, int max)
{
    if (value < min)
        return min;

    if (value > max)
        return max;

    return value;
}

private static float Clamp(float value, float min, float max)
{
    if (value < min)
        return min;

    if (value > max)
        return max;

    return value;
}

要使您的笔列表没有任何透明度,您可以采用以下简单方法:

var startColor = Color.Green;
var endColor = Color.Red;
var penWidth = 6;

var rainbow = startColor.Range(endColor, 40, bla.Direction.CounterClockwise);
var pens = rainbow.Select(color => new Pen(color, penWidth))
                  .ToList();

// Somewhere else...
int x = RetrieveDesiredCondition();
var neededPen = pens[x];

就我在你的例子中看到的那样,你想让透明度从每个75个颜色的块和75个颜色的块中重复。在这种情况下,这个算法可能会有所帮助:

var startColor = Color.Green;
var endColor = Color.Red;
var penWidth = 6;
var lowerTransparency = 75;
var higherTransparency = 95;
var stepsPerSection = 10;
var sections = 4;
var stepsNeeded = stepsPerSection * sections;

var transparencyRange = higherTransparency - lowerTransparency;
var stepSize = transparencyRange / stepsPerSection;
var rainbow = startColor.Range(endColor, stepsNeeded, Direction.CounterClockwise);
var rainbowWithTransparency = rainbow.Select((color, index) =>
{
    var step = (index % stepsPerSection) * stepSize;
    var neededTransparency = lowerTransparency + step;
    return Color.FromArgb(neededTransparency, color);
});

var pens = rainbowWithTransparency.Select(color => new Pen(color, penWidth))
                                  .ToList();

// Somewhere else...
int x = RetrieveDesiredCondition();
var neededPen = pens[x];