C#中各种颜色转换工具 Color convert tool for c#

效果截图:

 

实现效果:

选择一个颜色,用三种不同的颜色进行转换:

  1. 用RGB(A,R,G,B)表示
  2. 用#十六进制表示
  3. 用Win32表示
  4. 使用Color转换后即ToArgb表示
  5. 批量将HTML转为ARGB
  6. 批量将ARGB转为HTML

其中批量将HTML转为ARGB格式如下,即一行一个颜色值:

#FF0000

#0000FF

……

批量将HTML转为ARGB格式如下,即一行一个颜色值:

-65536

-16776961

……

附上源代码:

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ColorConvert
{
	public class ColorConvert : Form
	{
		private IContainer components = null;

		private Label m_LabelRgb;

		private TextBox m_TxtRgb;

		private Panel m_PanelRgb;

		private Label label1;

		private TextBox m_TxtHTML;

		private Panel m_PanelHTML;

		private Label label2;

		private TextBox m_TxtWin32;

		private Panel m_PanelWin32;

		private Label label3;

		private TextBox m_TxtHTML2ARGB;

		private Label label4;

		private TextBox m_TxtWin32ToArgb;

		private Panel m_PanelUserColor;

		private Label label5;

		private Button m_BtnUserSelectColor;

		private GroupBox groupBox1;

		private Button m_CovertHTML2ArgbInCSV;
        private Button m_CovertArgb2HTMLInCSV;

		private ToolTip m_TooTipOfCovertHTML2ArgbInCSV;

        private string m_CovertHTML2ArgbInCSVText = string.Empty;
        private string m_CovertArgb2HTMLInCSVText = string.Empty;

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			this.m_LabelRgb = new Label();
			this.m_TxtRgb = new TextBox();
			this.m_PanelRgb = new Panel();
			this.label1 = new Label();
			this.m_TxtHTML = new TextBox();
			this.m_PanelHTML = new Panel();
			this.label2 = new Label();
			this.m_TxtWin32 = new TextBox();
			this.m_PanelWin32 = new Panel();
			this.label3 = new Label();
			this.m_TxtHTML2ARGB = new TextBox();
			this.label4 = new Label();
			this.m_TxtWin32ToArgb = new TextBox();
			this.m_PanelUserColor = new Panel();
			this.label5 = new Label();
			this.m_BtnUserSelectColor = new Button();
            this.groupBox1 = new GroupBox();
            this.m_CovertHTML2ArgbInCSV = new Button();
            this.m_CovertArgb2HTMLInCSV = new Button();
			this.m_TooTipOfCovertHTML2ArgbInCSV = new ToolTip(this.components);
			this.groupBox1.SuspendLayout();
			base.SuspendLayout();
			this.m_LabelRgb.AutoSize = true;
			this.m_LabelRgb.Location = new Point(32, 55);
			this.m_LabelRgb.Name = "m_LabelRgb";
			this.m_LabelRgb.Size = new Size(23, 12);
			this.m_LabelRgb.TabIndex = 0;
			this.m_LabelRgb.Text = "RGB";
			this.m_TxtRgb.Location = new Point(61, 52);
			this.m_TxtRgb.Name = "m_TxtRgb";
			this.m_TxtRgb.Size = new Size(176, 21);
			this.m_TxtRgb.TabIndex = 1;
			this.m_PanelRgb.BorderStyle = BorderStyle.FixedSingle;
			this.m_PanelRgb.Location = new Point(259, 52);
			this.m_PanelRgb.Name = "m_PanelRgb";
			this.m_PanelRgb.Size = new Size(26, 21);
			this.m_PanelRgb.TabIndex = 2;
			this.label1.AutoSize = true;
			this.label1.Location = new Point(32, 101);
			this.label1.Name = "label1";
			this.label1.Size = new Size(29, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "HTML";
			this.m_TxtHTML.Location = new Point(61, 98);
			this.m_TxtHTML.Name = "m_TxtHTML";
			this.m_TxtHTML.Size = new Size(176, 21);
			this.m_TxtHTML.TabIndex = 1;
			this.m_PanelHTML.BorderStyle = BorderStyle.FixedSingle;
			this.m_PanelHTML.Location = new Point(259, 98);
			this.m_PanelHTML.Name = "m_PanelHTML";
			this.m_PanelHTML.Size = new Size(26, 21);
			this.m_PanelHTML.TabIndex = 2;
			this.label2.AutoSize = true;
			this.label2.Location = new Point(29, 143);
			this.label2.Name = "label2";
			this.label2.Size = new Size(35, 12);
			this.label2.TabIndex = 0;
			this.label2.Text = "Win32";
			this.m_TxtWin32.Location = new Point(61, 140);
			this.m_TxtWin32.Name = "m_TxtWin32";
			this.m_TxtWin32.Size = new Size(176, 21);
			this.m_TxtWin32.TabIndex = 1;
			this.m_PanelWin32.BorderStyle = BorderStyle.FixedSingle;
			this.m_PanelWin32.Location = new Point(259, 140);
			this.m_PanelWin32.Name = "m_PanelWin32";
			this.m_PanelWin32.Size = new Size(26, 21);
			this.m_PanelWin32.TabIndex = 2;
			this.label3.AutoSize = true;
			this.label3.Location = new Point(315, 101);
			this.label3.Name = "label3";
			this.label3.Size = new Size(41, 12);
			this.label3.TabIndex = 0;
			this.label3.Text = "ToArgb";
			this.m_TxtHTML2ARGB.Location = new Point(359, 98);
			this.m_TxtHTML2ARGB.Name = "m_TxtHTML2ARGB";
			this.m_TxtHTML2ARGB.Size = new Size(176, 21);
			this.m_TxtHTML2ARGB.TabIndex = 1;
			this.label4.AutoSize = true;
			this.label4.Location = new Point(315, 143);
			this.label4.Name = "label4";
			this.label4.Size = new Size(41, 12);
			this.label4.TabIndex = 0;
			this.label4.Text = "ToArgb";
			this.m_TxtWin32ToArgb.Location = new Point(359, 140);
			this.m_TxtWin32ToArgb.Name = "m_TxtWin32ToArgb";
			this.m_TxtWin32ToArgb.Size = new Size(176, 21);
			this.m_TxtWin32ToArgb.TabIndex = 1;
			this.m_PanelUserColor.BorderStyle = BorderStyle.FixedSingle;
			this.m_PanelUserColor.Location = new Point(259, 4);
			this.m_PanelUserColor.Name = "m_PanelUserColor";
			this.m_PanelUserColor.Size = new Size(26, 21);
			this.m_PanelUserColor.TabIndex = 2;
			this.label5.AutoSize = true;
			this.label5.Location = new Point(178, 9);
			this.label5.Name = "label5";
			this.label5.Size = new Size(59, 12);
			this.label5.TabIndex = 0;
			this.label5.Text = "UserColor";
			this.m_BtnUserSelectColor.Location = new Point(285, 4);
			this.m_BtnUserSelectColor.Name = "m_BtnUserSelectColor";
			this.m_BtnUserSelectColor.Size = new Size(25, 21);
			this.m_BtnUserSelectColor.TabIndex = 3;
			this.m_BtnUserSelectColor.Text = " ";
			this.m_BtnUserSelectColor.UseVisualStyleBackColor = true;
			this.m_BtnUserSelectColor.Click += this.m_BtnUserSelectColor_Click;
            this.groupBox1.Controls.Add(this.m_CovertHTML2ArgbInCSV);
            this.groupBox1.Controls.Add(this.m_CovertArgb2HTMLInCSV);
			this.groupBox1.Location = new Point(34, 180);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(406, 51);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "HTML Color to ARGB in CSV:";
			this.m_CovertHTML2ArgbInCSV.Location = new Point(9, 18);
			this.m_CovertHTML2ArgbInCSV.Name = "m_CovertHTML2ArgbInCSV";
			this.m_CovertHTML2ArgbInCSV.Size = new Size(188, 23);
			this.m_CovertHTML2ArgbInCSV.TabIndex = 2;
			this.m_CovertHTML2ArgbInCSV.Text = "Select CSV 2 RGB";
			this.m_CovertHTML2ArgbInCSV.UseVisualStyleBackColor = true;
			this.m_CovertHTML2ArgbInCSV.Click += this.m_CovertHTML2ArgbInCSV_Click;
			this.m_CovertHTML2ArgbInCSV.MouseHover += this.m_CovertHTML2ArgbInCSV_MouseHover;



            this.m_CovertArgb2HTMLInCSV.Location = new Point(200, 18);
            this.m_CovertArgb2HTMLInCSV.Name = "m_CovertArgb2HTMLInCSV";
            this.m_CovertArgb2HTMLInCSV.Size = new Size(188, 23);
            this.m_CovertArgb2HTMLInCSV.TabIndex = 2;
            this.m_CovertArgb2HTMLInCSV.Text = "Select CSV 2 HTML";
            this.m_CovertArgb2HTMLInCSV.UseVisualStyleBackColor = true;
            this.m_CovertArgb2HTMLInCSV.Click += this.m_CovertArgb2HTMLInCSV_Click;


			this.m_TooTipOfCovertHTML2ArgbInCSV.ToolTipTitle = "csv content e.g.";
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(566, 243);
			base.Controls.Add(this.groupBox1);
			base.Controls.Add(this.m_BtnUserSelectColor);
			base.Controls.Add(this.m_PanelWin32);
			base.Controls.Add(this.m_PanelHTML);
			base.Controls.Add(this.m_PanelUserColor);
			base.Controls.Add(this.m_PanelRgb);
			base.Controls.Add(this.m_TxtWin32ToArgb);
			base.Controls.Add(this.label4);
			base.Controls.Add(this.m_TxtWin32);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.m_TxtHTML2ARGB);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.m_TxtHTML);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.m_TxtRgb);
			base.Controls.Add(this.label5);
			base.Controls.Add(this.m_LabelRgb);
			base.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "ColorConvert";
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "ColorConvert";
			this.groupBox1.ResumeLayout(false);
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		public ColorConvert()
		{
			this.InitializeComponent();
			this.RegEvent();
			this.m_PanelUserColor.BackColor = Color.Red;
		}

		private string Color2ARGB(Color c)
		{
			int a = c.A;
			int r = c.R;
			int g = c.G;
			int b = c.B;
			return "RGB(" + a + "," + r + "," + g + "," + b + ")";
		}

		private void RegEvent()
		{
			this.m_TxtRgb.TextChanged += this.m_TxtRgb_TextChanged;
			this.m_TxtHTML.TextChanged += this.m_TxtHTML_TextChanged;
			this.m_TxtWin32.TextChanged += this.m_TxtWin32_TextChanged;
			this.m_PanelUserColor.BackColorChanged += this.m_PanelUserColor_BackColorChanged;
		}

		private void m_PanelUserColor_BackColorChanged(object sender, EventArgs e)
		{
			this.m_TxtRgb.Text = this.Color2ARGB(this.m_PanelUserColor.BackColor);
		}

		private void m_TxtWin32_TextChanged(object sender, EventArgs e)
		{
			int win32Color = 0;
			int.TryParse(this.m_TxtWin32.Text, out win32Color);
			this.m_PanelWin32.BackColor = ColorTranslator.FromWin32(win32Color);
			this.m_TxtWin32ToArgb.Text = this.m_PanelWin32.BackColor.ToArgb().ToString();
			if (this.m_TxtWin32.Focused)
			{
				this.m_TxtRgb.Text = this.Color2ARGB(this.m_PanelWin32.BackColor);
			}
		}

		private void m_TxtHTML_TextChanged(object sender, EventArgs e)
		{
			try
			{
				this.m_PanelHTML.BackColor = ColorTranslator.FromHtml(this.m_TxtHTML.Text);
				this.m_TxtHTML2ARGB.Text = this.m_PanelHTML.BackColor.ToArgb().ToString();
				if (this.m_TxtHTML2ARGB.Focused)
				{
					this.m_TxtRgb.Text = this.Color2ARGB(this.m_PanelHTML.BackColor);
				}
			}
			catch (Exception)
			{
			}
		}

		private void m_TxtRgb_TextChanged(object sender, EventArgs e)
		{
			try
			{
				string text = this.m_TxtRgb.Text.ToUpper();
				if (text.StartsWith("RGB") && text.EndsWith(")"))
				{
					string text2 = text.Replace("RGB", "").Replace("(", "").Replace(")", "");
					string[] array = text2.Split(new char[1]
					{
						','
					}, StringSplitOptions.RemoveEmptyEntries);
					if (array.Length >= 3)
					{
						if (array.Length == 3)
						{
							int red = int.Parse(array[0]);
							int green = int.Parse(array[1]);
							int blue = int.Parse(array[2]);
							this.m_PanelRgb.BackColor = Color.FromArgb(red, green, blue);
						}
						else if (array.Length == 4)
						{
							int alpha = int.Parse(array[0]);
							int red = int.Parse(array[1]);
							int green = int.Parse(array[2]);
							int blue = int.Parse(array[3]);
							this.m_PanelRgb.BackColor = Color.FromArgb(alpha, red, green, blue);
						}
						if (!this.m_TxtHTML.Focused)
						{
							this.m_TxtHTML.Text = ColorTranslator.ToHtml(this.m_PanelRgb.BackColor);
						}
						if (!this.m_TxtWin32.Focused)
						{
							this.m_TxtWin32.Text = ColorTranslator.ToWin32(this.m_PanelRgb.BackColor).ToString();
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void m_BtnUserSelectColor_Click(object sender, EventArgs e)
		{
			using (ColorDialog colorDialog = new ColorDialog())
			{
				if (colorDialog.ShowDialog() == DialogResult.OK)
				{
					this.m_PanelUserColor.BackColor = colorDialog.Color;
				}
			}
		}

		private void m_CovertHTML2ArgbInCSV_MouseHover(object sender, EventArgs e)
		{
			string str = "#FF0000\n";
			str += "#FF0010\n";
			str += "#0F00F0\n";
			str += "#3FF010\n";
			str += "#020F1E\n";
			this.m_TooTipOfCovertHTML2ArgbInCSV.Show(str, sender as Control);
		}

		private void m_CovertHTML2ArgbInCSV_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.m_CovertHTML2ArgbInCSVText))
			{
				this.m_CovertHTML2ArgbInCSVText = this.m_CovertHTML2ArgbInCSV.Text;
			}
			using (FileDialog fileDialog = new OpenFileDialog())
			{
				fileDialog.Filter = "csv文件|*.csv|所有文件|*.*";
				if (fileDialog.ShowDialog() == DialogResult.OK)
				{
					this.m_CovertHTML2ArgbInCSV.Text = "Coverting....";
					this.m_CovertHTML2ArgbInCSV.Enabled = false;
					string fileName = fileDialog.FileName;
					try
					{
						this.CovertColorInCSV(fileName);
					}
					catch (Exception)
					{
					}
				}
			}
			this.m_CovertHTML2ArgbInCSV.Enabled = true;
			this.m_CovertHTML2ArgbInCSV.Text = this.m_CovertHTML2ArgbInCSVText;
		}


        private void m_CovertArgb2HTMLInCSV_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.m_CovertArgb2HTMLInCSVText))
            {
                this.m_CovertArgb2HTMLInCSVText = this.m_CovertArgb2HTMLInCSV.Text;
            }
            using (FileDialog fileDialog = new OpenFileDialog())
            {
                fileDialog.Filter = "csv文件|*.csv|所有文件|*.*";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.m_CovertArgb2HTMLInCSV.Text = "Coverting....";
                    this.m_CovertArgb2HTMLInCSV.Enabled = false;
                    string fileName = fileDialog.FileName;
                    try
                    {
                        this.CovertColorInCSV2(fileName);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            this.m_CovertArgb2HTMLInCSV.Enabled = true;
            this.m_CovertArgb2HTMLInCSV.Text = this.m_CovertArgb2HTMLInCSVText;
        }

		private bool CovertColorInCSV(string csvFilePath)
		{
			string str = csvFilePath.Substring(csvFilePath.LastIndexOf(".") + 1, csvFilePath.Length - csvFilePath.LastIndexOf(".") - 1);
			string text = csvFilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + str;
			using (StreamWriter streamWriter = new StreamWriter(text))
			{
				using (StreamReader streamReader = new StreamReader(csvFilePath))
				{
					string htmlColor;
					while ((htmlColor = streamReader.ReadLine()) != null)
					{
						streamWriter.WriteLine(ColorTranslator.FromHtml(htmlColor).ToArgb());
					}
				}
				streamWriter.Flush();
				streamWriter.Close();
				MessageBox.Show("Output File: \n" + text, "提示");
			}
			return true;
		}
        private bool CovertColorInCSV2(string csvFilePath)
        {
            string str = csvFilePath.Substring(csvFilePath.LastIndexOf(".") + 1, csvFilePath.Length - csvFilePath.LastIndexOf(".") - 1);
            string text = csvFilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + str;
            using (StreamWriter streamWriter = new StreamWriter(text))
            {
                using (StreamReader streamReader = new StreamReader(csvFilePath))
                {
                    string htmlColor;
                    while ((htmlColor = streamReader.ReadLine()) != null)
                    {
                        streamWriter.WriteLine(ToHexColor(ColorTranslator.FromHtml(htmlColor)));
                    }
                }
                streamWriter.Flush();
                streamWriter.Close();
                MessageBox.Show("Output File: \n" + text, "提示");
            }
            return true;
        }
        private string ToHexColor(Color color)
        {
            string R = Convert.ToString(color.R, 16);
            if (R == "0")
                R = "00";
            string G = Convert.ToString(color.G, 16);
            if (G == "0")
                G = "00";
            string B = Convert.ToString(color.B, 16);
            if (B == "0")
                B = "00";
            string HexColor = "#" + R + G + B;
            return HexColor.ToUpper();
        }
	}
}

使用:

(new ColorConvert()).Show();

工具下载链接:

C#中各种颜色转换工具Colorconverttoolforc#_c#颜色转换-其它工具类资源-CSDN下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丷丩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值