.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.
You can use static method witch converts string with any correct color notation supporting by default. e.g:
string stringWithColor = "rgba(255,0,128,0.5)";
var color = ColorParser.Parse(stringWithColor);
In that case method parse string using all notations added to ColorParser.DefaultProvider
collection. This object by default contains all build in color notations.
If you wont to parser support 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
.
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, CustomNotation };
You can dynamically create new instance of ColorParser class and configure supporting 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
.
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);
}
}
- RGB
- RGBA
- HSL
- HSLA
- HSV
All this notations are default added to ColorNotationProvider.
Class name | Description | Syntax | e.g. |
---|
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
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]
#RGB[A]
#FFFFFF
#abc
#20ee33
#ff1250cc
- rgb(R, G, B)
- rgb R G B
rgb(255,255,0)
rgb( 255, 255, 255 )
The same as RGBNotation but with additional alpha channel.
- rgba(R, G, B, A)
- rgba R G B A
- rgba R G B / A
- rgba(R G B / A)
rgba(255,255,0,0.5)
rgba( 255, 255, 255, 100% )
hsl(hue, spectrum, lightness)
hsl hue, spectrum, lightness
hsla(hue spectrum lightness)
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
The same as HSLNotation but with additional alpha channel.
hsla(hue, spectrum, lightness, alpha)
hsla hue, spectrum, lightness / alpha
hsla hue spectrum lightness alpha
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
hsla(hue, spectrum, lightness, alpha)
hsla hue, spectrum, lightness / alpha
hsla hue spectrum lightness alpha
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.