Giter Club home page Giter Club logo

colorblender's Introduction

Color Blender

Version on NuGet

Blends colors with various blend modes in 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 Sytem.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 Standart 2.0

  • Adobe Photoshop uses slightly different algoritm and rounding to perform color blending, which means that you won't get an identical result.

  • Might be bugs. Please submit an issue if you find one.

Download

Get the library from NuGet Version on NuGet

Thanks

Many thanks to:

Creative Society

creative society logo

We support Creative Society

Creative Society is a worldwide project of all humanity and it concerns everyone.

Project objectives

  • To create conditions for building a creative society on the whole planet by peaceful means.
  • To ask people all over the world whether they want to live in a creative society, and how they envisage it.
  • To provide a platform for a global, international, open discussion of the concept and model of the creative society in all spheres of human life.
  • To find new ways to unite the entire humanity and create conditions for the active participation of every person in the life of society, regardless of social status, religion or nationality.

Join us today!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.