|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
using System.Windows; |
|
|
using System.Windows.Documents; |
|
|
using System.Windows.Media; |
|
|
|
|
|
namespace HtmlToFlowDocument.Rendering |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public class FlowDocumentColorInverter |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void InvertColorsRecursively(FrameworkContentElement parentElement) |
|
|
{ |
|
|
if (null == parentElement) |
|
|
return; |
|
|
|
|
|
foreach (var child in WpfHelper.GetImmediateChildsOf(parentElement)) |
|
|
InvertColorsRecursively(child); |
|
|
|
|
|
switch (parentElement) |
|
|
{ |
|
|
case TextElement block: |
|
|
if (block.ReadLocalValue(TextElement.ForegroundProperty) != DependencyProperty.UnsetValue) |
|
|
{ |
|
|
block.Foreground = GetBrushWithInvertColors(block.Foreground); |
|
|
} |
|
|
if (block.ReadLocalValue(TextElement.BackgroundProperty) != DependencyProperty.UnsetValue) |
|
|
{ |
|
|
block.Background = GetBrushWithInvertColors(block.Background); |
|
|
} |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Color InvertColor(Color color) |
|
|
{ |
|
|
return Color.FromArgb(color.A, (byte)(255 - color.R), (byte)(255 - color.G), (byte)(255 - color.B)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Brush GetBrushWithInvertColors(Brush brush) |
|
|
{ |
|
|
if (null == brush) |
|
|
return null; |
|
|
|
|
|
switch (brush) |
|
|
{ |
|
|
case SolidColorBrush solidColorBrush: |
|
|
{ |
|
|
return GetBrushFromColor(solidColorBrush.Color); |
|
|
} |
|
|
case LinearGradientBrush linearGradientBrush: |
|
|
{ |
|
|
var result = (LinearGradientBrush)linearGradientBrush.Clone(); |
|
|
for (int i = 0; i < result.GradientStops.Count; ++i) |
|
|
result.GradientStops[i].Color = InvertColor(result.GradientStops[i].Color); |
|
|
return result; |
|
|
} |
|
|
default: |
|
|
return brush; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
Dictionary<Color, Brush> _cachedSolidBrushes = new Dictionary<Color, System.Windows.Media.Brush>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public System.Windows.Media.Brush GetBrushFromColor(Color color) |
|
|
{ |
|
|
if (!_cachedSolidBrushes.TryGetValue(color, out var brush)) |
|
|
{ |
|
|
brush = new System.Windows.Media.SolidColorBrush(color); |
|
|
brush.Freeze(); |
|
|
_cachedSolidBrushes.Add(color, brush); |
|
|
} |
|
|
return brush; |
|
|
} |
|
|
} |
|
|
} |
|
|
|