Skip to content

Commit

Permalink
Added color serialization AngleSharp#96
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jan 5, 2022
1 parent 91a7444 commit 60b4a59
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Released on Wednesday, January 5 2022.

- Fixed issue with `text-shadow` missing the color part (#97)
- Added `Color.UseHex` to change color output format (#96)

# 0.16.2

Expand Down
20 changes: 18 additions & 2 deletions docs/tutorials/03-Questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ section: "AngleSharp.Css"
---
# Frequently Asked Questions

## What to ask?
## How to change the color output?

(tbd)
By default, AngleSharp.Css uses `rgba()` for the serialization of `Color`. To change this you can set

```cs
Color.UseHex = true;
```

which will automatically use hex for all non-transparent colors. All other colors would still be represented via the `rgba()` function.

So you'd get:

```cs
Color.UseHex = true;
var color1 = new Color(65, 12, 48);
// color1.CssText = #410C30
var color2 = new Color(65, 12, 48, 10);
// color2.CssText = rgba(65, 12, 48, 0.04)
```
21 changes: 21 additions & 0 deletions src/AngleSharp.Css.Tests/Library/StringRepresentation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AngleSharp.Css.Tests.Library
{
using AngleSharp.Css.Parser;
using AngleSharp.Css.Values;
using NUnit.Framework;
using System.IO;

Expand All @@ -20,5 +21,25 @@ public void PrettyStyleFormatterStringifyShouldWork_Issue41()
Assert.AreEqual("@media (min-width: 800px) { \n\t.ad_column {\n\t\twidth: 728px;\n\t\theight: 90px;\n\t}\n}", stringWriter.ToString());
}
}

[Test]
public void SimpleColorWorksWithHexOutput_Issue96()
{
var color = new Color(65, 12, 48);
Color.UseHex = true;
var text = color.CssText;
Color.UseHex = false;
Assert.AreEqual("#410C30", text);
}

[Test]
public void TransparentColorDoesNotWorkWithHexOutput_Issue96()
{
var color = new Color(65, 12, 48, 10);
Color.UseHex = true;
var text = color.CssText;
Color.UseHex = false;
Assert.AreEqual("rgba(65, 12, 48, 0.04)", text);
}
}
}
11 changes: 11 additions & 0 deletions src/AngleSharp.Css/Values/Primitives/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ public static Color FromHwba(Double h, Double w, Double b, Double alpha)

#region Properties

/// <summary>
/// Gets or sets if hex codes should be used for serialization.
/// This will not be applied in case of transparent colors, i.e.,
/// when alpha is not 1.
/// </summary>
public static Boolean UseHex { get; set; }

/// <summary>
/// Gets the CSS text representation.
/// </summary>
Expand All @@ -388,6 +395,10 @@ public String CssText
{
return CssKeywords.Invert;
}
else if (_alpha == 255 && UseHex)
{
return $"#{_red.ToString("X2", CultureInfo.InvariantCulture)}{_green.ToString("X2", CultureInfo.InvariantCulture)}{_blue.ToString("X2", CultureInfo.InvariantCulture)}";
}
else
{
var fn = FunctionNames.Rgba;
Expand Down

0 comments on commit 60b4a59

Please sign in to comment.