⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorutil.cs

📁 C#自定义查询控件
💻 CS
字号:
// Disclaimer and Copyright Information
// ColorUtil.cs : 
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.0
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@CommonComponent.com
///////////////////////////////////////////////////////////////////////////////
//

using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;

namespace CommonComponent.CommonChart
{
	/// <summary>
	/// Summary description for ColorUtil.
	/// </summary>
	public class ColorUtil
	{
		internal  ColorUtil()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="doc"></param>
		internal static bool GenerateRandomColors(int nSize, out ArrayList arrColors)
		{
			arrColors = null;
			Random rnd = new Random();
			try
			{
				//m_arrColors = new ArrayList();
				arrColors = new ArrayList();
				for (int i = 0; i < nSize; i++)
				{
					//m_arrColors.Add(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
					arrColors.Add(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
				}
			}
			catch (Exception ex)
			{
				Trace.Write(ex.Message);
				return false;
			}

			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="srcColor"></param>
		/// <param name="nFactor"></param>
		/// <returns></returns>
		internal  static Color GetDarkColor(Color srcColor, int nFactor)
		{
			try
			{
				int rColor = DarkenColor(srcColor.R, nFactor);
				int gColor = DarkenColor(srcColor.G, nFactor);
				int bColor = DarkenColor(srcColor.B, nFactor);

				return Color.FromArgb(rColor, gColor, bColor);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return Color.Black;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="srcColor"></param>
		/// <returns></returns>
		internal  static Color GetOpaqueColor(Color srcColor)
		{
			try
			{
				return Color.FromArgb(255, srcColor.R, srcColor.G, srcColor.B);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return srcColor;
			}
		}

		internal  static Color GetTransparentColor(Color srcColor)
		{
			try
			{
				return Color.FromArgb(0, srcColor.R, srcColor.G, srcColor.B);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return srcColor;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="val"></param>
		/// <param name="nFactor"></param>
		/// <returns></returns>
		internal  static int DarkenColor(Byte val, int nFactor)
		{
			int btNewVal = val + nFactor;
			if (btNewVal < 0)
			{
				btNewVal = 0;
			}
			else if (btNewVal > 255)
			{
				btNewVal = 255;
			}
			return btNewVal;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="colorName"></param>
		/// <returns></returns>
		internal  static Color GetColorValue(string colorName)
		{
			if (colorName == null || colorName.Length == 0)
			{
				throw new ArgumentException("Invalid color name specified", "colorName");
			}
			return Color.FromName(colorName);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strColorNames"></param>
		/// <param name="arrColors"></param>
		internal  static void GenerateColorValues(StringCollection strColorNames, out ArrayList arrColors)
		{
			int iCount = 0;
			arrColors = null;
			try
			{
				iCount = strColorNames.Count;
				if (iCount == 0)
				{
					throw new ArgumentException("String list containing color names is empty", "strColorName");
				}

				arrColors = new ArrayList(iCount);
				for (int i = 0; i < iCount; i++)
				{
					try
					{
						arrColors.Add(GetColorValue(strColorNames[i]));
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex.Message);
						// If an exception is thrown from GetColorValue for any reason,
						// then add Black color to collection.
						arrColors.Add(GetColorValue("Black"));
					}
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				throw ex;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -