RandomColor.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Drawing;
  3. namespace Microsoft.Windows.Forms
  4. {
  5. /// <summary>
  6. /// 随机颜色生成器
  7. /// </summary>
  8. public class RandomColor
  9. {
  10. /// <summary>
  11. /// 黄金分割比例
  12. /// </summary>
  13. private const float GOLDEN_RATIO = 0.618033988749895f;
  14. /// <summary>
  15. /// 随机颜色,Hue 起始值
  16. /// </summary>
  17. private float? m_Hue;
  18. /// <summary>
  19. /// 获取随机颜色
  20. /// </summary>
  21. /// <param name="saturation">饱和度[0-1]</param>
  22. /// <param name="brightness">亮度[0-1]</param>
  23. /// <returns>颜色</returns>
  24. public Color Next(float saturation, float brightness)
  25. {
  26. if (this.m_Hue == null)
  27. {
  28. Random random = new Random(unchecked((int)DateTime.Now.Ticks));
  29. this.m_Hue = (float)random.NextDouble();
  30. }
  31. float hue = this.m_Hue.Value;
  32. hue += GOLDEN_RATIO;
  33. hue %= 1;
  34. this.m_Hue = hue;
  35. return RenderEngine.FromHsv(hue, saturation, brightness);
  36. }
  37. /// <summary>
  38. /// 获取随机颜色
  39. /// </summary>
  40. /// <returns>颜色</returns>
  41. public Color Next()
  42. {
  43. return this.Next(0.5f, 0.99f);
  44. }
  45. }
  46. }