Skip to content

Commit e818a49

Browse files
authored
Merge pull request #156 from lee-m/colour-parsing-fixes
Couple of colour parsing fixes/tweaks
2 parents a1c71d8 + 350918f commit e818a49

3 files changed

Lines changed: 13 additions & 48 deletions

File tree

src/ExCSS.Tests/PropertyTests/BackgroundProperty.cs

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -144,60 +144,25 @@ public void BackgroundColorTealLegal()
144144
Assert.Equal("rgb(0, 128, 128)", concrete.Value);
145145
}
146146

147-
[Fact]
148-
public void BackgroundColorRgbLegal()
149-
{
150-
var snippet = "background-color : rgb(255 , 255 , 128)";
151-
var property = ParseDeclaration(snippet);
152-
Assert.Equal("background-color", property.Name);
153-
Assert.False(property.IsImportant);
154-
Assert.IsType<BackgroundColorProperty>(property);
155-
var concrete = (BackgroundColorProperty)property;
156-
Assert.False(concrete.IsInherited);
157-
Assert.True(concrete.HasValue);
158-
Assert.Equal("rgb(255, 255, 128)", concrete.Value);
159-
}
160-
161-
[Fact]
162-
public void BackgroundColorHslaLegal()
163-
{
164-
var snippet = "background-color : hsla(50, 33%, 25%, 0.75)";//equal to rgba(85, 78, 43, 0.75)
165-
var property = ParseDeclaration(snippet);
166-
Assert.Equal("background-color", property.Name);
167-
Assert.False(property.IsImportant);
168-
Assert.IsType<BackgroundColorProperty>(property);
169-
var concrete = (BackgroundColorProperty)property;
170-
Assert.False(concrete.IsInherited);
171-
Assert.True(concrete.HasValue);
172-
Assert.Equal("hsla(50deg, 33%, 25%, 0.75)", concrete.Value);
173-
}
174-
175-
[Fact]
176-
public void BackgroundColorTransparentLegal()
177-
{
178-
var snippet = "background-color : Transparent";
179-
var property = ParseDeclaration(snippet);
180-
Assert.Equal("background-color", property.Name);
181-
Assert.False(property.IsImportant);
182-
Assert.IsType<BackgroundColorProperty>(property);
183-
var concrete = (BackgroundColorProperty)property;
184-
Assert.False(concrete.IsInherited);
185-
Assert.True(concrete.HasValue);
186-
Assert.Equal("rgba(0, 0, 0, 0)", concrete.Value);
187-
}
188-
189-
[Fact]
190-
public void BackgroundColorHexLegal()
147+
[Theory]
148+
[InlineData("background-color: rgb(255, 255, 128)", "rgb(255, 255, 128)")]
149+
[InlineData("background-color: hsla(50, 33%, 25%, 0.75)", "hsla(50deg, 33%, 25%, 0.75)")]
150+
[InlineData("background-color : rgb(255 , 255 , 128)", "rgb(255, 255, 128)")]
151+
[InlineData("background-color: Transparent", "rgba(0, 0, 0, 0)")]
152+
[InlineData("background-color: #F09", "rgb(255, 0, 153)")]
153+
[InlineData("background-color: #F09F", "rgb(255, 0, 153)")]
154+
[InlineData("background-color: #AABBCC", "rgb(170, 187, 204)")]
155+
[InlineData("background-color: #AABBCC11", "rgba(170, 187, 204, 0.07)")]
156+
public void BackgroundColorRgbLegal(string snippet, string expectedValue)
191157
{
192-
var snippet = "background-color : #bbff00";
193158
var property = ParseDeclaration(snippet);
194159
Assert.Equal("background-color", property.Name);
195160
Assert.False(property.IsImportant);
196161
Assert.IsType<BackgroundColorProperty>(property);
197162
var concrete = (BackgroundColorProperty)property;
198163
Assert.False(concrete.IsInherited);
199164
Assert.True(concrete.HasValue);
200-
Assert.Equal("rgb(187, 255, 0)", concrete.Value);
165+
Assert.Equal(expectedValue, concrete.Value);
201166
}
202167

203168
[Fact]

src/ExCSS/Tokens/ColorToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public ColorToken(string data, TextPosition position)
77
{
88
}
99

10-
public bool IsValid => Data.Length != 3 && Data.Length != 6;
10+
public bool IsValid => Data.Length != 3 && Data.Length != 4 && Data.Length != 6 && Data.Length != 8;
1111

1212
public override string ToValue()
1313
{

src/ExCSS/Values/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public static Color FromHwba(float hue, float whiteness, float blackness, float
270270

271271
public int Value => _hashcode;
272272
public byte A => _alpha;
273-
public double Alpha => _alpha / 255.0;
273+
public double Alpha => Math.Round(_alpha / 255.0, 2);
274274
public byte R => _red;
275275
public byte G => _green;
276276
public byte B => _blue;

0 commit comments

Comments
 (0)