Generate color based on the given string. C# port of ColorHash Javascript Library.
| Actions | main | develop |
|---|---|---|
| CI |
dotnet add package ColorHashSharpusing 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 = #7984d2The 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>.
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.
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_USERto the NuGet.org profile namefernandezja. Under Deployment branches and tags, select Selected branches and tags and add a Tag rule forv*.*.*. -
On NuGet.org → Trusted Publishing, add a GitHub Actions policy named
ColorHashSharp GitHub Actionsfor the package owner. Set Repository Owner tofernandezja, Repository toColorHashSharp, Workflow File topublish-nuget.yml, and Environment tonuget. Select Push only new package versions and enterColorHashSharpunder 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
mainwith 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.0Check Actions → Publish to NuGet for the result, then verify the version on the ColorHashSharp package page.
Report issues and suggest improvements on GitHub. Contributions are welcome through pull requests.