Asked  7 Months ago    Answers:  5   Viewed   51 times

How can I get a color from a hexadecimal color code (e.g. #FFDFD991)?

I am reading a file and am getting a hexadecimal color code. I need to create the corresponding System.Windows.Media.Color instance for the hexadecimal color code. Is there an inbuilt method in the framework to do this?

 Answers

73

I'm assuming that's an ARGB code... Are you referring to System.Drawing.Color or System.Windows.Media.Color? The latter is used in WPF for example. I haven't seen anyone mention it yet, so just in case you were looking for it:

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");
Tuesday, June 1, 2021
 
Evernoob
answered 7 Months ago
14

Here's a correct table of percentages to hex values. E.g. for 50% white you'd use #80FFFFFF.

  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00

(source)

Tuesday, June 1, 2021
 
Skipper
answered 7 Months ago
64

If you've definitely got an absolute path, use Path.GetDirectoryName(path).

If you might only get a relative name, use new FileInfo(path).Directory.FullName.

Note that Path and FileInfo are both found in the namespace System.IO.

Wednesday, June 9, 2021
 
motanelu
answered 6 Months ago
100

I ran across my own answer from two years ago.

NameValueSectionHandler - can i use this section type for writing back to the application config file?

This is my approach to solve my current issue.

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { 
   ExeConfigFilename = "path to config here" 
    };

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
   configFileMap, ConfigurationUserLevel.None);

ConfigurationSection myParamsSection = config.GetSection("MyParams");

string myParamsSectionRawXml = myParamsSection .SectionInformation.GetRawXml();
XmlDocument sectionXmlDoc = new XmlDocument();
sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml ));
NameValueSectionHandler handler = new NameValueSectionHandler();

NameValueCollection handlerCreatedCollection = 
   handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection;

Console.WriteLine(handlerCreatedCollection.Count);

A method that works with any of the legacy IConfigurationSectionHandler types NameValueSectionHandler, DictionarySectionHandler, SingleTagSectionHandler.

public static object GetConfigurationValues(string configFileName, string sectionName)
{
    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configFileName };
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    ConfigurationSection section = config.GetSection(sectionName);
    string xml = section.SectionInformation.GetRawXml();
    XmlDocument doc = new XmlDocument();
    doc.Load(XmlReader.Create(new StringReader(xml)));
    string type = section.SectionInformation.Type;
    string assemblyName = typeof(IConfigurationSectionHandler).Assembly.GetName().FullName;
    ObjectHandle configSectionHandlerHandle = Activator.CreateInstance(assemblyName, section.SectionInformation.Type);
    if (configSectionHandlerHandle != null)
    {
        IConfigurationSectionHandler handler = configSectionHandlerHandle.Unwrap() as IConfigurationSectionHandler;
        return handler.Create(null, null, doc.DocumentElement);
    }
    return null;
}
Tuesday, August 17, 2021
 
Yu Sasaki
answered 4 Months ago
96

Just try this:

TextRange tr = new TextRange(rtb.Document.ContentEnd,­ rtb.Document.ContentEnd);
tr.Text = "textToColorize";
tr.ApplyPropertyValue(TextElement.­ForegroundProperty, Brushes.Red);
Saturday, September 11, 2021
 
Stephtheboss
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share