Skip to content

Commit 60b4a59

Browse files
committed
Added color serialization #96
1 parent 91a7444 commit 60b4a59

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Released on Wednesday, January 5 2022.
44

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

78
# 0.16.2
89

docs/tutorials/03-Questions.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ section: "AngleSharp.Css"
44
---
55
# Frequently Asked Questions
66

7-
## What to ask?
7+
## How to change the color output?
88

9-
(tbd)
9+
By default, AngleSharp.Css uses `rgba()` for the serialization of `Color`. To change this you can set
10+
11+
```cs
12+
Color.UseHex = true;
13+
```
14+
15+
which will automatically use hex for all non-transparent colors. All other colors would still be represented via the `rgba()` function.
16+
17+
So you'd get:
18+
19+
```cs
20+
Color.UseHex = true;
21+
var color1 = new Color(65, 12, 48);
22+
// color1.CssText = #410C30
23+
var color2 = new Color(65, 12, 48, 10);
24+
// color2.CssText = rgba(65, 12, 48, 0.04)
25+
```

src/AngleSharp.Css.Tests/Library/StringRepresentation.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AngleSharp.Css.Tests.Library
22
{
33
using AngleSharp.Css.Parser;
4+
using AngleSharp.Css.Values;
45
using NUnit.Framework;
56
using System.IO;
67

@@ -20,5 +21,25 @@ public void PrettyStyleFormatterStringifyShouldWork_Issue41()
2021
Assert.AreEqual("@media (min-width: 800px) { \n\t.ad_column {\n\t\twidth: 728px;\n\t\theight: 90px;\n\t}\n}", stringWriter.ToString());
2122
}
2223
}
24+
25+
[Test]
26+
public void SimpleColorWorksWithHexOutput_Issue96()
27+
{
28+
var color = new Color(65, 12, 48);
29+
Color.UseHex = true;
30+
var text = color.CssText;
31+
Color.UseHex = false;
32+
Assert.AreEqual("#410C30", text);
33+
}
34+
35+
[Test]
36+
public void TransparentColorDoesNotWorkWithHexOutput_Issue96()
37+
{
38+
var color = new Color(65, 12, 48, 10);
39+
Color.UseHex = true;
40+
var text = color.CssText;
41+
Color.UseHex = false;
42+
Assert.AreEqual("rgba(65, 12, 48, 0.04)", text);
43+
}
2344
}
2445
}

src/AngleSharp.Css/Values/Primitives/Color.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ public static Color FromHwba(Double h, Double w, Double b, Double alpha)
373373

374374
#region Properties
375375

376+
/// <summary>
377+
/// Gets or sets if hex codes should be used for serialization.
378+
/// This will not be applied in case of transparent colors, i.e.,
379+
/// when alpha is not 1.
380+
/// </summary>
381+
public static Boolean UseHex { get; set; }
382+
376383
/// <summary>
377384
/// Gets the CSS text representation.
378385
/// </summary>
@@ -388,6 +395,10 @@ public String CssText
388395
{
389396
return CssKeywords.Invert;
390397
}
398+
else if (_alpha == 255 && UseHex)
399+
{
400+
return $"#{_red.ToString("X2", CultureInfo.InvariantCulture)}{_green.ToString("X2", CultureInfo.InvariantCulture)}{_blue.ToString("X2", CultureInfo.InvariantCulture)}";
401+
}
391402
else
392403
{
393404
var fn = FunctionNames.Rgba;

0 commit comments

Comments
 (0)