ColorHashSharp
Generate color based on the given string. C# port of ColorHash Javascript Library.

Status
|Actions |main |develop |
|--- |--- |--- |
|CI | | |
Installation
dotnet add package ColorHashSharp
Basic usage
using System.Diagnostics;
using Fernandezja.ColorHashSharp;
Debug.WriteLine($"ColorHash > Hello World");
var colorHash = new ColorHash();
//HSL (return HSL object)
var hsl = colorHash.Hsl("Hello World");
Debug.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}");
// H = 233 | S = 0.5 | L = 0.65
//RGB (return System.Drawing.Color object)
var color = colorHash.Rgb("Hello World");
Debug.WriteLine($" R= {color.R} | G = {color.G} | B = {color.B}");
// R = 121 | G = 132 | B = 210
//Hex (return string)
var hex = colorHash.Hex("Hello World");
Debug.WriteLine($" hex = {hex}");
// hex = #7984d2
Hash selection and migration to 2.0
The default hash uses the first four bytes of SHA-256 over UTF-8 as an unsigned
big-endian integer, matching the hash used by JavaScript color-hash 2.0.2.
Options.UseJavaScriptCompatibility defaults to true and matches
JavaScript's HSL calculation, RGB rounding, and #rrggbb format. Set it to
false to use the port's historical color calculation and RRGGBB format.
var javaScriptBkdr = new ColorHash(new Options { HashFunction = new JavaScriptBkdrHash() });
var historical = new ColorHash(new Options
{
UseJavaScriptCompatibility = false,
HashFunction = new BKDRHash()
});
JavaScriptBkdrHash matches the JavaScript hash: 'bkdr' hash. Custom hashes
still use IHashFunction. In compatibility mode, their unsigned results
are converted to JavaScript-style double-precision numbers for color selection.
Use the historical configuration above with the same palettes and hue ranges
to reproduce historical ColorHashSharp colors. Applications upgrading from
NuGet 1.1.0 must recompile and update
Options.S, Options.L, and protected GetLS usage from ArrayList to
List<double>.
Options and null inputs in 2.0
ColorHash copies the saturation and lightness palettes and the hue bounds
at construction. To apply changes to Options, create a new ColorHash
instance. A custom hash instance is used as supplied.
S and L require at least one finite value between 0 and 1, inclusive.
An empty HueRanges list uses the default hue calculation. Each range
requires 0 <= Min <= Max <= 360. Invalid settings raise ArgumentException
at construction. A null input string is treated as empty, including when
passed to a custom hash.
Publishing to NuGet.org
Publish to NuGet builds and tests each release before packing it. The package version comes from the tag without its leading v. Use a stable tag such as v2.0.0 or a prerelease tag such as v2.0.0-rc.1. Omit build metadata (+...) because NuGet.org normalizes it out of version identifiers.
In GitHub, open Settings β Environments β nuget. Under Environment variables, set NUGET_USER to the NuGet.org profile name fernandezja. Under Deployment branches and tags, select Selected branches and tags and add a Tag rule for v*.*.*.
On NuGet.org β Trusted Publishing, add a GitHub Actions policy named ColorHashSharp GitHub Actions for the package owner. Set Repository Owner to fernandezja, Repository to ColorHashSharp, Workflow File to publish-nuget.yml, and Environment to nuget. Select Push only new package versions and enter ColorHashSharp under Glob Patterns and Packages. The workflow exchanges its OIDC token for a short-lived API key when the package is ready to publish.
After the workflow is merged, tag the release commit from main with an unused package version and push the tag. For example:
git tag -a v2.0.0 -m "ColorHashSharp 2.0.0"
git push origin v2.0.0
Check Actions β Publish to NuGet for the result, then verify the version on the ColorHashSharp package page.
Feedback and contributions
Report issues and suggest improvements on GitHub. Contributions are welcome through pull requests.