Skip to content
Permalink
Browse files

Implements hslcolor parser.

  • Loading branch information...
mntone committed Mar 20, 2018
1 parent c65e19c commit debf84fe03254001e2bbbf113225b1dfc9cd9d99
@@ -117,6 +117,35 @@ internal static SvgAngle Parse(string angleText)
return new SvgAngle(SvgAngleType.Degree, value);
}

internal static SvgAngle Parse(StringPtr ptr)
{
var begin = ptr.Index;
ptr.AdvanceNumber();
if (begin == ptr.Index) throw new ArgumentException(nameof(ptr));

var value = float.Parse(ptr.Target.Substring(begin, ptr.Index - begin), System.Globalization.CultureInfo.InvariantCulture);
if (ptr.Index != ptr.Target.Length)
{
var unit = ptr.String;
if (unit.StartsWith("deg"))
{
ptr += 3;
return new SvgAngle(SvgAngleType.Degree, value);
}
if (unit.StartsWith("rad"))
{
ptr += 3;
return new SvgAngle(SvgAngleType.Radian, value);
}
if (unit.StartsWith("grad"))
{
ptr += 4;
return new SvgAngle(SvgAngleType.Grade, value);
}
}
return new SvgAngle(SvgAngleType.Degree, value);
}

public bool Equals(SvgAngle other) => this.UnitType == other.UnitType && this.Value == other.Value;

public static implicit operator SvgAngle(float f) => new SvgAngle(SvgAngleType.Degree, f);
@@ -179,6 +179,8 @@ protected static RgbColor Parse(string color)
{
if (color[0] == '#') return ParseColorCode(color.Substring(1));
if (color.StartsWith("rgb(")) return ParseColorFunc(color.Substring(4));
if (color.StartsWith("hsl(")) return ParseHslColorFunc(color.Substring(4));
if (color.StartsWith("hsla(")) return ParseHslColorFunc(color.Substring(5));
if (_colors.ContainsKey(color)) return _colors[color];
throw new ArgumentException(nameof(color));
}
@@ -263,14 +265,77 @@ private static RgbColor ParseColorFunc(string color)
: new RgbColor(r, g, b);
}

private static RgbColor ParseHslColorFunc(string color)
{
var ptr = new StringPtr(color);
ptr.AdvanceWhiteSpace();

var s1 = ptr.Index;
var h = SvgAngle.Parse(ptr);
if (ptr.Index == s1) throw new ArgumentException(nameof(color));

ptr.AdvanceWhiteSpace();
if (ptr.Char != ',') throw new ArgumentException(nameof(color));
++ptr;
ptr.AdvanceWhiteSpace();

var s2 = ptr.Index;
ptr.AdvanceInteger();
if (ptr.Index == s2) throw new ArgumentException(nameof(color));
var s = byte.Parse(color.Substring(s2, ptr.Index - s2));
if (ptr.Char != '%') throw new ArgumentException(nameof(color));
++ptr;

ptr.AdvanceWhiteSpace();
if (ptr.Char != ',') throw new ArgumentException(nameof(color));
++ptr;
ptr.AdvanceWhiteSpace();

var s3 = ptr.Index;
ptr.AdvanceInteger();
if (ptr.Index == s3) throw new ArgumentException(nameof(color));
var l = byte.Parse(color.Substring(s3, ptr.Index - s3));
if (ptr.Char != '%') throw new ArgumentException(nameof(color));
++ptr;

ptr.AdvanceWhiteSpace();

if (ptr.Char != ')') throw new ArgumentException(nameof(color));

return HlsToRgb(h.ValueAsDegree, s / 100.0, l / 100.0);
}

private static RgbColor HlsToRgb(double hue, double sat, double light)
{
var t2 = light <= .5
? light * (sat + 1.0)
: light + sat - (light * sat);
var t1 = 2.0 * light - t2;
var r = HueToValue(t1, t2, hue + 120.0);
var g = HueToValue(t1, t2, hue);
var b = HueToValue(t1, t2, hue - 120.0);
return new RgbColor(r, g, b);
}

private static byte HueToValue(double t1, double t2, double hue)
{
while (hue < 0) hue += 360;
hue %= 360;

if (hue < 60) return (byte)(255.0 * ((t2 - t1) * hue / 60.0 + t1));
if (hue < 180) return (byte)(255.0 * t2);
if (hue < 240) return (byte)(255.0 * ((t2 - t1) * (240 - hue) / 60.0 + t1));
return (byte)(255.0 * t1);
}

private static RgbColor ParseOther(string color)
{
throw new NotImplementedException();
}

internal Windows.UI.Color ToPlatformColor(byte alpha)
{
return Windows.UI.Color.FromArgb(alpha, this.RgbColor.Red, this.RgbColor.Green, this.RgbColor.Blue);
internal Windows.UI.Color ToPlatformColor(byte alpha)
{
return Windows.UI.Color.FromArgb(alpha, this.RgbColor.Red, this.RgbColor.Green, this.RgbColor.Blue);
}
}
}

0 comments on commit debf84f

Please sign in to comment.
You can’t perform that action at this time.