在Wpf中使用Color.GetSaturation,Color.GetBrightness [英] Use Color.GetSaturation, Color.GetBrightness in Wpf

查看:73
本文介绍了在Wpf中使用Color.GetSaturation,Color.GetBrightness的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在此处找到的问题的答案
我在使用颜色类时遇到问题。例如, Color.Red.Range(Color.Green,numberOfIntermediateColors); 出现错误,提示 System.Windows.Media.Color不包含红色的定义。
GetBrightness GetSaturation GetHue 相同出现错误,提示'System.Windows.Media.Color'不包含'GetSaturation'的定义,并且找不到扩展方法'GetSaturation'接受类型为'System.Windows.Media.Color'的第一个参数(您是否缺少using指令或程序集引用?)"

I'm trying to use the answer of the question found here I have a problem with using the color class. For example,Color.Red.Range(Color.Green, numberOfIntermediateColors); an error appears saying" 'System.Windows.Media.Color' does not contain a definition for 'Red' " Same withGetBrightness,GetSaturation and GetHue there is an error saying "'System.Windows.Media.Color' does not contain a definition for 'GetSaturation' and no extension method 'GetSaturation' accepting a first argument of type 'System.Windows.Media.Color' could be found (are you missing a using directive or an assembly reference?) "

所以有人可以建议在Wpf中使用建议的答案吗?

So could anyone could please advise how to use the proposed answer in Wpf?

推荐答案

不确定是什么问题,但我从其他2个答案中复制了代码,如果添加 System,它似乎可以正常工作。绘制名称空间并修正歧义

Not sure what the problem is but I copied the code from the other 2 answers and it seems to work fine if you add the System.Drawing namespace and fix up the ambiguities

int numberOfIntermediateColors = 8;
IEnumerable<System.Drawing.Color> colorPalette = System.Drawing.Color.Red.Range(System.Drawing.Color.Green, numberOfIntermediateColors);
// returns 8 colors in that range.

范围扩展方法

public static class ColorExtensions
{
    public static IEnumerable<System.Drawing.Color> Range(this System.Drawing.Color firstColor, System.Drawing.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<System.Drawing.Color> Range(this System.Drawing.Color firstColor, System.Drawing.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;
    }

    public static System.Drawing.Color FromAhsb(int alpha, float hue, float saturation, float brightness)
    {
        if (0 > alpha
            || 255 < alpha)
        {
            throw new ArgumentOutOfRangeException(
                "alpha",
                alpha,
                "Value must be within a range of 0 - 255.");
        }

        if (0f > hue
            || 360f < hue)
        {
            throw new ArgumentOutOfRangeException(
                "hue",
                hue,
                "Value must be within a range of 0 - 360.");
        }

        if (0f > saturation
            || 1f < saturation)
        {
            throw new ArgumentOutOfRangeException(
                "saturation",
                saturation,
                "Value must be within a range of 0 - 1.");
        }

        if (0f > brightness
            || 1f < brightness)
        {
            throw new ArgumentOutOfRangeException(
                "brightness",
                brightness,
                "Value must be within a range of 0 - 1.");
        }

        if (0 == saturation)
        {
            return System.Drawing.Color.FromArgb(
                                alpha,
                                Convert.ToInt32(brightness * 255),
                                Convert.ToInt32(brightness * 255),
                                Convert.ToInt32(brightness * 255));
        }

        float fMax, fMid, fMin;
        int iSextant, iMax, iMid, iMin;

        if (0.5 < brightness)
        {
            fMax = brightness - (brightness * saturation) + saturation;
            fMin = brightness + (brightness * saturation) - saturation;
        }
        else
        {
            fMax = brightness + (brightness * saturation);
            fMin = brightness - (brightness * saturation);
        }

        iSextant = (int)Math.Floor(hue / 60f);
        if (300f <= hue)
        {
            hue -= 360f;
        }

        hue /= 60f;
        hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
        if (0 == iSextant % 2)
        {
            fMid = (hue * (fMax - fMin)) + fMin;
        }
        else
        {
            fMid = fMin - (hue * (fMax - fMin));
        }

        iMax = Convert.ToInt32(fMax * 255);
        iMid = Convert.ToInt32(fMid * 255);
        iMin = Convert.ToInt32(fMin * 255);

        switch (iSextant)
        {
            case 1:
                return System.Drawing.Color.FromArgb(alpha, iMid, iMax, iMin);
            case 2:
                return System.Drawing.Color.FromArgb(alpha, iMin, iMax, iMid);
            case 3:
                return System.Drawing.Color.FromArgb(alpha, iMin, iMid, iMax);
            case 4:
                return System.Drawing.Color.FromArgb(alpha, iMid, iMin, iMax);
            case 5:
                return System.Drawing.Color.FromArgb(alpha, iMax, iMin, iMid);
            default:
                return System.Drawing.Color.FromArgb(alpha, iMax, iMid, iMin);
        }
    }
}

这篇关于在Wpf中使用Color.GetSaturation,Color.GetBrightness的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆