颜色渐变取中间色(验证通过)
C#
/**
* 计算渐变颜色值 ARGB
*
* @param fraction
* 变化比率 0~1
* @param startValue
* 初始色值
* @param endValue
* 结束色值
* @return
*/
private Integer evaluate(float fraction, Object startValue, Integer endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = (Integer) endValue;
int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;
return (int) ((startA + (int) (fraction * (endA - startA))) << 24)
| (int) ((startR + (int) (fraction * (endR - startR))) << 16)
| (int) ((startG + (int) (fraction * (endG - startG))) << 8)
| (int) ((startB + (int) (fraction * (endB - startB))));
}
VB.net
Private Sub VScrollBarAdv1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles VScrollBarAdv1.ValueChanged
Dim pFraction As Single = (VScrollBarAdv1.Value - VScrollBarAdv1.Minimum) / (VScrollBarAdv1.Maximum - VScrollBarAdv1.Minimum)
Dim pColorEvaluate As Integer = getEvaluateColor(pFraction, ButtonX1.BackColor.ToArgb(), ButtonX2.BackColor.ToArgb)
ButtonX3.BackColor = Color.FromArgb(pColorEvaluate)
End Sub
Private Function getEvaluateColor(ByVal pFraction As Single, ByVal pStartColor As Integer, ByVal pEndColor As Integer) As Integer
Dim pStartA As Integer = (pStartColor >> 24) And &HFF
Dim pStartR As Integer = (pStartColor >> 16) And &HFF
Dim pStartG As Integer = (pStartColor >> 8) And &HFF
Dim pStartB As Integer = (pStartColor) And &HFF
Dim pEndA As Integer = (pEndColor >> 24) And &HFF
Dim pEndR As Integer = (pEndColor >> 16) And &HFF
Dim pEndG As Integer = (pEndColor >> 8) And &HFF
Dim pEndB As Integer = (pEndColor) And &HFF
Return (Convert.ToInt32(pStartA + ((pEndA - pStartA) * pFraction)) << 24) + _
(Convert.ToInt32(pStartR + ((pEndR - pStartR) * pFraction)) << 16) + _
(Convert.ToInt32(pStartG + ((pEndG - pStartG) * pFraction)) << 8) + _
Convert.ToInt32(pStartB + ((pEndB - pStartB) * pFraction))
End Function