DotNetColorParser 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotNetColorParser --version 0.1.1
NuGet\Install-Package DotNetColorParser -Version 0.1.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="DotNetColorParser" Version="0.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetColorParser --version 0.1.1
#r "nuget: DotNetColorParser, 0.1.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.
// Install DotNetColorParser as a Cake Addin
#addin nuget:?package=DotNetColorParser&version=0.1.1

// Install DotNetColorParser as a Cake Tool
#tool nuget:?package=DotNetColorParser&version=0.1.1

DotNetColorParser

Build Status DotNetColorParser NuGet Package DotNetColorParser.Abstractions NuGet Package Coverage

.Net library parsing strings with notated colors and converting them to .Net Color object. Library support almost all color notations used in CSS3. Color notations are extendable and customizable.

Usage

Static methods

Any color notation

You can use static method witch converts string with any correct color notation supported by default. e.g:

string stringWithColor = "rgba(255,0,128,0.5)";

var color = ColorParser.Parse(stringWithColor);

In that case method try parse string using all notations added to ColorParser.DefaultProvider collection. This object by default contains all standard color notations.

Specify color notation only

If you wont to parser only one specify notation then you can write:

string stringWithColor = "#ff008080";

var color = ColorParser.Parse<HexRGBANotation>(stringWithColor);

In that case if you pass string like "rgba(255,0,128,0.5)" method throw the UnkownColorNotationException.

Customizing and extending supported color notations

You can add custom color notation support by adding to ColorParser.DefaultProvider custom object implementing IColorNotation interface.

To customize supported by default color notations you can set to ColorParser.DefaultProvider new ColorNotationProvider class instance.

ColorParser.DefaultProvider = new ColorNotationProvider() { HexRGBANotation, RGBANotation, RGBNotation };

Class instance

You can dynamically create new instance of ColorParser class and configure supported notations by passing ColorNotationProvider object into constructor.

var colorParser = new ColorParser(new ColorNotationProvider() { HexRGBANotation, RGBANotation, RGBNotation });

string stringWithColor = "#ff008080";

var color = colorParser.ParseColor(stringWithColor);

If you pass null in ColorParser instead ColorNotationProvider object then ColorParser use ColorParser.DefaultProvider.

Inversion of Control (IoC)

You can use it also with IoC container:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IColorNotationProvider>(()=> new ColorNotationProvider() { HexRGBANotation, RGBANotation, RGBNotation });
    services.AddSingleton<IColorParser, ColorParser>();
}
public class SomeClass
{
    readonly IColorParser colorParser;
    string stringWithColor = "#ff008080";

    public SomeClass(IColorParser colorParser) 
    {
        this.colorParser = colorParser;
    }

    public Color ReadColor()
    {
        return colorParser.ParseColor(_stringWithColor);
    }
}

Supported color spaces:

  • RGB
  • RGBA
  • HSL
  • HSLA
  • HSV

Supported notations:

All this notations are default added to ColorNotationProvider.

Class name Description Syntax e.g.
KnownColorNameNotation Color names in English containing by KnownColor enumerable object. It's contains all color names using by browsers. Colors are in RGB color space. Every color corresponding specify RBG value. colorname silver, blue, black, green, yellow, darkblue
HexRGBANotation RGB or RGBA color expressed through hexadecimal value with # prefix (prefix not necessary to correct recognition). Color can be expressed as three, six or eight hexadecimal characters (0�9, A�F). #RRGGBB[AA]<br />#RGB[A] #FFFFFF, #abc, #20ee33, #ff1250cc
RGBNotation RGB color Syntax 1: rgb(R, G, B)<br />Syntax 2: rgb R G B rgb(255,255,0), rgb( 255, 255, 255 )
RGBANotation The same as RGBNotation but with additional alpha channel. Syntax 1: rgba(R, G, B, A)<br />Syntax 2: rgba R G B A<br />Syntax 3: rgba R G B / A<br />Syntax 4: rgba(R G B / A) rgba(255,255,0,0.5), rgba( 255, 255, 255, 100% )
HSLNotation HSLA color space Syntax 1: hsl(hue, spectrum, lightness)<br />Syntax 2: hsl hue, spectrum, lightness<br />Syntax 4: hsla(hue spectrum lightness)<br /><br /> hsla(180,50%,50%,0.5), hsla(180,50%,50%,30%), hsla(180deg,50%,50%,30%), hsla(4rad,50%,50%,30%), hsla(4ang,50%,50%,30%),hsla 180,50%,50%,0.5, hsla 180 50% 50% 0.5, hsla 180 50% 50% / 0.5
HSLANotation The same as HSLNotation but with additional alpha channel. Syntax 1: hsla(hue, spectrum, lightness, alpha)<br />Syntax 2: hsla hue, spectrum, lightness / alpha<br />Syntax 3: hsla hue spectrum lightness alpha<br />Syntax 4: hsla(hue spectrum lightness alpha) hsla(180,50%,50%,0.5), hsla(180,50%,50%,30%), hsla(180deg,50%,50%,30%), hsla(4rad,50%,50%,30%), hsla(4ang,50%,50%,30%),hsla 180,50%,50%,0.5, hsla 180 50% 50% 0.5, hsla 180 50% 50% / 0.5
HSVNotation HSVA color space Syntax 1: hsla(hue, spectrum, lightness, alpha)<br />Syntax 2: hsla hue, spectrum, lightness / alpha<br />Syntax 3: hsla hue spectrum lightness alpha<br />Syntax 4: hsla(hue spectrum lightness alpha) hsla(180,50%,50%,0.5), hsla(180,50%,50%,30%), hsla(180deg,50%,50%,30%), hsla(4rad,50%,50%,30%), hsla(4ang,50%,50%,30%),hsla 180,50%,50%,0.5, hsla 180 50% 50% 0.5, hsla 180 50% 50% / 0.5

R (red), G (green), B (blue) - number between 0 and 255.

A - decimal number between 0 and 1 or percentage value between 0% and 100%

hue - value between 0 and 360 degrees [deg], value can be also specify in other angels units like radius [rad], gradus [grad], turns [turn].

saturation, lightness, alpha - percentage value between 0% and 100% or decimal number between 0 and 1.

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 was computed.  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. 
.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 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DotNetColorParser:

Package Downloads
RoboLynx.Umbraco.QRCodeGenerator.Core

Part of QR Code Generator for Umbraco. Package implements necessary infrastructure and services to generate QR codes from code under Umbraco CMS.

RoboLynx.Umbraco.QRCodeGenerator.Cache

Cache implementation for QRCodeGenerator.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 4,244 5/22/2021
1.0.0 396 3/31/2021
0.1.1 315 3/30/2021