Skip to content

Commit

Permalink
Major overhaul:
Browse files Browse the repository at this point in the history
* Avoid new Function (fixes #28)
* Switch from INCLUDE to Common.js require
* Replace custom build procedure with browserify + plugins
* Enable source map support

Tests are broken at this stage, so [ci skip]. Coming up.
  • Loading branch information
papandreou committed Mar 14, 2016
1 parent 8d09dfe commit 57c931f
Show file tree
Hide file tree
Showing 46 changed files with 654 additions and 1,924 deletions.
29 changes: 0 additions & 29 deletions bin/build.js

This file was deleted.

20 changes: 20 additions & 0 deletions index.js
@@ -0,0 +1,20 @@
module.exports = require('./lib/color')
.use(require('./lib/XYZ'))
.use(require('./lib/LAB'))
.use(require('./lib/HSV'))
.use(require('./lib/HSL'))
.use(require('./lib/CMYK'))

// Convenience functions
.use(require('./lib/plugins/namedColors'))
.use(require('./lib/plugins/clearer.js'))
.use(require('./lib/plugins/darken.js'))
.use(require('./lib/plugins/desaturate.js'))
.use(require('./lib/plugins/greyscale.js'))
.use(require('./lib/plugins/lighten.js'))
.use(require('./lib/plugins/mix.js'))
.use(require('./lib/plugins/negate.js'))
.use(require('./lib/plugins/opaquer.js'))
.use(require('./lib/plugins/rotate.js'))
.use(require('./lib/plugins/saturate.js'))
.use(require('./lib/plugins/toAlpha.js'));
30 changes: 30 additions & 0 deletions lib/CMYK.js
@@ -0,0 +1,30 @@
module.exports = function CMYK(color) {
color.installColorSpace('CMYK', ['cyan', 'magenta', 'yellow', 'black', 'alpha'], {
rgb: function () {
return new color.RGB((1 - this._cyan * (1 - this._black) - this._black),
(1 - this._magenta * (1 - this._black) - this._black),
(1 - this._yellow * (1 - this._black) - this._black),
this._alpha);
},

fromRgb: function () { // Becomes one.color.RGB.prototype.cmyk
// Adapted from http://www.javascripter.net/faq/rgb2cmyk.htm
var red = this._red,
green = this._green,
blue = this._blue,
cyan = 1 - red,
magenta = 1 - green,
yellow = 1 - blue,
black = 1;
if (red || green || blue) {
black = Math.min(cyan, Math.min(magenta, yellow));
cyan = (cyan - black) / (1 - black);
magenta = (magenta - black) / (1 - black);
yellow = (yellow - black) / (1 - black);
} else {
black = 1;
}
return new color.CMYK(cyan, magenta, yellow, black, this._alpha);
}
});
};
29 changes: 29 additions & 0 deletions lib/HSL.js
@@ -0,0 +1,29 @@
module.exports = function HSL(color) {
color.use(require('./HSV'));

color.installColorSpace('HSL', ['hue', 'saturation', 'lightness', 'alpha'], {
hsv: function () {
// Algorithm adapted from http://wiki.secondlife.com/wiki/Color_conversion_scripts
var l = this._lightness * 2,
s = this._saturation * ((l <= 1) ? l : 2 - l),
saturation;

// Avoid division by zero when l + s is very small (approaching black):
if (l + s < 1e-9) {
saturation = 0;
} else {
saturation = (2 * s) / (l + s);
}

return new color.HSV(this._hue, saturation, (l + s) / 2, this._alpha);
},

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

fromRgb: function () { // Becomes one.color.RGB.prototype.hsv
return this.hsv().hsl();
}
});
};
93 changes: 93 additions & 0 deletions lib/HSV.js
@@ -0,0 +1,93 @@
module.exports = function HSV(color) {
color.installColorSpace('HSV', ['hue', 'saturation', 'value', 'alpha'], {
rgb: function () {
var hue = this._hue,
saturation = this._saturation,
value = this._value,
i = Math.min(5, Math.floor(hue * 6)),
f = hue * 6 - i,
p = value * (1 - saturation),
q = value * (1 - f * saturation),
t = value * (1 - (1 - f) * saturation),
red,
green,
blue;
switch (i) {
case 0:
red = value;
green = t;
blue = p;
break;
case 1:
red = q;
green = value;
blue = p;
break;
case 2:
red = p;
green = value;
blue = t;
break;
case 3:
red = p;
green = q;
blue = value;
break;
case 4:
red = t;
green = p;
blue = value;
break;
case 5:
red = value;
green = p;
blue = q;
break;
}
return new color.RGB(red, green, blue, this._alpha);
},

hsl: function () {
var l = (2 - this._saturation) * this._value,
sv = this._saturation * this._value,
svDivisor = l <= 1 ? l : (2 - l),
saturation;

// Avoid division by zero when lightness approaches zero:
if (svDivisor < 1e-9) {
saturation = 0;
} else {
saturation = sv / svDivisor;
}
return new color.HSL(this._hue, saturation, l / 2, this._alpha);
},

fromRgb: function () { // Becomes one.color.RGB.prototype.hsv
var red = this._red,
green = this._green,
blue = this._blue,
max = Math.max(red, green, blue),
min = Math.min(red, green, blue),
delta = max - min,
hue,
saturation = (max === 0) ? 0 : (delta / max),
value = max;
if (delta === 0) {
hue = 0;
} else {
switch (max) {
case red:
hue = (green - blue) / delta / 6 + (green < blue ? 1 : 0);
break;
case green:
hue = (blue - red) / delta / 6 + 1 / 3;
break;
case blue:
hue = (red - green) / delta / 6 + 2 / 3;
break;
}
}
return new color.HSV(hue, saturation, value, this._alpha);
}
});
};
33 changes: 33 additions & 0 deletions lib/LAB.js
@@ -0,0 +1,33 @@
module.exports = function LAB(color) {
color.use(require('./XYZ.js'));

color.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 color.XYZ(
convert(x) * 95.047,
convert(y) * 100.000,
convert(z) * 108.883,
this._alpha
);
}
});
};
64 changes: 64 additions & 0 deletions lib/XYZ.js
@@ -0,0 +1,64 @@
module.exports = function XYZ(color) {
color.installColorSpace('XYZ', ['x', 'y', 'z', 'alpha'], {
fromRgb: function () {
// http://www.easyrgb.com/index.php?X=MATH&H=02#text2
var convert = function (channel) {
return 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);

// Reference white point sRGB D65:
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
return new color.XYZ(
r * 0.4124564 + g * 0.3575761 + b * 0.1804375,
r * 0.2126729 + g * 0.7151522 + b * 0.0721750,
r * 0.0193339 + g * 0.1191920 + b * 0.9503041,
this._alpha
);
},

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

// Reference white point sRGB D65:
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
return new colorRGB(
convert(x * 3.2404542 + y * -1.5371385 + z * -0.4985314),
convert(x * -0.9692660 + y * 1.8760108 + z * 0.0415560),
convert(x * 0.0556434 + y * -0.2040259 + z * 1.0572252),
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 color.LAB(
(116 * y) - 16,
500 * (x - y),
200 * (y - z),
this._alpha
);
}
});
};

0 comments on commit 57c931f

Please sign in to comment.