如何使基于WPF中值特定的颜色变亮或变暗?(How to make specific color d

2019-09-24 02:53发布

我开发WPF应用程序。 我有颜色的物体在C#中的实例。 假设我有红色对象即实例Color c = Color.FromArgb(255,255,0,0)现在假设我有一个值,其范围为1〜10。因此,基于该值我想改变的颜色' C”对象。 我想1淡红色,暗红色10.淡红色变为暗从1值的增加如何才能做到这一点在C#中的WPF应用程序? 能否请您给我提供任何代码或链接,通过它我可以解决上述问题?

Answer 1:

您可以尝试简单地通过一些系数相乘的红色,绿色和蓝色分量。

public static Color ChangeLightness(this Color color, float coef)
{
    return Color.FromArgb((int)(color.R * coef), (int)(color.G * coef),
        (int)(color.B * coef));
}

或者,如果你想使用一个整数值从1到10,而不是系数:

private const int MinLightness = 1;
private const int MaxLightness = 10;
private const float MinLightnessCoef = 1f;
private const float MaxLightnessCoef = 0.4f;

public static Color ChangeLightness(this Color color, int lightness)
{
    if (lightness < MinLightness)
        lightness = MinLightness;
    else if (lightness > MaxLightness)
        lightness = MaxLightness;

    float coef = MinLightnessCoef +
      (
        (lightness - MinLightness) *
          ((MaxLightnessCoef - MinLightnessCoef) / (MaxLightness - MinLightness))
      );

    return Color.FromArgb(color.A, (int)(color.R * coef), (int)(color.G * coef),
        (int)(color.B * coef));
}


Answer 2:

比方说,您使用的是滑盖的最小值1和最大值10 。 您可以通过只乘以值25.5 (255 / max value) 。 然后,减去最大值(255),这个问题的答案,并用其作为红色值。

double newRedValue = 255 - (slider.Value * (255 / slider.Maximum));
int redValue = Convert.ToInt32(newRedValue);
Color c = Color.FromArgb(redValue ,255,0,0)

您可以更换255 / slider.Maximum具有恒定值,因为它可能会保持不变。 上面的公式将创建一个逆效果,所以滑块值越低,越亮的红色阴凉处。 当然,10的值将导致red0 ,这样你就可以有增加的最小值,如果你不想让红色成分是低。



Answer 3:

什么样式DataTrigger,如果你有值的定数?

https://www.google.co.uk/search?q=c%23+wpf+style+datatrigger

<Button>
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding NameOfYourProperty}" Value="0">
                    <Setter Property="Background"
                            Value="#FF000000" />
                </DataTrigger>
                <DataTrigger Binding="{Binding NameOfYourProperty}" Value="1">
                    <Setter Property="Background"
                            Value="#FF110000" />
                </DataTrigger>
                <DataTrigger Binding="{Binding NameOfYourProperty}" Value="2">
                    <Setter Property="Background"
                            Value="#FF220000" />
                </DataTrigger>
                ( etc ... )
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

然后,如果你需要重用的风格,那么你可以把在<Resources>你的窗口/用户控件的部分。



Answer 4:

一个清洁的解决方案是并列2个矩形:一个是你想要的颜色,另一种是黑色的。

然后玩Opacity的黑色矩形变暗/减轻潜在的颜色。

它看起来像:

<Grid>
   <Rectangle Fill="{Binding myColor}" />
   <Rectangle Fill="Black" Opacity="{Binding colorModifierPercentage}" />
</Grid>

当然, colorModifierPercentage必须介于0和1之间的数,并且矩形可以是任何形状 。



Answer 5:

有它在CodeProject的一篇文章 :

    /// <summary>
    /// Converts a WPF RGB color to an HSL color
    /// </summary>
    /// <param name="rgbColor">The RGB color to convert.</param>
    /// <returns>An HSL color object equivalent to the RGB color object passed in.</returns>
    static HlsColor RgbToHls(Color rgbColor)
    {
        // Initialize result
        var hlsColor = new HlsColor();

        // Convert RGB values to percentages
        double r = (double)rgbColor.R / 255;
        var g = (double)rgbColor.G / 255;
        var b = (double)rgbColor.B / 255;
        var a = (double)rgbColor.A / 255;

        // Find min and max RGB values
        var min = Math.Min(r, Math.Min(g, b));
        var max = Math.Max(r, Math.Max(g, b));
        var delta = max - min;

        /* If max and min are equal, that means we are dealing with 
         * a shade of gray. So we set H and S to zero, and L to either
         * max or min (it doesn't matter which), and  then we exit. */

        //Special case: Gray
        if (max == min)
        {
            hlsColor.H = 0;
            hlsColor.S = 0;
            hlsColor.L = max;
            return hlsColor;
        }

        /* If we get to this point, we know we don't have a shade of gray. */

        // Set L
        hlsColor.L = (min + max) / 2;

        // Set S
        if(hlsColor.L < 0.5)
        {
            hlsColor.S = delta / (max + min);
        }
        else
        {
            hlsColor.S = delta / (2.0 - max - min);
        }

        // Set H
        if (r == max) hlsColor.H = (g - b) / delta;
        if (g == max) hlsColor.H = 2.0 + (b - r) / delta;
        if (b == max) hlsColor.H = 4.0 + (r - g) / delta;
        hlsColor.H *= 60;
        if (hlsColor.H < 0) hlsColor.H += 360;

        // Set A
        hlsColor.A = a;

        // Set return value
        return hlsColor;

    }

    /// <summary>
    /// Converts a WPF HSL color to an RGB color
    /// </summary>
    /// <param name="hlsColor">The HSL color to convert.</param>
    /// <returns>An RGB color object equivalent to the HSL color object passed in.</returns>
    static Color HlsToRgb(HlsColor hlsColor)
    {
        // Initialize result
        var rgbColor = new Color();

        /* If S = 0, that means we are dealing with a shade 
         * of gray. So, we set R, G, and B to L and exit. */

        // Special case: Gray
        if (hlsColor.S == 0)
        {
            rgbColor.R = (byte)(hlsColor.L  * 255);
            rgbColor.G = (byte)(hlsColor.L * 255);
            rgbColor.B = (byte)(hlsColor.L * 255);
            rgbColor.A = (byte)(hlsColor.A * 255);
            return rgbColor;
        }

        double t1;
        if (hlsColor.L < 0.5)
        {
            t1 = hlsColor.L*(1.0 + hlsColor.S);
        }
        else
        {
            t1 = hlsColor.L + hlsColor.S - (hlsColor.L * hlsColor.S);
        }

        var t2 = 2.0*hlsColor.L - t1;

        // Convert H from degrees to a percentage
        var h = hlsColor.H / 360;

        // Set colors as percentage values
        var tR = h + (1.0/3.0);
        var r = SetColor(t1, t2, tR);

        var tG = h;
        var g = SetColor(t1, t2, tG);

        var tB = h - (1.0 / 3.0);
        var b = SetColor(t1, t2, tB);

        // Assign colors to Color object
        rgbColor.R = (byte)(r * 255);
        rgbColor.G = (byte)(g * 255);
        rgbColor.B = (byte)(b * 255);
        rgbColor.A = (byte)(hlsColor.A * 255);

        // Set return value
        return rgbColor;
    }

    /// <summary>
    /// Used by the HSL-to-RGB converter.
    /// </summary>
    /// <param name="t1">A temporary variable.</param>
    /// <param name="t2">A temporary variable.</param>
    /// <param name="t3">A temporary variable.</param>
    /// <returns>An RGB color value, in decimal format.</returns>
    private static double SetColor(double t1, double t2, double t3)
    {
        if (t3 < 0) t3 += 1.0;
        if (t3 > 1) t3 -= 1.0;

        double color;
        if (6.0 * t3 < 1)
        {
            color = t2 + (t1 - t2) * 6.0 * t3;
        }
        else if(2.0 * t3 < 1)
        {
            color = t1;
        }
        else if(3.0*t3 < 2)
        {
            color = t2 + (t1 - t2) * ((2.0/3.0) - t3) * 6.0;
        }
        else
        {
            color = t2;
        }

        // Set return value
        return color;
    }


文章来源: How to make specific color darken or lighten based on value in wpf?