如何实现平滑的颜色变化?

3

如何在C#中顺畅地将一种颜色平滑地转换为另一种颜色是最好的方法。

我的程序根据电池电量更改用户的颜色方案。

我的当前代码:

private void greater75_Tick(object sender, EventArgs e) {
        if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled())
        {
            PowerStatus ps = SystemInformation.PowerStatus;
            if (ps.BatteryLifePercent >= 0.75)
            {
                ChangeCol.clrAfterGlow = 5;
                ChangeCol.clrColor = ColorToBgra(Color.Green);
                ChangeCol.nIntensity += 1;
                DwmSetColorizationParameters(ref ChangeCol, false);
            }
            if (ps.BatteryLifePercent < 0.75)
            {
                ChangeCol.clrAfterGlow = 5;
                ChangeCol.clrColor = ColorToBgra(Color.Blue);
                ChangeCol.nIntensity += 1;
                DwmSetColorizationParameters(ref ChangeCol, false);
            }
        }
    }

这段代码使用这个结构体和方法来修改用户系统的颜色。

感谢这位那位提供的帮助。

    public DWM_COLORIZATION_PARAMS ChangeCol;
    public struct DWM_COLORIZATION_PARAMS
    {
        public uint clrColor;
        public uint clrAfterGlow;
        public uint nIntensity;
        public uint clrAfterGlowBalance;
        public uint clrBlurBalance;
        public uint clrGlassReflectionIntensity;
        public bool fOpaque;
    }


    [DllImport("dwmapi.dll", EntryPoint = "#127", PreserveSig = false)]
    private static extern void DwmGetColorizationParameters(out DWM_COLORIZATION_PARAMS parameters);

    [DllImport("dwmapi.dll", EntryPoint = "#131", PreserveSig = false)]
    private static extern void DwmSetColorizationParameters(ref DWM_COLORIZATION_PARAMS parameters,
                                                            bool unknown);

    // Helper method to convert from a Win32 BGRA-format color to a .NET color.
    private static Color BgraToColor(uint color)
    {
        return Color.FromArgb(Int32.Parse(color.ToString("X"), NumberStyles.HexNumber));
    }

    // Helper method to convert from a .NET color to a Win32 BGRA-format color.
    private static uint ColorToBgra(Color color)
    {
        return (uint)(color.B | (color.G << 8) | (color.R << 16) | (color.A << 24));
    }
    [DllImport("dwmapi.dll", PreserveSig = false)]
    public static extern bool DwmIsCompositionEnabled();
    // Gets or sets the current color used for DWM glass, based on the user's color scheme.

    public static Color ColorizationColor
    {
        get
        {
            // Call the DwmGetColorizationParameters function to fill in our structure.
            DWM_COLORIZATION_PARAMS parameters;
            DwmGetColorizationParameters(out parameters);

            // Convert the colorization color to a .NET color and return it.
            return BgraToColor(parameters.clrColor);
        }
        set
        {
            // Retrieve the current colorization parameters, just like we did above.
            DWM_COLORIZATION_PARAMS parameters;
            DwmGetColorizationParameters(out parameters);

            // Then modify the colorization color.
            // Note that the other parameters are left untouched, so they will stay the same.
            // You can also modify these; that is left as an exercise.
            parameters.clrColor = ColorToBgra(value);

            // Call the DwmSetColorizationParameters to make the change take effect.
            DwmSetColorizationParameters(ref parameters, false);
        }
    }

问题在于设置颜色后,每0.1秒增加一次强度,当强度为0时,颜色为黑色,我希望有一个相对“平滑”的过渡到新的颜色,以避免突然分散用户注意力。
1个回答

2
你可以使用线性插值多项式插值。两者都应该能产生平滑的过渡。如果你想要一个更渐进、逐渐减弱的效果,可以使用对数插值
每0.1秒更新可能对大多数人来说不够平滑,因为它在技术上相当于10帧每秒。我建议尝试每0.05-0.03秒更新(20-30 FPS)。
希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接