Commit a9d1cd69 authored by Tebjan Halm's avatar Tebjan Halm
Browse files

Merge pull request #150 from mkb137/master

HSL support, fix for clip path (issues 142 and 143)
parents 0f51d060 9a1fb1c5
Showing with 128 additions and 32 deletions
+128 -32
......@@ -57,7 +57,7 @@ namespace Svg
[SvgAttribute("clip-path")]
public virtual Uri ClipPath
{
get { return this.Attributes.GetInheritedAttribute<Uri>("clip-path"); }
get { return this.Attributes.GetAttribute<Uri>("clip-path"); }
set { this.Attributes["clip-path"] = value; }
}
......
......@@ -32,9 +32,9 @@ namespace Svg
if (colour != null)
{
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
colour = colour.Trim();
if (colour.StartsWith("rgb"))
......@@ -43,32 +43,32 @@ namespace Svg
{
int start = colour.IndexOf("(") + 1;
//get the values from the RGB string
string[] values = colour.Substring(start, colour.IndexOf(")") - start).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
//determine the alpha value if this is an RGBA (it will be the 4th value if there is one)
int alphaValue = 255;
if (values.Length > 3)
{
//the alpha portion of the rgba is not an int 0-255 it is a decimal between 0 and 1
//so we have to determine the corosponding byte value
var alphastring = values[3];
if(alphastring.StartsWith("."))
{
alphastring = "0" + alphastring;
}
var alphaDecimal = decimal.Parse(alphastring);
if(alphaDecimal <= 1)
{
alphaValue = (int)(alphaDecimal * 255);
}
else
{
alphaValue = (int)alphaDecimal;
}
}
//get the values from the RGB string
string[] values = colour.Substring(start, colour.IndexOf(")") - start).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
//determine the alpha value if this is an RGBA (it will be the 4th value if there is one)
int alphaValue = 255;
if (values.Length > 3)
{
//the alpha portion of the rgba is not an int 0-255 it is a decimal between 0 and 1
//so we have to determine the corosponding byte value
var alphastring = values[3];
if(alphastring.StartsWith("."))
{
alphastring = "0" + alphastring;
}
var alphaDecimal = decimal.Parse(alphastring);
if(alphaDecimal <= 1)
{
alphaValue = (int)(alphaDecimal * 255);
}
else
{
alphaValue = (int)alphaDecimal;
}
}
Color colorpart;
if (values[0].Trim().EndsWith("%"))
......@@ -82,7 +82,36 @@ namespace Svg
colorpart = System.Drawing.Color.FromArgb(alphaValue, int.Parse(values[0]), int.Parse(values[1]), int.Parse(values[2]));
}
return colorpart;
return colorpart;
}
catch
{
throw new SvgException("Colour is in an invalid format: '" + colour + "'");
}
}
// HSL support
else if ( colour.StartsWith( "hsl" ) ) {
try
{
int start = colour.IndexOf("(") + 1;
//get the values from the RGB string
string[] values = colour.Substring(start, colour.IndexOf(")") - start).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
if ( values[1].EndsWith( "%" ) )
{
values[1] = values[1].TrimEnd( '%' );
}
if ( values[2].EndsWith( "%" ) )
{
values[2] = values[2].TrimEnd( '%' );
}
// Get the HSL values in a range from 0 to 1.
double h = double.Parse( values[0] ) / 360.0;
double s = double.Parse( values[1] ) / 100.0;
double l = double.Parse( values[2] ) / 100.0;
// Convert the HSL color to an RGB color
Color colorpart = Hsl2Rgb( h, s, l );
return colorpart;
}
catch
{
......@@ -126,7 +155,7 @@ namespace Svg
case "windowtext": return SystemColors.WindowText;
}
Thread.CurrentThread.CurrentCulture = oldCulture;
Thread.CurrentThread.CurrentCulture = oldCulture;
}
return base.ConvertFrom(context, culture, value);
......@@ -162,5 +191,72 @@ namespace Svg
return base.ConvertTo(context, culture, value, destinationType);
}
/// <summary>
/// Converts HSL color (with HSL specified from 0 to 1) to RGB color.
/// Taken from http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm
/// </summary>
/// <param name="h"></param>
/// <param name="sl"></param>
/// <param name="l"></param>
/// <returns></returns>
private static Color Hsl2Rgb( double h, double sl, double l ) {
double r = l; // default to gray
double g = l;
double b = l;
double v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
if (v > 0)
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;
m = l + l - v;
sv = (v - m ) / v;
h *= 6.0;
sextant = (int)h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant)
{
case 0:
r = v;
g = mid1;
b = m;
break;
case 1:
r = mid2;
g = v;
b = m;
break;
case 2:
r = m;
g = v;
b = mid1;
break;
case 3:
r = m;
g = mid2;
b = v;
break;
case 4:
r = mid1;
g = m;
b = v;
break;
case 5:
r = v;
g = m;
b = mid2;
break;
}
}
Color rgb = Color.FromArgb( (int)( r * 255.0 ), (int)( g * 255.0 ), (int)( b * 255.0f ) );
return rgb;
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment