Skip to content

michaldivis/dark-colors

Repository files navigation

Dark Colors

C# color helpers - color blender.

What's the resulting color of #AABBCC with 70% opacity on a white background?

That's what this library is for!

Nuget

Nuget

DarkColors is available using nuget. To install DarkColors, run the following command in the Package Manager Console

PM> Install-Package Divis.DarkColors

Color blender

Blend multiple colors together with transparency support.

Basic

//The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0); 

//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(125, 55, 13); 

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);

Advanced

//The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0); 

//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(127, 125, 55, 13); 
//The color's amount is set to 50% and it's alpha channel is at 50% so in the result, only 25% of this color will be added on top of the base color.
var colorToAddLayer = new ColorLayer(colorToAdd, 50); 

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAddLayer);

var anotherColorToAdd = Color.FromArgb(255, 13, 79);
var anotherColorToAddLayer = new ColorLayer(anotherColorToAdd, 25);

var threeColorsCombined = ColorBlender.Combine(baseColor, colorToAddLayer, anotherColorToAddLayer);

Transparency

Using the Color's alpha channel:

var baseColor = Color.FromArgb(0, 0, 0);
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);

Using the ColorLayer's AmountPercentage property:

var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(125, 55, 13);
//set 50% transparency using the color AmountPercentage property of the ColorLayer
var colorToAddLayer = new ColorLayer(colorToAdd, 50); 
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);

Using both the Color's alpha channel and ColorLayer's AmountPercentage property:

var baseColor = Color.FromArgb(0, 0, 0);
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13); 
//set 50% transparency using the color AmountPercentage property of the ColorLayer. The resulting color will only be added by 25% because both color's Alpha and layer's AmountPercentage were used.
var colorToAddLayer = new ColorLayer(colorToAdd, 50); 
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);

Color extensions

var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(125, 55, 13);
var twoColorsCombined = baseColor.Combine(colorToAdd);

Color analyzer

⚠️ Warning: this feature is pretty slow at the moment. It can process roughly one mega pixel per second, so it's not recommended to use it on large images.

Find dominant color in an image.

Basic usage

//array of pixels that represents an image
Color[] pixels; 
//returns a list of dominant color candidates, ordered by probability
List<DominantColorCandidate> candidates = ColorAnalyzer.FindDominantColors(pixels); 

Advanced (with configuration)

Color[] pixels;
List<DominantColorCandidate> candidates = ColorAnalyzer.FindDominantColors(pixels, options => 
{
	options.MinSaturation = 0.4f,
	options.MinSpaceCoverage = 0.05f,
	options.ColorGrouping = 0.3f
});