Skip to content

Commit

Permalink
Added new color spaces: XYZ, CIE-L*ab
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
Munter committed Mar 17, 2013
1 parent ff2b5dc commit ccc8cb3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
33 changes: 33 additions & 0 deletions lib/color/LAB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*global INCLUDE, installColorSpace, ONECOLOR*/
INCLUDE('lib:color.js');
INCLUDE('lib:color/XYZ.js');

installColorSpace('LAB', ['l', 'a', 'b', 'alpha'], {
fromRgb: function () {
return this.xyz().lab();
},

rgb: function () {
return this.xyz().rgb();
},

xyz: function () {
// http://www.easyrgb.com/index.php?X=MATH&H=08#text8
var convert = function (channel) {
var pow = Math.pow(channel, 3);
return pow > 0.008856 ?
pow :
(channel - 16 / 116) / 7.87;
},
y = (this._l + 16) / 116,
x = this._a / 500 + y,
z = y - this._b / 200;

return new ONECOLOR.XYZ(
convert(x) * 95.047,
convert(y) * 100.000,
convert(z) * 108.883,
this._alpha
);
}
});
62 changes: 62 additions & 0 deletions lib/color/XYZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*global INCLUDE, installColorSpace, ONECOLOR*/
INCLUDE('lib:color.js');

installColorSpace('XYZ', ['x', 'y', 'z', 'alpha'], {
fromRgb: function () {
// http://www.easyrgb.com/index.php?X=MATH&H=02#text2
var convert = function (channel) {
// assume sRGB
return 100 * (channel > 0.04045 ?
Math.pow((channel + 0.055) / 1.055, 2.4) :
channel / 12.92);
},
r = convert(this._red),
g = convert(this._green),
b = convert(this._blue);

return new ONECOLOR.XYZ(
r * 0.4124 + g * 0.3576 + b * 0.1805,
r * 0.2126 + g * 0.7152 + b * 0.0722,
r * 0.0193 + g * 0.1192 + b * 0.9505,
this._alpha
);
},

rgb: function () {
// http://www.easyrgb.com/index.php?X=MATH&H=01#text1
var x = this._x / 100,
y = this._y / 100,
z = this._z / 100,
convert = function (channel) {
return channel > 0.0031308 ?
1.055 * Math.pow(channel, 1 / 2.4) - 0.055 :
12.92 * channel;
};

return new ONECOLOR.RGB(
convert(x * 3.2406 + y * -1.5372 + z * -0.4986),
convert(x * -0.9689 + y * 1.8758 + z * 0.0415),
convert(x * 0.0557 + y * -0.2040 + z * 1.0570),
this._alpha
);
},

lab: function () {
// http://www.easyrgb.com/index.php?X=MATH&H=07#text7
var convert = function (channel) {
return channel > 0.008856 ?
Math.pow(channel, 1 / 3) :
7.787037 * channel + 4 / 29;
},
x = convert(this._x / 95.047),
y = convert(this._y / 100.000),
z = convert(this._z / 108.883);

return new ONECOLOR.LAB(
(116 * y) - 16,
500 * (x - y),
200 * (y - z),
this._alpha
);
}
});
4 changes: 4 additions & 0 deletions lib/color/_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// This file is purely for the build system
INCLUDE('lib:color.js');
INCLUDE('lib:color-namedColors.js');

// Order is important to prevent channel name clashes. Lab <-> hsL
INCLUDE('lib:color/XYZ.js');
INCLUDE('lib:color/LAB.js');
INCLUDE('lib:color/HSV.js');
INCLUDE('lib:color/HSL.js');
INCLUDE('lib:color/CMYK.js');
Expand Down
29 changes: 22 additions & 7 deletions test/color-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ var vows = require('vows'),
{
name: 'HSL',
channels: ['hue', 'saturation', 'lightness', 'alpha']
}/*,
{
name: 'CMYK',
channels: ['cyan', 'magenta', 'yellow', 'black', 'alpha']
}*/
}
];

var namedColorSamples = {
Expand Down Expand Up @@ -304,8 +300,27 @@ function createTest(bundleFileName) {
}

vows.describe('Color').addBatch({
'all, debug': createTest('../one-color-all-debug'),
'all, minified': createTest('../one-color-all'),
'base, debug': createTest('../one-color-debug'),
'base, minified': createTest('../one-color')
}).export(module);


spaces.push(
{
name: 'XYZ',
channels: ['x', 'y', 'z', 'alpha']
},
{
name: 'LAB',
channels: ['l', 'a', 'b', 'alpha']
}/*
name: 'CMYK',
channels: ['cyan', 'magenta', 'yellow', 'black', 'alpha']
}*/
);


vows.describe('Color-all').addBatch({
'all, debug': createTest('../one-color-all-debug'),
'all, minified': createTest('../one-color-all')
}).export(module);

0 comments on commit ccc8cb3

Please sign in to comment.