color-space
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -65,3 +65,3 @@ var rgb = require('./rgb'); | ||
if (max == 0) | ||
if (max === 0) | ||
s = 0; | ||
@@ -68,0 +68,0 @@ else |
33
husl.js
@@ -1,5 +0,34 @@ | ||
//@link http://www.boronine.com/husl/ | ||
//TODO | ||
var xyz = require('./xyz'); | ||
var lchuv = require('./lchuv'); | ||
var _husl = require('husl'); | ||
/** | ||
* A uniform wrapper for husl. | ||
* // http://www.boronine.com/husl/ | ||
* | ||
* @module color-space/husl | ||
*/ | ||
module.exports = { | ||
name: 'husl', | ||
min: [0,0,0], | ||
max: [360,100,100], | ||
channel: ['hue', 'saturation', 'lightness'], | ||
lchuv: _husl._conv.husl.lch, | ||
xyz: function(arg){ | ||
return lchuv.xyz(_husl._conv.husl.lch(arg)); | ||
}, | ||
//a shorter way to convert to huslp | ||
huslp: function(arg){ | ||
return _husl._conv.lch.huslp( _husl._conv.husl.lch(arg)); | ||
} | ||
}; | ||
//extend lchuv, xyz | ||
lchuv.husl = _husl._conv.lch.husl; | ||
xyz.husl = function(arg){ | ||
return _husl._conv.lch.husl(xyz.lchuv(arg)); | ||
}; |
28
huslp.js
@@ -1,4 +0,28 @@ | ||
//TODO | ||
var xyz = require('./xyz'); | ||
var lchuv = require('./lchuv'); | ||
var _husl = require('husl'); | ||
/** | ||
* A uniform wrapper for huslp. | ||
* // http://www.boronine.com/husl/ | ||
* | ||
* @module color-space/huslp | ||
*/ | ||
module.exports = { | ||
name: 'huslp', | ||
min: [0,0,0], | ||
max: [360,100,100], | ||
channel: ['hue', 'saturation', 'lightness'], | ||
}; | ||
lchuv: _husl._conv.huslp.lch, | ||
xyz: function(arg){return lchuv.xyz(_husl._conv.huslp.lch(arg));}, | ||
//a shorter way to convert to husl | ||
husl: function(arg){ | ||
return _husl._conv.lch.husl( _husl._conv.huslp.lch(arg)); | ||
} | ||
}; | ||
//extend lchuv, xyz | ||
lchuv.huslp = _husl._conv.lch.huslp; | ||
xyz.huslp = function(arg){return _husl._conv.lch.huslp(xyz.lchuv(arg));}; |
@@ -1,4 +0,3 @@ | ||
//TODO: save hue on setting sat = 0; | ||
//TODO: if to start with lchuv, it gets bad rgb/other values | ||
//TODO: specify exact lchuv/luv limits | ||
//TODO: check luv/lch by boronine husl lib | ||
@@ -15,5 +14,8 @@ var spaces = { | ||
lchuv: require('./lchuv'), | ||
luv: require('./luv') | ||
luv: require('./luv'), | ||
husl: require('./husl'), | ||
huslp: require('./huslp') | ||
}; | ||
//make each space able to transform to every other space | ||
@@ -20,0 +22,0 @@ var fromSpace, toSpace; |
45
luv.js
@@ -6,2 +6,6 @@ var xyz = require('./xyz'); | ||
name: 'luv', | ||
//NOTE: luv has no rigidly defined limits | ||
//easyrgb fails to get proper coords | ||
//boronine states no rigid limits | ||
//colorMine refers this ones: | ||
min: [0,-134,-140], | ||
@@ -14,3 +18,6 @@ max: [100,224,122], | ||
var _u, _v, l, u, v, x, y, z, xn, yn, zn, un, vn; | ||
l = arg[0], u = arg[1], v = arg[2]; | ||
if (l === 0) return [0,0,0]; | ||
//get constants | ||
@@ -24,30 +31,26 @@ var e = 0.008856451679035631; //(6/29)^3 | ||
xn = xyz.observer[o][i][0]; | ||
yn = xyz.observer[o][i][1]; | ||
zn = xyz.observer[o][i][2]; | ||
xn = xyz.whitepoint[o][i][0]; | ||
yn = xyz.whitepoint[o][i][1]; | ||
zn = xyz.whitepoint[o][i][2]; | ||
un = (4 * xn) / (xn + (15 * yn) + (3 * zn)); | ||
vn = (9 * yn) / (xn + (15 * yn) + (3 * zn)); | ||
// un = 0.19783000664283; | ||
// vn = 0.46831999493879; | ||
l = arg[0], u = arg[1], v = arg[2]; | ||
_u = u / (13 * l) + un || 0; | ||
_v = v / (13 * l) + vn || 0; | ||
y = l > 8 ? yn * Math.pow( (l + 16) / 116 , 3) : yn * l * k; | ||
//wikipedia method | ||
y = l > 8 ? yn * Math.pow( (l + 16) / 116 , 3) : yn * l * k; | ||
x = y * 9 * _u / (4 * _v) || 0; | ||
z = y * (12 - 3 * _u - 20 * _v) / (4 * _v) || 0; | ||
//lindbloom method (needs checking) | ||
// var a = (52 * l / (u + 13 * l * un) - 1) / 3; | ||
// var b =-5 * y; | ||
// var c = -1/3; | ||
// var d = y * ( 39 * l /(v + 13 * l * vn) - 5); | ||
//boronine method | ||
//https://github.com/boronine/husl/blob/master/husl.coffee#L201 | ||
// x = 0 - (9 * y * _u) / ((_u - 4) * _v - _u * _v); | ||
// z = (9 * y - (15 * _v * y) - (_v * x)) / (3 * _v); | ||
// x = (d-b) / (a-c); | ||
// y = l > k*e ? Math.pow( (l + 16) / 116 , 3) : l/k; | ||
// z = x*a + b; | ||
return [x, y, z]; | ||
@@ -57,4 +60,4 @@ } | ||
//http://www.brucelindbloom.com/index.html?Equations.html | ||
// http://www.brucelindbloom.com/index.html?Equations.html | ||
// https://github.com/boronine/husl/blob/master/husl.coffee | ||
//i - illuminant | ||
@@ -69,9 +72,9 @@ //o - observer | ||
//get illuminant/observer | ||
//get illuminant/observer coords | ||
i = i || 'D65'; | ||
o = o || 2; | ||
xn = xyz.observer[o][i][0]; | ||
yn = xyz.observer[o][i][1]; | ||
zn = xyz.observer[o][i][2]; | ||
xn = xyz.whitepoint[o][i][0]; | ||
yn = xyz.whitepoint[o][i][1]; | ||
zn = xyz.whitepoint[o][i][2]; | ||
@@ -78,0 +81,0 @@ un = (4 * xn) / (xn + (15 * yn) + (3 * zn)); |
{ | ||
"name": "color-space", | ||
"description": "Math & data behind color spaces and color conversions.", | ||
"version": "0.3.3", | ||
"author": "Deema Ywanow <dfcreative@gmail.com>", | ||
"version": "0.4.0", | ||
"author": "Deema Yvanow <dfcreative@gmail.com>", | ||
"repository": { | ||
@@ -43,3 +43,6 @@ "type": "git", | ||
"build-test": "browserify -r assert -r mumath -r query-relative -r ./index.js:../index > test/test.bundle.js" | ||
}, | ||
"dependencies": { | ||
"husl": ">=5.0" | ||
} | ||
} |
# color-space [![Build Status](https://travis-ci.org/dfcreative/color-space.svg?branch=master)](https://travis-ci.org/dfcreative/color-space) | ||
Math and data behind color spaces and color conversions. [Converter & tests](https://cdn.rawgit.com/dfcreative/color-space/master/test/index.html). | ||
Math and data behind color spaces and color conversions. _Color-space_ provides uniform interface to all known color-spaces. [Converter & tests](https://cdn.rawgit.com/dfcreative/color-space/master/test/index.html). | ||
@@ -48,9 +48,8 @@ [![NPM](https://nodei.co/npm/color-space.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/color-space/) | ||
```js | ||
space.name //space name | ||
space.min //channel minimums | ||
space.max //channel maximums | ||
space.channel //channel names | ||
space.alias //alias space names, if any | ||
space.illuminant //space-specific properties | ||
space.observer | ||
*.name //space name | ||
*.min //channel minimums | ||
*.max //channel maximums | ||
*.channel //channel names | ||
*.alias //alias space names, if any | ||
xyz.whitepoint //list of whitepoint references | ||
``` | ||
@@ -70,16 +69,10 @@ | ||
* lchuv ([CIE LCHuv](http://en.wikipedia.org/wiki/CIELUV#Cylindrical_representation)) | ||
* [husl](http://www.boronine.com/husl/), [huslp](http://www.boronine.com/husl/) | ||
To see details `console.log(space)`. | ||
# Contribute | ||
Please fork, add color spaces/convertions. If you add a new space, make sure you have implemented at least to/from _XYZ_ or to/from _RGB_ conversion functions. Don’t forget to add a test-case. | ||
Please fork, add color space & basic conversions (to/from XYZ or RGB), tests. | ||
This is meant to be a basic library that can be used by other libraries to wrap color calculations in some cool way. | ||
<a href="http://unlicense.org/UNLICENSE"><img src="http://upload.wikimedia.org/wikipedia/commons/6/62/PD-icon.svg" width="40"/></a> |
var s = require("../index"); | ||
var assert = require("assert"); | ||
var round = require('mumath').round; | ||
var mult = require('mumath').mult; | ||
var div = require('mumath').div; | ||
var max = require('mumath').max; | ||
var husl = require('husl'); | ||
@@ -14,3 +18,5 @@ | ||
//these two are basic spaces | ||
createSpaceCase('rgb'); | ||
createSpaceCase('xyz'); | ||
@@ -250,3 +256,3 @@ | ||
assert.deepEqual(round(s.xyz.luv([0, 0, 0])), [0, 0, 0]); | ||
assert.deepEqual(round(s.xyz.luv([95, 100, 100])), [100, 3, 9]); | ||
assert.deepEqual(round(s.xyz.luv([95, 100, 100]),.1), [100, 3.5, 8.6]); | ||
assert.deepEqual(round(s.xyz.luv([50, 50, 50])), [76, 13, 5]); | ||
@@ -263,3 +269,2 @@ assert.deepEqual(round(s.xyz.luv([100, 0, 0])), [0, 0, 0]); | ||
assert.deepEqual(round(s.luv.xyz([50, 50, 50])), [21, 18, 2]); | ||
assert.deepEqual(round(s.luv.xyz([100, 0, 0])), [95, 100, 109]); | ||
}); | ||
@@ -287,3 +292,3 @@ }); | ||
describe.skip('husl', function(){ | ||
describe('husl', function(){ | ||
before(function(){ | ||
@@ -293,17 +298,62 @@ createSpaceCase('husl'); | ||
it('to rgb', function(){ | ||
it('_husl: lch → luv ≡ lchuv → luv', function(){ | ||
assert.deepEqual(round(husl._conv.lch.luv([1,20,40]), .0001), round(s.lchuv.luv([1,20,40]), .0001)); | ||
assert.deepEqual(round(husl._conv.lch.luv([21,50,40]), .0001), round(s.lchuv.luv([21,50,40]), .0001)); | ||
assert.deepEqual(round(husl._conv.lch.luv([25,30,43]), .0001), round(s.lchuv.luv([25,30,43]), .0001)); | ||
}); | ||
it('_husl: luv → xyz ≡ luv → xyz ', function(){ | ||
assert.deepEqual(round(mult(husl._conv.luv.xyz([21,50,40]), 100), .0001), round(s.luv.xyz([21,50,40]), .0001)); | ||
assert.deepEqual(round(mult(husl._conv.luv.xyz([1,20,40]), 100), .0001), round(s.luv.xyz([1,20,40]), .0001)); | ||
assert.deepEqual(round(mult(husl._conv.luv.xyz([25,30,43]), 100), .0001), round(s.luv.xyz([25,30,43]), .0001)); | ||
}); | ||
it('to xyz', function(){ | ||
it('_husl: xyz → rgb ≡ xyz → rgb', function(){ | ||
assert.deepEqual( | ||
round( | ||
max(mult(husl._conv.xyz.rgb(div([33,40,50], 100)), 255), 0), .0001 | ||
), | ||
round(s.xyz.rgb([33,40,50]), .0001) | ||
); | ||
assert.deepEqual( | ||
round( | ||
max(mult(husl._conv.xyz.rgb(div([1,20,40], 100)), 255), 0), .0001 | ||
), | ||
round(s.xyz.rgb([1,20,40]), .0001) | ||
); | ||
assert.deepEqual( | ||
round( | ||
max(mult(husl._conv.xyz.rgb(div([25,30,43], 100)), 255), 0), .0001 | ||
), | ||
round(s.xyz.rgb([25,30,43]), .0001) | ||
); | ||
}); | ||
it('to ', function(){ | ||
it('_husl: lch → rgb ≡ lchuv → rgb', function(){ | ||
assert.deepEqual( | ||
max(round(mult(husl._conv.lch.rgb([1,20,40]), 255), .001), 0), | ||
max(round(s.lchuv.rgb([1,20,40]), .001), 0) | ||
); | ||
assert.deepEqual( | ||
max(round(mult(husl._conv.lch.rgb([25,30,43]), 255), .001), 0), | ||
max(round(s.lchuv.rgb([25,30,43]), .001), 0) | ||
); | ||
assert.deepEqual( | ||
max(round(mult(husl._conv.lch.rgb([33,40,50]), 255), .001), 0), | ||
max(round(s.lchuv.rgb([33,40,50]), .001), 0) | ||
); | ||
}); | ||
it('_husl → rgb ≡ husl → rgb', function(){ | ||
assert.deepEqual( | ||
round(mult(husl.toRGB(25, 30, 43), 255)), | ||
round(s.husl.rgb([25, 30, 43])) | ||
); | ||
}); | ||
}); | ||
describe.skip('huslp', function(){ | ||
describe('huslp', function(){ | ||
before(function(){ | ||
@@ -313,13 +363,9 @@ createSpaceCase('huslp'); | ||
it('to rgb', function(){ | ||
it('huslp → rgb', function(){ | ||
}); | ||
it('to xyz', function(){ | ||
it('huslp → xyz', function(){ | ||
}); | ||
it('to ', function(){ | ||
}); | ||
}); | ||
@@ -326,0 +372,0 @@ |
20
xyy.js
@@ -1,5 +0,19 @@ | ||
//TODO | ||
module.exports = { | ||
var rgb = require('./rgb'); | ||
var xyz = module.exports = { | ||
name: 'xyy', | ||
alias: ['ciexyy'] | ||
min: [0,0,0], | ||
max: [96,100,109], | ||
channel: ['x','y','Y'], | ||
alias: ['yxy'], | ||
//TODO: fix this maths so to return 255,255,255 in rgb | ||
xyz: function(arg) { | ||
} | ||
}; | ||
//extend rgb | ||
xyz.xyy = function(arg) { | ||
}; |
17
xyz.js
@@ -12,5 +12,4 @@ var rgb = require('./rgb'); | ||
// http://en.wikipedia.org/wiki/Standard_illuminant | ||
//http://www.easyrgb.com/index.php?X=MATH&H=15#text15 | ||
//Xn, Yn, Zn | ||
observer: { | ||
whitepoint: { | ||
2: { | ||
@@ -24,3 +23,3 @@ //incadescent | ||
//daylight | ||
D65: [95.047, 100, 108.883], | ||
D65: [95.045592705167, 100, 108.9057750759878], | ||
D75: [94.972, 100, 122.638], | ||
@@ -70,5 +69,5 @@ //flourescent | ||
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html | ||
r = (x * 3.2404542) + (y * -1.5371385) + (z * -0.4985314); | ||
g = (x * -0.9692660) + (y * 1.8760108) + (z * 0.0415560); | ||
b = (x * 0.0556434) + (y * -0.2040259) + (z * 1.0572252); | ||
r = (x * 3.240969941904521) + (y * -1.537383177570093) + (z * -0.498610760293); | ||
g = (x * -0.96924363628087) + (y * 1.87596750150772) + (z * 0.041555057407175); | ||
b = (x * 0.055630079696993) + (y * -0.20397695888897) + (z * 1.056971514242878); | ||
@@ -104,7 +103,7 @@ r = r > 0.0031308 ? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055) | ||
var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805); | ||
var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722); | ||
var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505); | ||
var x = (r * 0.41239079926595) + (g * 0.35758433938387) + (b * 0.18048078840183); | ||
var y = (r * 0.21263900587151) + (g * 0.71516867876775) + (b * 0.072192315360733); | ||
var z = (r * 0.019330818715591) + (g * 0.11919477979462) + (b * 0.95053215224966); | ||
return [x * 100, y *100, z * 100]; | ||
}; |
Sorry, the diff of this file is not supported yet
232758
8115
1
23
76
+ Addedhusl@>=5.0
+ Addedhusl@6.0.6(transitive)