C#颜色合成器

来源:互联网 发布:网络与狗带是什么意思 编辑:程序博客网 时间:2024/05/22 08:00


首先来看一下完成后的界面,通过调节R,G,B三个滑动条可以设置三种颜色的混合比例;滑动Alpha滑动条可以调节背景色的透明度,而且四个滑动条的取值最大值都是255.

当我们滑动滑动条的时候label的背景颜色也会相应的发生改变,你也可以在后面的文本框中输入,按回车就会自动执行了。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace sy17{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        int r=0, g=0, b=0,o=255;      //自己定义的ARGB三个变量        public void hecheng() {       //颜色合成函数            label1.BackColor = Color.FromArgb(o, r, g, b);        }        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)        {            o = Int32.Parse(textBox4.Text);        }        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)        {            textBox1.Text = hScrollBar1.Value.ToString();            r = Int32.Parse(textBox1.Text);            hecheng();        }        private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)        {            textBox2.Text = hScrollBar2.Value.ToString();            g = Int32.Parse(textBox2.Text);            hecheng();        }        private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)        {            textBox3.Text = hScrollBar3.Value.ToString();            b = Int32.Parse(textBox3.Text);            hecheng();        }        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)        {            if(e.KeyChar==13)                 hecheng();        }        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)        {            textBox4.Text =vScrollBar1.Value.ToString();            o = Int32.Parse(textBox4.Text);            hecheng();        }        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)                hecheng();        }        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == 13)                hecheng();        }        private void Form1_Load(object sender, EventArgs e)        {            hecheng();        }        private void textBox4_TextChanged(object sender, EventArgs e)        {        }    }}
其他没什么,注意的是

 int r=0, g=0, b=0,o=255;        public void hecheng() {            label1.BackColor = Color.FromArgb(o, r, g, b);        }
在这里我设置了四个全局变量,r,g,b,o,分别是红色,绿色,蓝色,透明度;另外定义了一个函数,用于合成颜色。
 Color.FromArgb(o, r, g, b)//共有四个属性,分别为透明度,红色,绿色,蓝色,这样就合成了一种颜色。
请指正》》》》

0 0
原创粉丝点击