private Style FindCustomStyle(XElement block) { var child = block.Descendants(Namespace + "T").FirstOrDefault(); if (child != null) { var analyzer = new StyleAnalyzer(Root); var style = new Style(analyzer.CollectFrom(child, true)); // normalize style background to page background if (style.Highlight != pageColor && pageColor == "automatic") { if (style.Highlight == "#FFFFFF") { style.Highlight = "automatic"; } } // compare and find var find = customStyles.Find(s => s.Equals(style)); return(find); } return(null); }
private bool ClearTextBackground(IEnumerable <XElement> runs, bool deep = false) { var updated = false; foreach (var run in runs) { var style = new Style(analyzer.CollectFrom(run, true)); if (!string.IsNullOrEmpty(style.Highlight)) { style.Highlight = Style.Automatic; } var darkText = !style.Color.Equals(Style.Automatic) && ColorTranslator.FromHtml(style.Color).GetBrightness() < 0.5; // if dark-on-dark or light-on-light if (darkText == darkPage) { style.Color = darkText ? White : Black; } var stylizer = new Stylizer(style); stylizer.ApplyStyle(run); ClearHyperlinkBackground(run); updated = true; // deep prevents runs from being processed multiple times // NOTE sometimes OneNote will incorrectly set the collapsed attribute, // thinking it is set to 1 but is not visually collapsed! if (!deep && run.Parent.Attribute("collapsed")?.Value == "1") { updated |= ClearTextBackground( run.Parent.Descendants(ns + "T").Where(e => e != run), true); } } return(updated); }