diff --git a/commonItems.UnitTests/Colors/ColorTests.cs b/commonItems.UnitTests/Colors/ColorTests.cs
index 88d0564a..5516d080 100644
--- a/commonItems.UnitTests/Colors/ColorTests.cs
+++ b/commonItems.UnitTests/Colors/ColorTests.cs
@@ -1,4 +1,5 @@
using commonItems.Colors;
+using Fernandezja.ColorHashSharp;
using System;
using System.Collections.Generic;
using Xunit;
@@ -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);
@@ -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) {
diff --git a/commonItems/Colors/ColorFactory.cs b/commonItems/Colors/ColorFactory.cs
index 512f79a1..f9cf2c77 100644
--- a/commonItems/Colors/ColorFactory.cs
+++ b/commonItems/Colors/ColorFactory.cs
@@ -1,4 +1,5 @@
-using System;
+using Fernandezja.ColorHashSharp;
+using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
@@ -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) {
diff --git a/commonItems/commonItems.csproj b/commonItems/commonItems.csproj
index 5ca2e297..ac27416d 100644
--- a/commonItems/commonItems.csproj
+++ b/commonItems/commonItems.csproj
@@ -6,7 +6,7 @@
False
PGCG.$(AssemblyName)
- 11.0.0
+ 11.1.0
PGCG
https://github.com/ParadoxGameConverters/commonItems.NET
https://github.com/ParadoxGameConverters/commonItems.NET
@@ -27,6 +27,7 @@
+