Results 1 to 6 of 6

Thread: Get Color Name

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Get Color Name

    Hi, I've a color pick dialog form, and I need which color is selected by the user and get the name.

    I see that some colors have the Name property set to a system color name (yellow, red, etc...) but some others are in the RGB style.

    How can I do, to get a valid name or show only a set of colours with a valid system name ?

    Thank you.

    Hope you understand my problem and sorry for my english

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get Color Name

    This has ben answered before. I remember it. Try a forums search and you will find the thread and its source code example.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Color Name

    All Colors known by the system can be retrieved using the KnownColor enumeration. Just be aware that that enumeration includes SystemColors too, which are the colours assigned to theme elements.
    vb.net Code:
    1. Dim c As Color
    2.  
    3. For Each value As KnownColor In [Enum].GetValues(GetType(KnownColor))
    4.     c = Color.FromKnownColor(value)
    5.     MessageBox.Show(c.ToArgb().ToString("X"), c.Name)
    6. Next value
    You should note from running that code that all the SystemColors are listed first, so that should make it relatively easy to exclude those from your list if you so desire.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: Get Color Name

    Quote Originally Posted by jmcilhinney
    You should note from running that code that all the SystemColors are listed first, so that should make it relatively easy to exclude those from your list if you so desire.
    I don't understand sorry, I need to create a custom ColorDialog form for exclude the color without a system name ?

    This my simple code for get the color from the color dialog picker:

    Code:
      
            private void coloreToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    string col = colorDialog1.Color.Name;
                    functionColor(col);
                }
            }
    where I need to pass a valid color name to the functionColor.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Color Name

    You should be testing the IsNamedColor property of the selected Color.

    Note that it is considered good style to always test for a positive condition where possible, so the tenets of good style dictate that you change this:
    CSharp Code:
    1. if (colorDialog1.ShowDialog() != DialogResult.Cancel)
    to this:
    CSharp Code:
    1. if (colorDialog1.ShowDialog() == DialogResult.OK)
    By the way, sorry for my VB code before. I often forget that I'm in the C# forum. Call me goldfish.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Color Name

    In reading the documentation I see that the IsNamedColor property doesn't consider the RGB value of a Color, so even if the Color represents a named Color the property will still return False if the Color was created from an Argb value rather than a name. To see what I mean try this code:
    CSharp Code:
    1. Color c1 = Color.FromArgb(255, 255, 255, 255);
    2. Color c2 = Color.FromKnownColor(KnownColor.White);
    3. Color c3 = Color.FromName("White");
    4.  
    5. MessageBox.Show(c1.ToArgb().ToString("X"), c1.Name);
    6. MessageBox.Show(c2.ToArgb().ToString("X"), c2.Name);
    7. MessageBox.Show(c3.ToArgb().ToString("X"), c3.Name);
    All three Colors have the same Argb value but the first one has no common name.

    This might suggest that if the user created a custom Color using the dialogue then it would never have a common name, but in some brief testing that didn't seem to be the case. You'd want to test it out yourself to make sure but I'd suggest that the ColorDialog first compares the selected Argb value to KnownColors and then calls FromKnownColor if a match exists.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width