KeyColor 1.0.1

dotnet add package KeyColor --version 1.0.1
                    
NuGet\Install-Package KeyColor -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KeyColor" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KeyColor" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="KeyColor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KeyColor --version 1.0.1
                    
#r "nuget: KeyColor, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=KeyColor&version=1.0.1
                    
Install KeyColor as a Cake Addin
#tool nuget:?package=KeyColor&version=1.0.1
                    
Install KeyColor as a Cake Tool

KeyColor

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! ⭐

Features

  • Deterministic Color Generation: Same input always produces the same color
  • 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

Demo

<a href="https://brave-bush-06f230610.6.azurestaticapps.net/" target="_blank"> <img src="ReadmeAsssets/Screenshot.png" alt="KeyColor Demo Screenshot"> </a>

Try out the <a href="https://brave-bush-06f230610.6.azurestaticapps.net/" target="_blank">live demo</a> to see KeyColor in action!

Installation

Install KeyColor via NuGet:

dotnet add package KeyColor

Quick Start

// 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();

Advanced Usage

Customizing Color Generation

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");

API Reference

ColorFrom (Static Class)

  • String(string key): Generate color from string input
  • Key<T>(T key) where T : struct: Generate color from struct
  • Span<T>(ReadOnlySpan<T> span) where T : struct: Generate color from span
  • Rng(): Generate random color

All methods in ColorFrom are thread-safe and can be safely called from multiple threads.

KeyColorGenerator (Class)

  • GetUniqueColor(string label): Generate color from string
  • GetUniqueColor<T>(T key) where T : struct: Generate color from struct
  • GetUniqueColor(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.

GeneratedColor (Struct)

  • R: Red component (0-255)
  • G: Green component (0-255)
  • B: Blue component (0-255)
  • ToArray(): Convert to byte array
  • ToCssColor(): Convert to CSS color string

Performance

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
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 130 5/21/2025