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.
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:
Watch the documentation below for more informations !
Sure! And open-sourced on GitHub. Feel free to star or fork. Pull-requests are welcome!
And if you really like it, you can also
<!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>
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 + ')');
// 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) } }];
// 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 } } ]);
var color = ColorMix.blend(69); console.log(color); alert('Your blended color is: rgb(' + color.red + ', ' + color.green + ', ' + color.blue + ')');
var color = ColorMix.blend(69).useAsBackground('body');
Integer - The red value of the color [0 - 255]
Integer - The green value of the color [0 - 255]
Integer - The blue value of the color [0 - 255]
Set the RGB properties from the integer values R, G and B.
var color = new ColorMix.Color(255, 255, 255); // ColorMix.Color { red: 255, green: 255, blue: 255 }
var color = new ColorMix.Color('#FFFFFF'); // ColorMix.Color { red: 255, green: 255, blue: 255 }
Set the RGB properties from an hexadecimal String.
var color = new ColorMix.Color().fromHex('#FFFFFF'); // ColorMix.Color { red: 255, green: 255, blue: 255 }
Getter - returns the red value of the color
var red = new ColorMix.Color(255, 255, 255).getRed();
Setter - set the red value for a color, and returns the color instance.
var red = new ColorMix.Color(255, 255, 255).setRed(0);
Getter - returns the green value of the color
var green = new ColorMix.Color(255, 255, 255).getGreen();
Setter - set the green value for a color, and returns the color instance.
var green = new ColorMix.Color(255, 255, 255).setGreen(0);
Getter - returns the blue value of the color
var blue = new ColorMix.Color(255, 255, 255).getBlue();
Setter - set the blue value for a color, and returns the color instance.
var blue = new ColorMix.Color(255, 255, 255).setBlue(0);
Format the color for a given mode. Returns a string representation of the color.
var colorName = new ColorMix.Color(255, 255, 255).toString('rgb'); // colorName = rgb(255, 255, 255);
Set this color as CSS background-color for a given selector. Returns the colors instance.
var colorName = new ColorMix.Color(255, 255, 255).useAsBackground('body'); // <body>.style.backgroundColor = rgb(255, 255, 255);
Set this color as CSS color for a given selector. Returns the colors instance.
var colorName = new ColorMix.Color(255, 255, 255).useAsColor('body'); // <body>.style.color = rgb(255, 255, 255);
Create and returns an RGB-color object
var RGB = new ColorMix.ColorSpace.RGB(255, 255, 255); // { red: 255, green: 255, blue: 255 }
Create and returns an XYZ-color object
var XYZ = new ColorMix.ColorSpace.XYZ(0, 0, 0); // { x: 0, y: 0, z: 0 }
Create and returns an HSL-color object
var HSL = new ColorMix.ColorSpace.HSL(360, 100, 100); // { hue: 360, sat: 100, lig: 100 }
Create and returns an Lab-color object
var Lab = new ColorMix.ColorSpace.Lab(100, 0, 0); // { L: 100, a: 0, b: 0 }
Convert a RGB color to a XYZ one and returns a XYZ-color object.
var XYZ = new ColorMix.ColorSpace.RGBtoXYZ(255, 255, 255); // { x: 95, y: 100, z: 108 }
Convert a RGB color to a HSL one and returns a HSL-color object.
var HSL = new ColorMix.ColorSpace.RGBtoHSL(255, 255, 255); // { hue: 0, sat: 0, lig: 100 }
Convert a RGB color to a Lab one and returns a Lab-color object.
var Lab = new ColorMix.ColorSpace.RGBtoLab(255, 255, 255); // { L: 100, a: 0, b: 0 }
Convert a XYZ color to a RGB one and returns a RGB-color object.
var RGB = new ColorMix.ColorSpace.XYZtoRGB(50, 50, 50); // { red: 204, green: 183, blue: 180 }
Convert a XYZ color to a Lab one and returns a Lab-color object.
var Lab = new ColorMix.ColorSpace.XYZtoLab(50, 50, 50); // { L: 76.06926101415557, a: 6.777038667061586, b: 4.4398523641493215 }
Convert a XYZ color to a Lab one and returns a Lab-color object.
var RGB = new ColorMix.ColorSpace.LabtoRGB(100, 0.00526049995830391, -0.010408184525267927); // { red: 255, green: 255, blue: 255 }
Convert a XYZ color to a Lab one and returns a Lab-color object.
var XYZ = new ColorMix.ColorSpace.LabtoXYZ(100, 0.00526049995830391, -0.010408184525267927); // { x: 95.05, y: 100, z: 108.89999999999996 }
Mix an array of colors with corresponding percents
var C1 = new ColorMix.Color(0, 152, 204), C2 = new ColorMix.Color(0, 120, 240), mix = ColorMix.mix([C1, C2], [50, 50]); // ColorMix.Color { red: 18, green: 136, blue: 222 } --> Average between C1 and C2 in Lab color space
Getter - returns the gradient of the ColorMix.
var gradient = ColorMix.getGradient(); // [{ reference: (reference), color: (color) }]
Setter - set the gradient for the ColorMix.
ColorMix.setGradient([ { reference: -100, color: { red: 0, green: 0, blue: 0 } }, { reference: 100, color: { red: 255, green: 255, blue: 255 } } ]);
Blend a reference with the gradient and returns the mixed color instance.
var blend = ColorMix.blend(50); // ColorMix.Color { red: (red), green: (green), blue: (blue) }