Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据wpf中的值使特定颜色变暗或变亮?_C#_Wpf_Colors_System.windows.media - Fatal编程技术网

C# 如何根据wpf中的值使特定颜色变暗或变亮?

C# 如何根据wpf中的值使特定颜色变暗或变亮?,c#,wpf,colors,system.windows.media,C#,Wpf,Colors,System.windows.media,我正在开发wpf应用程序。我在C#中有一个Color对象的实例。假设我有一个红色对象的实例,即Color c=Color。从argb(255255,0,0)现在假设我有一个值,范围从1到10。基于这个值,我想改变“c”对象的颜色。我想要浅红色的1和深红色的10。当值从1增加时,浅红色变暗。如何在C#中为wpf应用程序执行此操作?您能为我提供解决上述问题的代码或链接吗?如果您有一组值,样式数据触发器如何 (等等……) 然后,如果需要重用样式,则可以将其放入窗口/用户控件的部分。假设您使用的滑

我正在开发wpf应用程序。我在C#中有一个Color对象的实例。假设我有一个红色对象的实例,即
Color c=Color。从argb(255255,0,0)
现在假设我有一个值,范围从1到10。基于这个值,我想改变“c”对象的颜色。我想要浅红色的1和深红色的10。当值从1增加时,浅红色变暗。如何在C#中为wpf应用程序执行此操作?您能为我提供解决上述问题的代码或链接吗?

如果您有一组值,样式数据触发器如何


(等等……)

然后,如果需要重用样式,则可以将其放入窗口/用户控件的
部分。

假设您使用的滑块的最小值为
1
,最大值为
10
。您可以将该值乘以
25.5(255/最大值)
。然后,从最大值(255)中减去该答案,并将其用作红色值

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

您可以将
255/滑块。最大值
替换为常量值,因为它可能保持不变。上面的公式将创建反向效果,因此滑块值越低,红色阴影越亮。当然,值为10将导致
red
0
,因此如果不希望红色分量那么低,可以在那里添加一个最小值。

您可以尝试简单地将红色、绿色和蓝色分量乘以某个系数

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));
}
private const int MinLightness=1;
私有常量int最大亮度=10;
私有常量浮点最小亮度系数=1f;
私有常量浮点MaxLightnessCoef=0.4f;
公共静态颜色变化亮度(此颜色,内部亮度)
{
if(亮度<最小亮度)
轻盈=轻盈;
else if(亮度>最大亮度)
亮度=最大亮度;
浮动系数=最小亮度系数+
(
(亮度-最小亮度)*
((MaxLightnessCoef-MinLightnessCoef)/(MaxLightness-MinLightness))
);
返回Color.FromArgb(Color.A,(int)(Color.R*coef),(int)(Color.G*coef),
(内部)(颜色B*coef));
}

一个更干净的解决方案是将两个矩形并列:一个是您想要的颜色,另一个是黑色

然后在黑色矩形上玩
Opacity
,使基础颜色变暗/变亮

它看起来像:

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

当然,
colorModifierPercentage
必须是一个介于0和1之间的数字,矩形可以是任何一个。

有:

//
///将WPF RGB颜色转换为HSL颜色
/// 
///要转换的RGB颜色。
///与传入的RGB颜色对象等效的HSL颜色对象。
静态HLS颜色RgbToHls(颜色RGB颜色)
{
//初始化结果
var hlsColor=新的hlsColor();
//将RGB值转换为百分比
双色r=(双色)rgbColor.r/255;
var g=(双色)rgbColor.g/255;
VarB=(双色)rgbColor.b/255;
变量a=(双)rgbColor.a/255;
//查找最小和最大RGB值
var min=Math.min(r,Math.min(g,b));
var max=Math.max(r,Math.max(g,b));
var delta=最大-最小值;
/*如果最大值和最小值相等,则表示我们正在处理
*灰色的阴影。所以我们把H和S设为零,把L设为任意一个
*最大值或最小值(不管是哪个),然后我们退出*/
//特殊情况:灰色
如果(最大=最小)
{
hlsColor.H=0;
hlsColor.S=0;
hlsColor.L=最大值;
返回hlsColor;
}
/*如果我们达到这一点,我们知道我们没有灰色的阴影*/
//集合L
hlsColor.L=(最小+最大)/2;
//设置
如果(hlsColor.L<0.5)
{
hlsColor.S=增量/(最大值+最小值);
}
其他的
{
hlsColor.S=增量/(2.0-最大-最小);
}
//设置H
如果(r==max)hlsColor.H=(g-b)/delta;
如果(g==max)hlsColor.H=2.0+(b-r)/delta;
如果(b==max)hlsColor.H=4.0+(r-g)/delta;
hlsColor.H*=60;
如果(hlsColor.H<0)hlsColor.H+=360;
//设定
hlsColor.A=A;
//设置返回值
返回hlsColor;
}
/// 
///将WPF HSL颜色转换为RGB颜色
/// 
///要转换的HSL颜色。
///与传入的HSL颜色对象等效的RGB颜色对象。
静态颜色HlsToRgb(HlsColor HlsColor)
{
//初始化结果
var rgbColor=新颜色();
/*如果S=0,这意味着我们正在处理一个阴影
*我们把R,G,B设为L,然后退出*/
//特殊情况:灰色
如果(hlsColor.S==0)
{
rgbColor.R=(字节)(hlsColor.L*255);
rgbColor.G=(字节)(hlsColor.L*255);
rgbColor.B=(字节)(hlscoolor.L*255);
rgbColor.A=(字节)(hlscoolor.A*255);
返回RGB颜色;
}
双t1;
如果(hlsColor.L<0.5)
{
t1=hlsColor.L*(1.0+hlsColor.S);
}
其他的
{
t1=hlsColor.L+hlsColor.S-(hlsColor.L*hlsColor.S);
}
变量t2=2.0*hlsColor.L-t1;
//将H从度转换为百分比
var h=hlsColor.h/360;
//将颜色设置为百分比值
var tR=h+(1.0/3.0);
var r=SetColor(t1,t2,tR);
var tG=h;
var g=设置颜色(t1
<Grid>
   <Rectangle Fill="{Binding myColor}" />
   <Rectangle Fill="Black" Opacity="{Binding colorModifierPercentage}" />
</Grid>
    /// <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;
    }