KeyColor is a .NET library that generates unique and consistent colors based on input keys. It's perfect for applications that need deterministic color generation, such as data visualization, user interfaces, and color-coded elements.
If you find this project helpful, please consider giving it a star! ⭐
- Deterministic Color Generation: Same input always produces the same color
- Text Contrast Guarantee: Default settings ensure text readability on generated colors
- Type Support: Generate colors from strings, byte arrays, or structs
- Customizable Parameters: Control saturation, lightness, and brightness ranges
- Framework Support:
- .NET 9.0
- .NET 8.0
- .NET 7.0
- .NET Standard 2.0
- High Performance: Optimized for both modern and legacy .NET frameworks
- Thread Safety: Static
ColorFrom
APIs are thread-safe,KeyColorGenerator
has thread-safe color generation

Try out the live demo to see KeyColor in action!
Install KeyColor via NuGet:
dotnet add package KeyColor
// Using string as input
var color1 = ColorFrom.String("uniqueKey");
string cssColor1 = color1.ToCssColor(); // Returns "#RRGGBB"
// Using struct as input
var myStruct = new MyStruct();
var color2 = ColorFrom.Key(myStruct);
// Using byte array as input
byte[] data = new byte[] { 1, 2, 3 };
var color3 = ColorFrom.Span(data);
// Generate random color
var randomColor = ColorFrom.Rng();
var generator = new KeyColorGenerator();
// Customize saturation range (0-1)
generator.Saturation.Min = 0.3;
generator.Saturation.Max = 0.7;
// Customize lightness range (0-1)
generator.Lightness.Min = 0.2;
generator.Lightness.Max = 0.8;
// Customize brightness range (0-255)
generator.Brightness.Min = 80;
generator.Brightness.Max = 200;
// Generate color with custom settings
var color = generator.GetUniqueColor("myKey");
The default configuration ensures that both black and white text will be readable on any generated color:
- Lightness Range: Constrained between 0.2 and 0.8 (not too dark, not too light)
- Brightness Range: Kept between 80-200 (moderate brightness)
- Perceived Brightness: Algorithm considers human eye perception of different colors
This means you can safely overlay text on generated colors without additional contrast checks for most use cases.
String(string key)
: Generate color from string inputKey<T>(T key) where T : struct
: Generate color from structSpan<T>(ReadOnlySpan<T> span) where T : struct
: Generate color from spanRng()
: Generate random color
All methods in ColorFrom
are thread-safe and can be safely called from multiple threads.
GetUniqueColor(string label)
: Generate color from stringGetUniqueColor<T>(T key) where T : struct
: Generate color from structGetUniqueColor(ReadOnlySpan<byte> key)
: Generate color from byte span
Thread Safety Note: The color generation methods are thread-safe, but modifying properties (Seed, Saturation, Lightness, Brightness) is not thread-safe. If you need to modify these properties, do it before making the generator available to multiple threads.
R
: Red component (0-255)G
: Green component (0-255)B
: Blue component (0-255)ToArray()
: Convert to byte arrayToCssColor()
: Convert to CSS color string
Here's a performance comparison of ColorFrom.Key<struct>
across different .NET versions:
Framework | Mean | Allocated |
---|---|---|
.NET 9.0 | 680.5 ns | 0 bytes |
.NET 8.0 | 688.6 ns | 0 bytes |
.NET 7.0 | 689.0 ns | 0 bytes |
.NET Core 3.1 | 1,475 ns | 1.2 KB |