Skip to content

Commit

Permalink
Add an RGB color parser (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 20, 2019
1 parent e057623 commit 1e12d2c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"TextStyle": null,
"RootAtom": {
"RowAtom": {
"PreviousAtom": null,
"Elements": [
{
"Character": "x",
"TextStyle": null,
"IsDefaultTextStyle": true,
"IsTextSymbol": false,
"Type": "Ordinary",
"Source": {
"Start": 29,
"End": 30,
"Length": 1,
"Source": "\\color [RGB] {128, 128, 128} x"
}
}
],
"Type": "Ordinary",
"Source": {
"Start": 1,
"End": 30,
"Length": 29,
"Source": "\\color [RGB] {128, 128, 128} x"
}
},
"Background": null,
"Foreground": "#FF808080",
"Type": "Ordinary",
"Source": {
"Start": 1,
"End": 30,
"Length": 29,
"Source": "\\color [RGB] {128, 128, 128} x"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"TextStyle": null,
"RootAtom": {
"RowAtom": {
"PreviousAtom": null,
"Elements": [
{
"Character": "x",
"TextStyle": null,
"IsDefaultTextStyle": true,
"IsTextSymbol": false,
"Type": "Ordinary",
"Source": {
"Start": 32,
"End": 33,
"Length": 1,
"Source": "\\colorbox [RGB] {128, 128, 128} x"
}
}
],
"Type": "Ordinary",
"Source": {
"Start": 1,
"End": 33,
"Length": 32,
"Source": "\\colorbox [RGB] {128, 128, 128} x"
}
},
"Background": "#FF808080",
"Foreground": null,
"Type": "Ordinary",
"Source": {
"Start": 1,
"End": 33,
"Length": 32,
"Source": "\\colorbox [RGB] {128, 128, 128} x"
}
}
}
27 changes: 27 additions & 0 deletions src/WpfMath/Colors/RgbColorParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Media;

namespace WpfMath.Colors
{
public class RgbColorParser : IColorParser
{
public Color? Parse(IEnumerable<string> components)
{
var rgbStrings = components.ToList();
if (rgbStrings.Count != 3)
return null;

var rgb = rgbStrings.Select(x =>
{
var success = byte.TryParse(x, NumberStyles.None, CultureInfo.InvariantCulture, out var val);
return success ? (byte?) val : null;
}).ToArray();
var r = rgb[0];
var g = rgb[1];
var b = rgb[2];
return r == null || g == null || b == null ? (Color?) null : Color.FromRgb(r.Value, g.Value, b.Value);
}
}
}
3 changes: 2 additions & 1 deletion src/WpfMath/Colors/StandardColorParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ internal static class StandardColorParsers
public static IReadOnlyDictionary<string, IColorParser> Dictionary = new Dictionary<string, IColorParser>
{
["gray"] = new GrayscaleColorParser(),
["HTML"] = new HtmlColorParser()
["HTML"] = new HtmlColorParser(),
["RGB"] = new RgbColorParser()
};
}
}

0 comments on commit 1e12d2c

Please sign in to comment.