Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9f3bb38
Update nugets.
Jan 30, 2026
21f7dc9
Add .NET 10.0 target framework support (Project now targets: netstand…
Jan 30, 2026
6d620af
Increase test coverage from 42% to 98%
Jan 30, 2026
cb4fa29
perf: Optimize BKDRHash by eliminating unnecessary string conversions
Feb 8, 2026
f2d80a6
perf: Remove Debug.WriteLine calls from hot paths
fernandezja Feb 8, 2026
a14656b
perf: Replace ArrayList with generic List<double> in Options
fernandezja Feb 9, 2026
b6e1eea
perf: Cache helper instances in ColorHash to reduce allocations
fernandezja Feb 9, 2026
97781b6
feat: Add support for custom hash functions (closes #1)
fernandezja Feb 9, 2026
77e0d02
build: upgrade .NET targets (11 & 10) and NuGet dependencies
fernandezja Nov 22, 2026
d875298
ci: validate .NET 10 and 11 on Windows and Linux
fernandezja Nov 22, 2026
398d913
ci: upgrade GitHub Actions to Node.js 24-compatible versions
fernandezja Nov 22, 2026
eb02609
docs: update README CI badges
fernandezja Nov 22, 2026
90bd616
docs: update README CI badges
fernandezja Nov 22, 2026
287514f
feat!: adopt SHA-256 as the default color hash
fernandezja Nov 22, 2026
9d67f77
fix!: validate color options at construction
fernandezja Nov 22, 2026
34b4626
feat!: match color-hash (original code) by default
fernandezja Nov 22, 2026
30888a0
feat: add original and updated icon assets
fernandezja Nov 22, 2026
72d6ba2
docs: update README to improve badge formatting
fernandezja Nov 22, 2026
af62229
chore: update version to 2.0.0 and adjust target frameworks in nuspec
fernandezja Nov 22, 2026
03f6f19
feat: add CircleCI configuration for .NET testing workflows
fernandezja Nov 22, 2026
702a67d
feat: replace checkout step with custom Git commands for better control
fernandezja Nov 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: 2.1

jobs:
test:
parameters:
dotnet-version:
type: string
docker:
- image: "mcr.microsoft.com/dotnet/sdk:<< parameters.dotnet-version >>"
steps:
- run:
name: Check out the GitHub commit over HTTPS
command: |
if ! command -v git >/dev/null 2>&1; then
apt-get update
apt-get install -y --no-install-recommends git
fi

git init --quiet
git remote add origin "https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}.git"
GIT_TERMINAL_PROMPT=0 git fetch --depth=1 origin "$CIRCLE_SHA1"
git checkout --detach FETCH_HEAD
- run:
name: Build and test in Release
command: >-
dotnet test src/ColorHashSharp.Tests/ColorHashSharp.Tests.csproj
--configuration Release
-p:TargetFrameworks=net<< parameters.dotnet-version >>
-p:GeneratePackageOnBuild=false

workflows:
dotnet-tests:
jobs:
- test:
matrix:
parameters:
dotnet-version: ["10.0", "11.0"]
68 changes: 68 additions & 0 deletions .github/workflows/ci-dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CI

on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]

permissions:
contents: read

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build (${{ matrix.os }}, ${{ matrix.framework }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest]
framework: [net10.0, net11.0]

steps:
- name: Checkout
uses: actions/checkout@v5

- name: Setup .NET SDKs
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
10.0.x
11.0.x

- name: Restore library
run: dotnet restore src/ColorHashSharp/ColorHashSharp.csproj

- name: Build library
run: dotnet build src/ColorHashSharp/ColorHashSharp.csproj --configuration Release --framework ${{ matrix.framework }} --no-restore

test:
name: Test (${{ matrix.os }}, ${{ matrix.framework }})
needs: build
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest]
framework: [net10.0, net11.0]

steps:
- name: Checkout
uses: actions/checkout@v5

- name: Setup .NET SDKs
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
10.0.x
11.0.x

- name: Restore tests
run: dotnet restore src/ColorHashSharp.Tests/ColorHashSharp.Tests.csproj

- name: Test
run: dotnet test src/ColorHashSharp.Tests/ColorHashSharp.Tests.csproj --configuration Release --framework ${{ matrix.framework }} --no-restore --verbosity normal
31 changes: 0 additions & 31 deletions .github/workflows/dotnet-core.yml

This file was deleted.

54 changes: 47 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# ColorHashSharp
Generate color based on the given string. C# port of [ColorHash Javascript Library](https://github.com/zenozeng/color-hash).

[![NuGet version (ColorHashSharp)](https://img.shields.io/nuget/v/ColorHashSharp.svg?style=flat-square)](https://www.nuget.org/packages/ColorHashSharp/) &nbsp; [![NuGet Downloads](https://img.shields.io/nuget/dt/ColorHashSharp.svg)](https://www.nuget.org/packages/ColorHashSharp/) &nbsp; [![CodeFactor](https://www.codefactor.io/repository/github/fernandezja/colorhashsharp/badge)](https://www.codefactor.io/repository/github/fernandezja/colorhashsharp)
[![NuGet version (ColorHashSharp)](https://img.shields.io/nuget/v/ColorHashSharp.svg?style=flat-square)](https://www.nuget.org/packages/ColorHashSharp/) &nbsp; [![NuGet Downloads](https://img.shields.io/nuget/dt/ColorHashSharp.svg)](https://www.nuget.org/packages/ColorHashSharp/) &nbsp;

[![CodeFactor](https://www.codefactor.io/repository/github/fernandezja/colorhashsharp/badge)](https://www.codefactor.io/repository/github/fernandezja/colorhashsharp) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/7b256a3706a2434e9984dfb9f0d3da93)](https://app.codacy.com/gh/fernandezja/ColorHashSharp/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)


[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml/badge.svg)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml)&nbsp; [![Build status](https://fernandezja.visualstudio.com/ColorHashSharp/_apis/build/status/ColorHashSharp-CI)](https://fernandezja.visualstudio.com/ColorHashSharp/_build/latest?definitionId=4)

[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml/badge.svg)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml)&nbsp; [![Build status](https://fernandezja.visualstudio.com/ColorHashSharp/_apis/build/status/ColorHashSharp-CI)](https://fernandezja.visualstudio.com/ColorHashSharp/_build/latest?definitionId=4)


#### Status


|Actions |master |develop |
|Actions |main |develop |
|--- |--- |--- |
|CI |[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml/badge.svg?branch=master)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml) |[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml/badge.svg?branch=develop)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/dotnet-core.yml) |
|CI |[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml/badge.svg?branch=main)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml) |[![CI](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml/badge.svg?branch=develop)](https://github.com/fernandezja/ColorHashSharp/actions/workflows/ci-dotnet.yml) |


#### Basic
Expand All @@ -27,18 +29,56 @@ var colorHash = new ColorHash();
var hsl = colorHash.Hsl("Hello World");

Debug.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}");
// H = 225 | S = 0,35 | L = 0,65
// 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= 135 | G = 150 | B = 197
// R = 121 | G = 132 | B = 210

//Hex (return string)
var hex = colorHash.Hex("Hello World");

Debug.WriteLine($" hex = {hex}");
// hex = 8796C5
// 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.

```csharp
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icon/ColorHashSharp-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions samples/CustomHashFunction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Custom Hash Function Example

This example demonstrates how to use a custom hash function with ColorHashSharp.

## Basic Usage with Custom Hash Function

```csharp
using Fernandezja.ColorHashSharp;
using Fernandezja.ColorHashSharp.Interfaces;

// 1. Create a custom hash function by implementing IHashFunction
public class SimpleCharCodeHash : IHashFunction
{
public ulong Generate(string value)
{
if (string.IsNullOrEmpty(value))
return 0;

ulong hash = 0;
foreach (char c in value)
{
hash += (ulong)c;
}
return hash;
}
}

// 2. Use the custom hash function
var customHash = new SimpleCharCodeHash();
var options = new Options
{
HashFunction = customHash
};

var colorHash = new ColorHash(options);

// Generate colors using your custom hash
var hsl = colorHash.Hsl("Hello World");
var rgb = colorHash.Rgb("Hello World");
var hex = colorHash.Hex("Hello World");

Console.WriteLine($"HSL: H={hsl.H}, S={hsl.S}, L={hsl.L}");
Console.WriteLine($"RGB: R={rgb.R}, G={rgb.G}, B={rgb.B}");
Console.WriteLine($"Hex: {hex}");
```

## Advanced Example: MD5-based Hash Function

```csharp
using System.Security.Cryptography;
using System.Text;

public class MD5HashFunction : IHashFunction
{
public ulong Generate(string value)
{
using (var md5 = MD5.Create())
{
byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value));

// Convert first 8 bytes to ulong
ulong hash = 0;
for (int i = 0; i < 8 && i < hashBytes.Length; i++)
{
hash |= ((ulong)hashBytes[i]) << (i * 8);
}
return hash;
}
}
}

// Use MD5-based hash
var options = new Options
{
HashFunction = new MD5HashFunction(),
L = new List<double> { 0.5 },
S = new List<double> { 0.7 }
};

var colorHash = new ColorHash(options);
var color = colorHash.Hex("SecureString");
```

## Why Use Custom Hash Functions?

- **Consistency across platforms**: Use a specific hash that behaves identically everywhere
- **Security considerations**: Use cryptographic hashes for sensitive data
- **Domain-specific needs**: Implement hash functions optimized for your data patterns
- **Testing**: Create predictable hash functions for unit tests

## Default Behavior

ColorHashSharp uses SHA-256 by default. Set `Options.HashFunction` to select
another implementation.

```csharp
var colorHash1 = new ColorHash();
var colorHash2 = new ColorHash(new Options()); // Also uses SHA-256
var javaScriptBkdr = new ColorHash(new Options { HashFunction = new JavaScriptBkdrHash() });
var historical = new ColorHash(new Options
{
UseJavaScriptCompatibility = false,
HashFunction = new BKDRHash()
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading