ColorNamesSharp 1.1.0

dotnet add package ColorNamesSharp --version 1.1.0
                    
NuGet\Install-Package ColorNamesSharp -Version 1.1.0
                    
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="ColorNamesSharp" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ColorNamesSharp" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ColorNamesSharp" />
                    
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 ColorNamesSharp --version 1.1.0
                    
#r "nuget: ColorNamesSharp, 1.1.0"
                    
#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.
#:package ColorNamesSharp@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ColorNamesSharp&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ColorNamesSharp&version=1.1.0
                    
Install as a Cake Tool

<img align="left" height="119" width="119" src="https://meodai.github.io/color-names/logo/cockatoo-fill.svg">

Color Names

GitHub Actions Workflow Status GitHub Release NuGet Version NuGet Downloads Join the chat at https://discord.gg/nU63sFMcnX

This library's primary purpose is to be able to specify a color and end up with a fitting name for that color 🌈

Examples:<br>

"#ffffff"White<br> "#facfea"Classic Rose<br> #abcdefAlphabet Blue<br> #123456Incremental Blue<br> #c1b2a3Balanced Beige<br> ...and so on

Where do the names come from?!

Color Names is meant to act as an easy drop-in dependency you can import and start using, meaning it's already bundled with a list of color names, a list that is maintained by another awesome open-sourced project: meodai/color-names. There's around 16.7 million sRGB colors, obviously not all of these are named, but this list provides plenty to work with, we can just extrapolate the closest color from the list. <br>

Ermm actually I don't like the bundled color list 🤓🤓

Color Names remain customisable for those who'd like the extra control: you can use the ColorNameBuilder (usage outlined below) to fully customize the color names used, enabling you to add your own colors and even bypass the default list entirely. Alternatively, you could fork this project yourself and replace/modify the list in ColorLists/Default.csv with whatever you'd like. If you'd like to make changes to the default list, consider reviewing the naming rules of the open sourced list mentioned above and contributing there.

How about performance, and how accurate is the "nearest" color?

The default color list has over 30,000 names (that's a lot). Trying to find the closest color by comparing distance in the 3D color space can be pretty computationally expensive. <br><br> Other libraries with similar functionality seem to often approach this by iterating over all the colors, plotting the sRGB values and calculating Euclidian distance and whatever has the lowest distance is the "closest" color. This has 2 notable concerns: <br>

  1. The sRGB color space isn't all that accurate in terms of visual similarity, ie: 3 sRGB values that are equally apart in terms of raw numerical value are unlikely to be visually "different" by the same factor. Okay... so, how do we put a number on the visual similarity of colors? Fortunately, that's not my job. The CIELAB Color Space has us covered! This color space precisely revolves around positioning colors with uniform visual perception and for this reason, its used for all sorts of color correction work, and is exactly what we need. Perfect, we convert our values from the sRGB color space to the CIELAB color space, problem one solved!
  2. Iterating through >30,000 vectors in a 3D space and finding the distance between all of them to a point is... a lot of calculations. But it's exactly what we need, since that's how we find the Delta-E variance between all our CIELAB colors to see whats the closest. So, we should really try to optimise this. For this we cache our colors in a K-D Tree with 3 dimensions, providing us with fast nearest neighbour searches. This takes the time complexity for searches from O(n) to O(logn). In practice, this makes a pretty substantial difference.

Benchmarks

The repository includes a BenchmarkDotNet suite covering:

  • exact name and hexadecimal lookups;
  • nearest-color lookup using the KD-tree;
  • an equivalent precomputed linear CIELAB scan for comparison;
  • loading the bundled CSV and constructing the search index.

Run all benchmarks from the repository root in Release mode for either supported runtime:

dotnet run --project benchmarks/ColorNamesSharp.Benchmarks -c Release -f net8.0
dotnet run --project benchmarks/ColorNamesSharp.Benchmarks -c Release -f net10.0

To run one group, pass a BenchmarkDotNet filter:

dotnet run --project benchmarks/ColorNamesSharp.Benchmarks -c Release -f net10.0 -- --filter "*LookupBenchmarks*"

The suite supports both .NET 8 and .NET 10, reports managed allocations, and writes detailed reports to BenchmarkDotNet.Artifacts/results. Run benchmarks on an otherwise idle machine and compare results produced on the same hardware and operating system.

<br>

Pretty notes ✨

You can find many resources online about KD-Trees, here's some visuals made by UwUAroze to help you understand how they work: kdTree

You can open that image in a new tab for a nicer, full-resolution view.

<br>

Development

Install the .NET 8 and .NET 10 SDKs to build and test every target framework.

Run the complete test suite from the repository root:

dotnet test ColorNamesSharp.sln

Usage

You can download and install the nuget package from here. <br> Or you can clone this repository and use it as a library in your project.

dotnet add package ColorNamesSharp

Supported frameworks

Target Intended consumers
net10.0 Current .NET applications
net8.0 Modern .NET applications
netstandard2.0 Older .NET implementations and .NET Framework applications

Creating the instance

ColorNames colorNames = new ColorNamesBuilder()
	.Add("Best Blue", "#3299fe") // Add your own custom colors
	.LoadDefault() // Load the default color list
	.AddFromCsv("path/to/your/colorlist.csv") // Add a custom color list from a csv file
	.Build(); // Get a new ColorNames instance that includes all the colors you've added

Builder methods are chainable, and colors remain in insertion order. Calling LoadDefault() more than once intentionally adds the default list more than once.

Custom CSV files must include a header row followed by name,hex rows:

name,hex
Best Blue,#3299fe
Classic Rose,#facfea

Accessing the configured colors

// Enumerate the colors in their original insertion order
foreach (NamedColor color in colorNames.Colors)
    Console.WriteLine($"{color.Name}: {color.Hex}");

// Exact lookups are case-insensitive and do not calculate color distance
if (colorNames.TryGetByName("alphabet blue", out NamedColor? byName))
    Console.WriteLine(byName.Hex);

if (colorNames.TryGetByHex("#abcdef", out NamedColor? byHex))
    Console.WriteLine(byHex.Name);

Getting a fitting color name

NamedColor customNamedColor = new("Best Blue", 50, 153, 254);

// You can directly get the name of the color as a string
string colorNameFromHex = colorNames.FindClosestColorName("#ffffff");
string colorNameFromRgb = colorNames.FindClosestColorName(255, 255, 255);
string colorNameFromNamedColor = colorNames.FindClosestColorName(customNamedColor);

// Or similarly you can get the NamedColor object
NamedColor? namedColorFromHex = colorNames.FindClosestColor("#ffffff");
NamedColor? namedColorFromRgb = colorNames.FindClosestColor(255, 255, 255);
NamedColor? namedColorFromNamedColor = colorNames.FindClosestColor(customNamedColor);

// Or a random color
NamedColor? randomColor = colorNames.GetRandomNamedColor();

Hexadecimal inputs use the six-digit #RRGGBB form. Invalid values throw ArgumentException; an empty color collection returns null from FindClosestColor and "Unknown" from FindClosestColorName.

The package also includes an XML documentation file, so the public API descriptions and parameter details appear in IDE IntelliSense.

Credits

This library is a C# implementation of the original color names library by meodai and it takes inspiration from UwUAroze's implementation in Kotlin. Huge thanks to both of them for their work!

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 was computed.  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 was computed.  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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net8.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.1.0 106 7/16/2026
1.0.1 94 7/16/2026
1.0.0 716 11/19/2024