Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Added color conversion methods overloads to CssColor without ref. Add…
Browse files Browse the repository at this point in the history
…ed CssValue.CalculatedColor property.
  • Loading branch information
Athari committed May 24, 2014
1 parent c3e323d commit bae6caa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .nuget/Upload.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NuGet Push Alba.CsCss.1.0.0.3.nupkg
NuGet Push Alba.CsCss.1.0.1.0.nupkg
Pause
25 changes: 24 additions & 1 deletion Alba.CsCss.Tests/Style/CssLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CssLoaderTests
private static readonly Uri SheetUri = new Uri("http://example.com/sheet.css"), BaseUri = new Uri("http://example.com/");

[TestMethod]
public void ParseSheet_Simple ()
public void ParseSheet_Simple_ColorHex ()
{
var loader = new CssLoader();
var css = loader.ParseSheet("h1 { color: #123; }", SheetUri, BaseUri);
Expand All @@ -35,6 +35,29 @@ public void ParseSheet_Simple ()
Assert.AreEqual(CssColor.RGB(0x11, 0x22, 0x33), css.StyleRules.Single().Declaration.Color.Color);
}

[TestMethod]
public void ParseSheet_Simple_ColorName ()
{
var loader = new CssLoader();
var css = loader.ParseSheet("h1 { color: red; }", SheetUri, BaseUri);

Assert.AreEqual(SheetUri, css.SheetUri);
Assert.AreEqual(BaseUri, css.BaseUri);
Assert.AreEqual(1, css.Rules.Count());
Assert.AreEqual(1, css.AllRules.Count());
Assert.AreEqual(1, css.StyleRules.Count());
Assert.AreEqual(1, css.AllStyleRules.Count());
var h1 = css.StyleRules.Single();
var h1sel = h1.SelectorGroups.Single().Selectors.Single();
Assert.AreEqual("h1", h1sel.Tag);
var h1color = h1.Declaration.Data.Single();
Assert.AreEqual(CssProperty.Color, h1color.Property);
Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), CssColor.ColorNameToRGB(h1color.Value.String));
Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), CssColor.ColorNameToRGB(css.StyleRules.Single().Declaration.Color.String));
Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), h1color.Value.CalculatedColor);
Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), css.StyleRules.Single().Declaration.Color.CalculatedColor);
}

[TestMethod]
public void ParseSheet_ParseErrorEvent ()
{
Expand Down
20 changes: 19 additions & 1 deletion Alba.CsCss/Gfx/CssColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public static bool HexToRGB (string aColorSpec, ref CssColor aResult)
return false;
}

public static CssColor? HexToRGB (string aColorSpec)
{
var aResult = new CssColor();
return HexToRGB(aColorSpec, ref aResult) ? aResult : (CssColor?)null;
}

public static bool LooseHexToRGB (string aColorSpec, ref CssColor aResult)
{
if (aColorSpec.EqualsLiteral("transparent")) {
Expand Down Expand Up @@ -146,6 +152,12 @@ public static bool LooseHexToRGB (string aColorSpec, ref CssColor aResult)
return true;
}

public static CssColor? LooseHexToRGB (string aColorSpec)
{
var aResult = new CssColor();
return LooseHexToRGB(aColorSpec, ref aResult) ? aResult : (CssColor?)null;
}

public static bool ColorNameToRGB (string aColorName, ref CssColor aResult)
{
if (gColorTable == null)
Expand All @@ -159,6 +171,12 @@ public static bool ColorNameToRGB (string aColorName, ref CssColor aResult)
return false;
}

public static CssColor? ColorNameToRGB (string aColorName)
{
var aResult = new CssColor();
return ColorNameToRGB(aColorName, ref aResult) ? aResult : (CssColor?)null;
}

public static CssColor HSL2RGB (float h, float s, float l)
{
float m2 = l <= 0.5f ? (l * (s + 1)) : (l + s - l * s);
Expand Down Expand Up @@ -207,7 +225,7 @@ private static byte ComponentValue (string aColorSpec, int aLen, int color, int
component = (component * 16) + (ch - '0');
}
else if ((('a' <= ch) && (ch <= 'f')) ||
(('A' <= ch) && (ch <= 'F'))) {
(('A' <= ch) && (ch <= 'F'))) {
// "ch&7" handles lower and uppercase hex alphabetics
component = (component * 16) + (ch & 7) + 9;
}
Expand Down
4 changes: 2 additions & 2 deletions Alba.CsCss/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.3")]
[assembly: AssemblyFileVersion("1.0.0.3")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]

[assembly: InternalsVisibleTo("Alba.CsCss.Tests")]
19 changes: 19 additions & 0 deletions Alba.CsCss/Style/Values/CssValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,25 @@ public CssColor Color
get { return GetColorValue(); }
}

public CssColor CalculatedColor
{
get
{
switch (GetUnit()) {
case CssUnit.Color:
return Color;
case CssUnit.Ident:
var result = new CssColor();
if (CssColor.ColorNameToRGB(String, ref result))
return result;
else
throw new InvalidOperationException(string.Format("Unknown color name: {0}", String));
default:
throw new InvalidOperationException(string.Format("Cannot convert {0} value to color.", GetUnit()));
}
}
}

public CssValueGradient Gradient
{
get { return GetGradientValue(); }
Expand Down

0 comments on commit bae6caa

Please sign in to comment.