gpt4 book ai didi

c# - 如何获得平滑的颜色变化?

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:47 25 4
gpt4 key购买 nike

在 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);
}
}
}

此代码使用此结构和方法来修改用户的系​​统颜色。

感谢this guythis other guy

    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 后每 0.1 秒增加 1,当设置为 0 时,强度为 BLACK ,我想要稍微“平滑”地过渡到新颜色,这样它就不会突然分散用户的注意力。

最佳答案

您可以使用 linearpolynomial插值。两者都应该产生平稳的过渡。如果你想要一个更渐变、逐渐变细的效果,你可以使用 logarithmic interpolation .

对于大多数人来说,每 0.1 秒可能看起来不流畅,因为它在技术上是每秒 10 帧。我可能会尝试每 0.05-0.03 秒更新一次(20-30 FPS)。

希望对您有所帮助。

关于c# - 如何获得平滑的颜色变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17848090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com