简体   繁体   中英

C# color name from argb

I have 2 ownerdrawn combo boxes. One is a line color picker- Here in the picture it is not a combo box. The other being a linestyle selector. Once the user picks a color in the first combo, the linestyle selector combo (See second picture, the last combo(one with noname)) must have the same colored lines.

在此处输入图片说明

Currently it shows like this

在此处输入图片说明 Here's the code for both combos Linecolor picker

    public partial class ColorPicker : ComboBox
    {
        // Data for each color in the list
        public class ColorInfo
        {
            public string Text { get; set; }
            public Color Color { get; set; }

            public ColorInfo(string text, Color color)
            {
                Text = text;
                Color = color;
            }
        }

        public ColorPicker()
        {
            InitializeComponent();

            DropDownStyle = ComboBoxStyle.DropDownList;
            DrawMode = DrawMode.OwnerDrawFixed;
            DrawItem += OnDrawItem;
        }

        // Populate control with standard colors
        public void AddStandardColors()
        {
            Items.Clear();
            Items.Add(new ColorInfo("Black", Color.Black));
            Items.Add(new ColorInfo("Blue", Color.Blue));
            Items.Add(new ColorInfo("Lime", Color.Lime));
            Items.Add(new ColorInfo("Cyan", Color.Cyan));
            Items.Add(new ColorInfo("Red", Color.Red));
            Items.Add(new ColorInfo("Fuchsia", Color.Fuchsia));
            Items.Add(new ColorInfo("Yellow", Color.Yellow));
            Items.Add(new ColorInfo("White", Color.White));
            Items.Add(new ColorInfo("Navy", Color.Navy));
            Items.Add(new ColorInfo("Green", Color.Green));
            Items.Add(new ColorInfo("Teal", Color.Teal));
            Items.Add(new ColorInfo("Maroon", Color.Maroon));
            Items.Add(new ColorInfo("Purple", Color.Purple));
            Items.Add(new ColorInfo("Olive", Color.Olive));
            Items.Add(new ColorInfo("Gray", Color.Gray));
        }

        // Draw list item
        protected void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                // Get this color
                ColorInfo color = (ColorInfo)Items[e.Index];

                // Fill background
                e.DrawBackground();

                // Draw color box
                Rectangle rect = new Rectangle();
                rect.X = e.Bounds.X + 2;
                rect.Y = e.Bounds.Y + 2;
                rect.Width = 18;
                rect.Height = e.Bounds.Height - 5;
                e.Graphics.FillRectangle(new SolidBrush(color.Color), rect);
                e.Graphics.DrawRectangle(SystemPens.WindowText, rect);

                // Write color name
                Brush brush;
                if ((e.State & DrawItemState.Selected) != DrawItemState.None)
                    brush = SystemBrushes.HighlightText;
                else
                    brush = SystemBrushes.WindowText;
                e.Graphics.DrawString(color.Text, Font, brush,
                    e.Bounds.X + rect.X + rect.Width + 2,
                    e.Bounds.Y + ((e.Bounds.Height - Font.Height) / 2));

                // Draw the focus rectangle if appropriate
                if ((e.State & DrawItemState.NoFocusRect) == DrawItemState.None)
                    e.DrawFocusRectangle();
            }
        }

        /// <summary>
        /// Gets or sets the currently selected item.
        /// </summary>
        public new ColorInfo SelectedItem
        {
            get
            {
                return (ColorInfo)base.SelectedItem;
            }
            set
            {
                base.SelectedItem = value;
            }
        }

        /// <summary>
        /// Gets the text of the selected item, or sets the selection to
        /// the item with the specified text.
        /// </summary>
        public new string SelectedText
        {
            get
            {
                if (SelectedIndex >= 0)
                    return SelectedItem.Text;
                return String.Empty;
            }
            set
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    if (((ColorInfo)Items[i]).Text == value)
                    {
                        SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the value of the selected item, or sets the selection to
        /// the item with the specified value.
        /// </summary>
        public new int SelectedValue
        {
            get
            {
                if (SelectedIndex >= 0)
                    return SelectedItem.Color.ToArgb();
                return Color.White.ToArgb();
            }
            set
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    if (((ColorInfo)Items[i]).Color.ToArgb() == value)
                    {
                        SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }
}

Code for lineselector combo. This is the last combo(the one with no name) on my second picture. : `


    public partial class comboBoxLineStyle : ComboBox
    {
        public comboBoxLineStyle()
        {
            InitializeComponent();
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;


        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (e.Index < 0) { return; }
            e.DrawBackground();
            ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
           e.Graphics.DrawImage(item.Picture,new Point(e.Bounds.X, e.Bounds.Y));

        }
        public new Image SelectedItem
        {
            get
            {
                return (Image)base.SelectedItem;
            }
            set
            {
                base.SelectedItem = value;
            }
        }
        public new Image SelectedValue
        {
            get
            {
                return (Image)base.SelectedValue;
            }
            set
            {
                base.SelectedValue = value;
            }
        }

    }

    public class ComboBoxItem
    {
        public string text;
        public Image Picture;
        public int foreColor;
        //public override string ToString()
        //{
        //    return text;
        //}

        public ComboBoxItem() { }
        public ComboBoxItem(string pText, Image pValue)
        {
            text = pText;
            Picture = pValue;
        }
        public ComboBoxItem(string pText, Image pValue, int pColor)
        {

            text = pText; Picture = pValue; foreColor = pColor;

        }


    }
}

in the form I am accessing these as follows:

private void DlgGraphOptions_Load(object sender, EventArgs e)
        {

            Color selCol = m_SerOpts.GetColor(idxSeries);
            int selcolARGB = selCol.ToArgb();
            Color color = Color.FromArgb(selcolARGB);

            ComboBoxItem item1Solid = new ComboBoxItem("Solid Line",Properties.Resources.Solidline,selcolARGB);
            ComboBoxItem item1dash = new ComboBoxItem("Dashed Line", Properties.Resources.dashedline,selcolARGB);
            ComboBoxItem item1dashed = new ComboBoxItem("Dashed Line", Properties.Resources.dashdash,selcolARGB);

             comboBoxLineStyle1.Items.Add(item1Solid);
            comboBoxLineStyle1.Items.Add(item1dash);
            comboBoxLineStyle1.Items.Add(item1dashed);
            comboBoxLineStyle1.SelectedIndex = 0;



              //I prefer doing something like this
comboBoxLineStyle1.ForeColor.ToArgb() = selcolARGB .


            }

The m_SerOpts.GetColor(idxSeries) method returns a color object but returns a '00fff00' kind of value for Name. Pls lemme know if u need more explanation

`

Thank u

I would use reflection to populate a list with all colours from the Color class (although you could copy this in code):

Dictionary<String, Color> colors = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public).ToDictionary(p => p.Name, p => (Color)p.GetValue(null, null) );

then use a method to search this dictionary for a color which matches a passed color:

public String GetColorName(Color color)
{
    return colors.Where(c => c.Value.A == color.A && c.Value.R == color.R && c.Value.G == color.G && c.Value.B == color.B).FirstOrDefault().Key;
}

If no color is matched, null will be returned.

只是,

System.Drawing.ColorTranslator.FromHtml("#FFFFFF").Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM