ColorMix

The color mixing open source library

Fork me on GitHub

ColorMix.js

A javascript library for mixing, blending and easily manipulating colors and color spaces [Hex, RGB, XYZ, HSL, Lab].

The mixing methods are using the Lab color space.
This color space is designed to approximate the human vision, that's why a Lab mix is closer to our vision than a RGB mix.

It means that your colors will first be converted to this schema, what can be a bit heavy / slow if you use it inside an interval as requestAnimationFrame.

Okay, and technically ?

ColorMix is a global JS singleton object, so accessible from anywhere.
It contains some direct methods to mix several colors, get and set up a custom colors gradient, and blend a reference in this gradient.
It contains also two subclasses:

  • ColorMix.Color() - A class to easily manipulating colors
  • ColorMix.ColorSpace - A singleton to easily convert colors into different color spaces.

Watch the documentation below for more informations !

Is it free ?

Sure! And open-sourced on GitHub. Feel free to star or fork. Pull-requests are welcome!

Do you find it usefull ?

Just send me your feedbacks!

And if you really like it, you can also

  1. Note - It is recommended to use it with jQuery, however it is not required
    <!DOCTYPE html>
    <head>
    	[...]
    </head>
    <body>
    	[...]
    	<script src='/path/to/your/scripts/jquery-1.9.1.min.js'></script>
    	<script src='/path/to/your/scripts/colormix.js'></script>
    	<script>
    		// Use ColorMix here !
    		var Color = new ColorMix.Color(255, 255, 255);
    	</script>
    </body>
    
  2. var C1 = new ColorMix.Color(41, 51, 61),
    	C2 = new ColorMix.Color(125, 140, 220),
    	C3 = new ColorMix.Color(255, 128, 0),
    	mix = ColorMix.mix([C1, C2, C3], [25, 25, 50]);
    
    console.log(C1, C2, C3, mix);
    alert('Your mixed color is: rgb(' + mix.red + ', ' + mix.green + ', ' + mix.blue + ')');
    
  3. Note - your gradient must be an array of objects as following
    // Your objects must have 2 properties:
    - Reference [integer], who is basically the "value" of the gradient step
    - Color [object], who is just an object with red, green and blue properties [integer values; 0-255]
    Example:
    var gradient = [{
    	reference: (Ref),
    	color: {
    		red: (R),
    		green: (G),
    		blue: (B)
    	}
    }];
    
    Note - your gradient should be sorted by the reference property of its objects, from smallest to biggest
    // Set an array of objects
    ColorMix.setGradient([
    	{ reference: 0, color: { red: 0, green: 0, blue: 255 } },
    	{ reference: 25, color: { red: 64, green: 0, blue: 191 } },
    	{ reference: 50, color: { red: 128, green: 0, blue: 128 } },
    	{ reference: 75, color: { red: 191, green: 0, blue: 64 } },
    	{ reference: 100, color: { red: 255, green: 0, blue: 0 } }
    ]);
    
  4. var color = ColorMix.blend(69);
    console.log(color);
    alert('Your blended color is: rgb(' + color.red + ', ' + color.green + ', ' + color.blue + ')');
    
  5. Note - You can chain operations for a more comfortable syntax.
    var color = ColorMix.blend(69).useAsBackground('body');
    

ColorMix

Average mixing of two colors

Result