-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize PredefinedColorParser as an IColorParser instance (#165)
- Loading branch information
Showing
8 changed files
with
129 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module WpfMath.Tests.PredefinedColorParserTests | ||
|
||
open System.Windows.Media | ||
|
||
open Xunit | ||
|
||
open WpfMath.Colors | ||
|
||
let parser = PredefinedColorParser.Instance | ||
|
||
[<Fact>] | ||
let ``PredefinedColorParser parses a correctly defined color``(): unit = | ||
Assert.Equal(Color.FromRgb(237uy, 27uy, 35uy), parser.Parse([| "red" |]).Value) | ||
|
||
[<Fact>] | ||
let ``PredefinedColorParser returns null for wrong input``(): unit = | ||
Assert.Null(parser.Parse([| "nonexistent-color" |])) | ||
|
||
[<Fact>] | ||
let ``PredefinedColorParser returns null for empty input``(): unit = | ||
Assert.Null(parser.Parse(Array.empty)) | ||
|
||
[<Fact>] | ||
let ``PredefinedColorParser returns null for too long input``(): unit = | ||
Assert.Null(parser.Parse([| "red"; "green" |])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Windows.Media; | ||
|
||
namespace WpfMath.Colors | ||
{ | ||
/// <summary>A parser for colors.</summary> | ||
public interface IColorParser | ||
{ | ||
/// <summary>Parses the color components.</summary> | ||
/// <param name="components">A sequence of the components that were separated by comma.</param> | ||
/// <returns>Either a parsed color or <c>null</c> if it cannot be parsed.</returns> | ||
Color? Parse(IEnumerable<string> components); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Windows.Media; | ||
using System.Xml.Linq; | ||
|
||
namespace WpfMath.Colors | ||
{ | ||
internal class PredefinedColorParser : IColorParser | ||
{ | ||
private const string ResourceName = TexUtilities.ResourcesDataDirectory + "PredefinedColors.xml"; | ||
public static readonly PredefinedColorParser Instance = new PredefinedColorParser(ResourceName); | ||
|
||
private readonly IReadOnlyDictionary<string, Color> _colors; | ||
|
||
private PredefinedColorParser(string resourceName) | ||
{ | ||
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) | ||
{ | ||
var doc = XDocument.Load(resource); | ||
_colors = Parse(doc.Root); | ||
} | ||
} | ||
|
||
public Color? Parse(IEnumerable<string> components) | ||
{ | ||
// Return a color iff components contain only one element, and it is contained in the _colors dictionary. | ||
var firstItem = true; | ||
Color? color = null; | ||
foreach (var component in components) | ||
{ | ||
if (!firstItem) | ||
return null; | ||
|
||
var colorName = component; | ||
if (_colors.TryGetValue(colorName, out var parsedColor)) | ||
color = parsedColor; | ||
|
||
firstItem = false; | ||
} | ||
|
||
return color; | ||
} | ||
|
||
private Dictionary<string, Color> Parse(XElement rootElement) | ||
{ | ||
var colors = new Dictionary<string, Color>(); | ||
foreach (var colorElement in rootElement.Elements("color")) | ||
{ | ||
var name = colorElement.AttributeValue("name"); | ||
var r = colorElement.AttributeValue("r"); | ||
var g = colorElement.AttributeValue("g"); | ||
var b = colorElement.AttributeValue("b"); | ||
colors.Add(name, Color.FromRgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b))); | ||
} | ||
|
||
return colors; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters