/// <summary> /// 按比例,获取红蓝两色之间的渐变色,中间使用白色过渡,适用于表现气温、负载等 /// </summary> /// <param name="scale">比例, 0-1之间的数据</param> /// <returns></returns> public static Color getColorBetweenRedBlue(double scale) { float h, s; //Color c = System.Drawing.Color.FromArgb(0x10, 0x00, 0xDC); if (scale > 0.5) { float ss = (float)(scale - 0.5) * 2; h = 60f - 60f * ss; s = 255f * ss; } else { float ss = (float)scale * 2; h = 240 - 60 * ss; s = 255f * (1 - ss); } System.Drawing.Color dc = HSBColor.FromHSB(new HSBColor(255, h * 255 / 360, s, 255)); Color color = Color.FromArgb(dc.A, dc.R, dc.G, dc.B); return(color); }
public static Color FromHSB(HSBColor hsbColor) { float r = hsbColor.b; float g = hsbColor.b; float b = hsbColor.b; if (hsbColor.s != 0) { float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h * 360f / 255f; if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; } else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; } else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; } else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; } else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; } else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; } else { r = 0; g = 0; b = 0; } } return(Color.FromArgb ( hsbColor.a, (int)Math.Round(Math.Min(Math.Max(r, 0), 255)), (int)Math.Round(Math.Min(Math.Max(g, 0), 255)), (int)Math.Round(Math.Min(Math.Max(b, 0), 255)) )); }