Color.FromKnownColor : Color « System.Drawing « C# / C Sharp by API






Color.FromKnownColor

  


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ColorChanger
{
    public class ColorChanger : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label lblSaturation;
        internal System.Windows.Forms.Label lblHue;
        internal System.Windows.Forms.Label lblBrightness;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.ListBox lstColors;

        private System.ComponentModel.Container components = null;

        public ColorChanger()
        {
            InitializeComponent();
        }

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

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.lblSaturation = new System.Windows.Forms.Label();
            this.lblHue = new System.Windows.Forms.Label();
            this.lblBrightness = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.lstColors = new System.Windows.Forms.ListBox();
            this.SuspendLayout();

            this.lblSaturation.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblSaturation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblSaturation.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblSaturation.Location = new System.Drawing.Point(264, 56);
            this.lblSaturation.Name = "lblSaturation";
            this.lblSaturation.Size = new System.Drawing.Size(136, 20);
            this.lblSaturation.TabIndex = 9;
            this.lblSaturation.Text = " Saturation";

            this.lblHue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblHue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblHue.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblHue.Location = new System.Drawing.Point(264, 32);
            this.lblHue.Name = "lblHue";
            this.lblHue.Size = new System.Drawing.Size(136, 20);
            this.lblHue.TabIndex = 8;
            this.lblHue.Text = " Hue";

            this.lblBrightness.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblBrightness.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblBrightness.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblBrightness.Location = new System.Drawing.Point(264, 8);
            this.lblBrightness.Name = "lblBrightness";
            this.lblBrightness.Size = new System.Drawing.Size(136, 20);
            this.lblBrightness.TabIndex = 7;
            this.lblBrightness.Text = " Brightness";

            this.Label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Label1.Location = new System.Drawing.Point(8, 8);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(200, 20);
            this.Label1.TabIndex = 6;
            this.Label1.Text = " Choose a Background Color:";

            this.lstColors.Location = new System.Drawing.Point(8, 36);
            this.lstColors.Name = "lstColors";
            this.lstColors.Size = new System.Drawing.Size(200, 238);
            this.lstColors.TabIndex = 5;
            this.lstColors.SelectedIndexChanged += new System.EventHandler(this.lstColors_SelectedIndexChanged);

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(472, 290);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lblSaturation,
                                                                          this.lblHue,
                                                                          this.lblBrightness,
                                                                          this.Label1,
                                                                          this.lstColors});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ColorChanger";
            this.Text = "Color Changer";
            this.Load += new System.EventHandler(this.ColorChanger_Load);
            this.ResumeLayout(false);

        }
        #endregion
        [STAThread]
        static void Main() 
        {
            Application.Run(new ColorChanger());
        }

        private void ColorChanger_Load(object sender, System.EventArgs e)
        {
            string[] colorNames;
            colorNames = System.Enum.GetNames(typeof(KnownColor));

            lstColors.Items.AddRange(colorNames);
        }

        private void lstColors_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            KnownColor selectedColor;
            selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), lstColors.Text);
            
            this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);
            
            lblBrightness.Text = "Brightness = " +this.BackColor.GetBrightness().ToString();
            lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
            lblSaturation.Text = "Saturation = " + this.BackColor.GetSaturation().ToString();
        }
    }
}

   
    
  








Related examples in the same category

1.Color.AliceBlue
2.Color.Chocolate
3.Color.FromArgb (int r, int g, int b)
4.Color.FromArgb(100, Color.Blue)
5.Color.FromArgb(77, color.R, color.G, color.B)
6.Color.FromName
7.Color.LightGray
8.Color.Tomato
9.Color.White