2013-07-25 30 views
2

在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.1秒強度增加其設置爲0,當在0,強度BLACK後,我希望對新顏色進行某種「平滑」過渡,以免突然分散用戶注意力。

回答

1

您可以使用linearpolynomial插值。兩者都應該產生平穩過渡。如果你想要更漸進的漸變效果,你可以使用logarithmic interpolation

對於大多數人來說,每0.1秒看起來可能不夠平滑,因爲它在技術上每秒鐘10幀。我可能會嘗試更新每0.05-0.03秒(20-30 FPS)。

希望有所幫助。