将颜色淡化为白色(增加亮度)

我想在.NET中创建一个文本框“发光”黄色,然后“淡化”为白色(基本上,通过逐步增加亮度)。 我认为Stackoverflow会在您发布答案后执行此操作。 我知道增加亮度并不是那么简单(它不仅仅是均匀地增加/减少RGB),但我不知道如何做到这一点。

完美的色彩准确性对此并不重要。 我正在使用C#,虽然VB的例子也很好。

编辑:这适用于Winforms。

这可能比你需要的多,这是我使用的类的代码:

public class ControlColorAnimator { private const int INTERVAL = 100; private readonly decimal _alphaIncrement; private readonly decimal _blueIncrement; private readonly Color _endColor; private readonly decimal _greenIncrement; private readonly int _iterations; private readonly decimal _redIncrement; private readonly Color _startColor; private decimal _currentAlpha; private decimal _currentBlueValue; private decimal _currentGreenValue; private decimal _currentRedValue; private Timer _timer; public ControlColorAnimator(TimeSpan duration, Color startColor, Color endColor) { _startColor = startColor; _endColor = endColor; resetColor(); _iterations = duration.Milliseconds / INTERVAL; _alphaIncrement = ((decimal) startColor.A - endColor.A) / _iterations; _redIncrement = ((decimal) startColor.R - endColor.R) / _iterations; _greenIncrement = ((decimal) startColor.G - endColor.G) / _iterations; _blueIncrement = ((decimal) startColor.B - endColor.B) / _iterations; } public Color CurrentColor { get { int alpha = Convert.ToInt32(_currentAlpha); int red = Convert.ToInt32(_currentRedValue); int green = Convert.ToInt32(_currentGreenValue); int blue = Convert.ToInt32(_currentBlueValue); return Color.FromArgb(alpha, red, green, blue); } } public event EventHandler> ColorChanged; public void Go() { disposeOfTheTimer(); OnColorChanged(_startColor); resetColor(); int currentIteration = 0; _timer = new Timer(delegate { if (currentIteration++ >= _iterations) { Stop(); return; } _currentAlpha -= _alphaIncrement; _currentRedValue -= _redIncrement; _currentGreenValue -= _greenIncrement; _currentBlueValue -= _blueIncrement; OnColorChanged(CurrentColor); }, null, TimeSpan.FromMilliseconds(INTERVAL), TimeSpan.FromMilliseconds(INTERVAL)); } public void Stop() { disposeOfTheTimer(); OnColorChanged(_endColor); } protected virtual void OnColorChanged(Color color) { if (ColorChanged == null) return; ColorChanged(this, color); } private void disposeOfTheTimer() { Timer timer = _timer; _timer = null; if (timer != null) timer.Dispose(); } private void resetColor() { _currentAlpha = _startColor.A; _currentRedValue = _startColor.R; _currentGreenValue = _startColor.G; _currentBlueValue = _startColor.B; } } 

这使用DataEventArgs (如下所示)

 ///  /// Generic implementation of  that allows for a data element to be passed. ///  /// The type of data to contain. [DebuggerDisplay("{Data}")] public class DataEventArgs : EventArgs { private T _data; ///  /// Constructs a . ///  /// The data to contain in the  [DebuggerHidden] public DataEventArgs(T data) { _data = data; } ///  /// Gets the data for this . ///  public virtual T Data { [DebuggerHidden] get { return _data; } [DebuggerHidden] protected set { _data = value; } } [DebuggerHidden] public static implicit operator DataEventArgs(T data) { return new DataEventArgs(data); } [DebuggerHidden] public static implicit operator T(DataEventArgs e) { return e.Data; } } 

在您的表单中使用如下:

 private ControlColorAnimator _animator; private void runColorLoop() { endCurrentAnimation(); startNewAnimation(); } private void endCurrentAnimation() { ControlColorAnimator animator = _animator; _animator = null; if (animator != null) { animator.ColorChanged -= _animator_ColorChanged; animator.Stop(); } } private void startNewAnimation() { _animator = new ControlColorAnimator(TimeSpan.FromSeconds(.6), Color.Yellow, BackColor); _animator.ColorChanged += _animator_ColorChanged; _animator.Go(); } private void _animator_ColorChanged(object sender, DataEventArgs e) { invokeOnFormThread(delegate { setColor(e); }); } private void setColor(Color color) { // code to set color of the controls goes here } private void invokeOnFormThread(MethodInvoker method) { if (IsHandleCreated) Invoke(method); else method(); } 

只需根据时间在颜色之间进行插值。

如果您的橙色是(r1,g1,b1)并且您希望淡化为不同的颜色(r2,g2,b2),则线性插值的公式为(r1 +(r2-r1)* t,g1 +(g2) -g1)* t,b1 +(b2-b1)* t),其中t在[0.0 1.0]的范围内。

在您的示例中,您的第一种颜色可能类似于(255,200,0),第二种颜色可能是(255,255,255)。

如果希望更平滑的过渡,请查找不同的插值方式。

如果您想要快速顺畅地完成它,请查看ColorMatrix类。

当我提交这个答案时,你最初没有指定技术,但是这里是你如何使用jQuery来实现的。

UI /效果/突出显示 。

 $("div").click(function () { $(this).effect("highlight", {}, 3000); });