Skip to content

Blends RGBA colors with different blend modes in JavaScript

License

Notifications You must be signed in to change notification settings

VisualComposer/color-blend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

color-blend

Blends RGBA colors with different blend modes

JavaScript implementation of the blend modes introduced in the W3C Compositing and Blending spec.

Install

$ npm install color-blend --save

Usage

Example

It's simple to wrap your head around, you should get it after just reading this:

var blender = require('color-blend')

// Mix red and lime green
var backdrop = { r: 255, g: 0,   b: 0, a: 0.6 }
var source   = { r: 0,   g: 255, b: 0, a: 0.4 }

blender.normal(backdrop, source)
// Returns { r: 121, g: 134, b: 0, a: 0.76 }

Explanation

This module exports an object—let's agree on calling it blender in this guide—which provides all blend modes listed in the W3C document as methods. Those are: normal, multiply, screen, overlay, darken, lighten, colorDodge, colorBurn, hardLight, softLight, difference, exclusion, hue, saturation, color and luminosity.

They all work the same: Each of these methods takes a backdrop color and a source color as arguments. Those parameters are expected to be RGBA colors represented as plain objects containing the keys r, g, b (each ranging from 0 to 255) and a (ranging from 0 to 1).

The result of the blending operation will be returned as such an RGBA object as well.

Options

The blender also has an options property containing the default blending settings. Change those if you'd like to alter the behaviour of the module.

Here's the default options:

{

  // Set this to true if you want to provide color channel values from 0 to 1 instead of 0 to 255
  unitInput: false,

  // As in the above, but for the result
  unitOutput: false,

  // Set this to false if the result color channel values should not be rounded (only applies if `unitOutput` is false)
  roundOutput: true

}

About

Blends RGBA colors with different blend modes in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%