Skip to content

DigitalE4rth/ColorBlender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Color Blender

Mixing colors using different blending modes with C#

This is a Color Blending library that implements all the blending modes introduced in the W3C Compositing and Blending spec

Table of Content

Features

Blend Modes

This library provides an implementation for all blend modes listed in the W3C document, such as:

  • Normal
  • Multiply
  • Screen
  • Overlay
  • Darken
  • Lighten
  • Color Dodge
  • Color Burn
  • Hard Light
  • Soft Light
  • Difference
  • Exclusion
  • Hue
  • Color
  • Luminosity
  • Saturation

Color types

Build-in support for commonly used color types: Unit Rgb, Rgb, Hsl, Hex and Color from System.Drawing assembly.

Unit Rgb

It is a higher precision form of RGB color that uses decimal values.
All the inner blending calculations are performed in this form of a color.
The Red, Green, Blue and Alpha channels are represented by a fractional value between 0 and 1.

RGB

The Red, Green and Blue channels are represented by a fractional value between 0 and 255.
The Alpha channel is represented by a fractional value between 0 and 1.

Color

Default Color Struct from System.Drawing assembly

HSL

The Hue channel is represented by a fractional value between 0 and 360.
The Saturation and Luminosity channels are represented by a fractional value between 0 and 100.
The Alpha channel is represented by a fractional value between 0 and 1.

HEX

The Red, Green, Blue and Alpha channels are represented by a hexadecimal form of a String between "00" and "ff".
Class constructor supports any form of a Hex color, such as it's short form #rgb, and full forms with leading or trailing Alpha channel (#aarrggbb and #rrggbbaa) with an optional # sign.
The Hex Object also can be represented as a String with all the forms described above.

Example
using ColorBlender;

static void Main(string[] args)
{
    var hex        = new HEX("#bbccdd");
    var anotherHex = new HEX("bbccdd");
    var ahex       = new HEX("#aabbccdd", EHEXFormat.AHEX);
    var hexa       = new HEX("#bbccddaa", EHEXFormat.HEXA);
    var shortHex   = new HEX("#abc");
    var userHex    = new HEX("bb", "cc", "dd", "aa");

    var resultHexAOpt   = hex.ToString(EHEXOutputFormat.HEXAOpt,   EHashSignFormat.Visible); // Result: "#bbccdd"
    var resultOptAHex   = hex.ToString(EHEXOutputFormat.OptAHEX,   EHashSignFormat.Hidden);  // Result: "bbccdd"
    var resultHexAConst = hex.ToString(EHEXOutputFormat.HEXAConst, EHashSignFormat.Visible); // Result: "#bbccddff"
    var resultConstAHex = hex.ToString(EHEXOutputFormat.ConstAHEX, EHashSignFormat.Hidden);  // Result: "ffbbccdd"
}

Converter

Built-in converter for all the color types described above, with an optional output rounding.

Usage
using ColorBlender;

private readonly IColorConverterService _colorConverterService = new ColorConverterService();

static void Main(string[] args)
{
   var rgb = new RGB(211, 107, 184, 0.94);

   // Conversion
   var resultRgb   = _colorConverterService.ToRgb(rgb);   // Result: new RGB(211, 107, 184, 0.94);
   var resultHex   = _colorConverterService.ToHex(rgb);   // Result: new HEX("d3", "6b", "b8", "ef")
   var resultHsl   = _colorConverterService.ToHsl(rgb);   // Result: new HSL(315, 54, 62, 0.94)
   var resultColor = _colorConverterService.ToColor(rgb); // Result: new Color(211, 107, 184, 240) *;

   // Conversion with rounding (Input Color, Rounding, Number of decimal places in the return value)
   var resultUrgb  = _colorConverterService.ToURgb(rgb, true, 5); // Result: new URGB(0.82645, 0.41961, 0.72157, 0.94);
}

* This is not a proper way to create a system color. It is used here in this form only to display the result.

Blending

Create an instance of the ColorBlenderService and use it blend your colors.
The result of the blending will be returned as an Object of a Uniform Color type, that can be further represented as any color type you want. See the example bellow.

Example

Usage

using ColorBlender;

private readonly IColorBlenderService _colorBlenderService = new ColorBlenderService();

static void Main(string[] args)
{
    var background = new RGB(105, 151, 206, 0.8);
    var foreground = new RGB(247, 92, 177, 0.7);

    RGB   resultRgb   = _colorBlenderService.Normal(background, foreground).ToRgb();   // Result: new RGB(211, 107, 184, 0.94);
    HEX   resultHex   = _colorBlenderService.Normal(background, foreground).ToHex();   // Result: new HEX("d3", "6b", "b8", "ef");
    HSL   resultHsl   = _colorBlenderService.Normal(background, foreground).ToHsl();   // Result: new HSL(315, 54, 62, 0.94);
    Color resultColor = _colorBlenderService.Normal(background, foreground).ToColor(); // Result: new Color(211, 107, 184, 240) *;
    // Rounding the output value to 5 decimal places in the return value.
    URGB  resultURgb  = _colorBlenderService.Normal(background, foreground).ToUrgb(true, 5); // Result: new URGB(0.82645, 0.41986, 0.72315, 0.94);
}

* This is not a proper way to create a system color. It is used here in this form only to display the result.

Notes

  • Powered by .NET Standard 2.0
  • Adobe Photoshop uses slightly different algorithm and rounding to perform color blending, which means that you won't get an identical result.
  • May contain bugs. Please submit an issue if you find any.

Thanks

Many thanks to:

About

Color mixer with different color blending modes

Topics

Resources

License

Stars

Watchers

Forks

Languages