5

ここで見つかった質問の回答を使用しようとしています 。カラークラスの使用に問題があります。たとえば、Color.Red.Range(Color.Green, numberOfIntermediateColors);「'System.Windows.Media.Color' には 'Red' の定義が含まれていません」というエラーが表示さGetBrightnessGetSaturationますGetHue。 'GetSaturation' の定義と、'System.Windows.Media.Color' 型の最初の引数を受け入れる拡張メソッド 'GetSaturation' が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)"

では、提案された回答を Wpf で使用する方法を教えてください。

4

3 に答える 3

11

次の拡張メソッドを使用します。

public static float GetHue(this System.Windows.Media.Color c) =>
  System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B).GetHue();

public static float GetBrightness(this System.Windows.Media.Color c) =>
  System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B).GetBrightness();

public static float GetSaturation(this System.Windows.Media.Color c) =>
  System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B).GetSaturation();
于 2014-04-12T21:43:20.927 に答える
1

問題が何であるかはわかりませんが、他の2つの回答からコードをコピーしました。System.Drawing名前空間を追加してあいまいさを修正すると、正常に機能するようです

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);
        }
    }
}
于 2013-09-17T03:18:37.253 に答える
0

WPF は気にしませんSystem.Drawing。プロジェクトからその参照を削除します。

あなたが探しているのはLinearGradientBrush

<Border>
    <Border.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="Green" Offset="0"/>
            <GradientStop Color="Red" Offset="1"/>
        </LinearGradientBrush>
    </Border.Background>
</Border>

結果:

ここに画像の説明を入力

また、手続き型コードを使用して UI を定義しないでください。それが XAML の目的です。

于 2013-09-17T02:13:58.653 に答える