Skip to content

Commit

Permalink
Return color hash as fallback for missing named colors, instead of re…
Browse files Browse the repository at this point in the history
…turning black (#399)

* Return color hash as fallback for missing named colors

* Update ColorTests.cs

* Bump version

* Tweak test name
  • Loading branch information
IhateTrains authored Jan 25, 2024
1 parent 2b33f7f commit ca66cfd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
23 changes: 21 additions & 2 deletions commonItems.UnitTests/Colors/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using commonItems.Colors;
using Fernandezja.ColorHashSharp;
using System;
using System.Collections.Generic;
using Xunit;
Expand Down Expand Up @@ -528,8 +529,7 @@ public void ColorCanBeInitializedWithName() {
[InlineData("grey", "gray")] // the classic
[InlineData("red_green_blue", "red")] // first matching word is used
[InlineData("snow_white", "snow")]
[InlineData("random_bullshit", "black")] // black is the final fallback
public void ColorCanBeReturnedEvenForUncachedName(string colorName, string expectedReturnedColorName) {
public void ColorCanBeReturnedEvenForUncachedNameIfNameContainsKnownBuiltinColor(string colorName, string expectedReturnedColorName) {
var colorFactory = new ColorFactory();

var color = colorFactory.GetColorByName(colorName);
Expand All @@ -539,6 +539,25 @@ public void ColorCanBeReturnedEvenForUncachedName(string colorName, string expec
Assert.Equal(expectedColor.G, color.G);
Assert.Equal(expectedColor.B, color.B);
}

[Fact]
public void ColorHashIsUsedWhenNoMatchingColorIsFound() {
var colorFactory = new ColorFactory();
var color = colorFactory.GetColorByName("random_bullshit");

var colorHash = new ColorHash().Rgb("random_bullshit");
Assert.Equal(colorHash.R, color.R);
Assert.Equal(colorHash.G, color.G);
Assert.Equal(colorHash.B, color.B);

// Check if the hash is the same for the same name.
var color2 = colorFactory.GetColorByName("random_bullshit");
Assert.Equal(color, color2);

// Check if the hash is different for different names.
var color3 = colorFactory.GetColorByName("random_bullshit2");
Assert.NotEqual(color, color3);
}

private class Foo : Parser {
public Foo(BufferedReader reader) {
Expand Down
12 changes: 8 additions & 4 deletions commonItems/Colors/ColorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Fernandezja.ColorHashSharp;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -169,9 +170,12 @@ public Color GetColorByName(string colorName) {
return color;
}

// If all else fails, return black.
Logger.Warn($"No matching fallback color found for {colorName}, using black");
return new Color(System.Drawing.Color.Black);
// If all else fails, instead of always returning the same color, use a color hash.
// This will at least give us a different color for each name.
Color fallbackColor = new(new ColorHash().Rgb(colorName));
Logger.Warn($"No matching fallback color found for {colorName}, " +
$"using color hash {fallbackColor.OutputRgb()}.");
return fallbackColor;
}

public void AddNamedColor(string name, Color color) {
Expand Down
3 changes: 2 additions & 1 deletion commonItems/commonItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageId>PGCG.$(AssemblyName)</PackageId>
<Version>11.0.0</Version>
<Version>11.1.0</Version>
<Authors>PGCG</Authors>
<PackageProjectUrl>https://github.com/ParadoxGameConverters/commonItems.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/ParadoxGameConverters/commonItems.NET</RepositoryUrl>
Expand All @@ -27,6 +27,7 @@

<ItemGroup>
<PackageReference Include="AnyAscii" Version="0.3.2" />
<PackageReference Include="ColorHashSharp" Version="1.0.0" />
<PackageReference Include="GameFinder.StoreHandlers.GOG" Version="4.1.0" />
<PackageReference Include="GameFinder.StoreHandlers.Steam" Version="4.1.0" />
<PackageReference Include="IcgSoftware.IntToOrdinalNumber" Version="1.0.0" />
Expand Down

0 comments on commit ca66cfd

Please sign in to comment.