rgb2lab.js
3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var expect = require('chai').expect,
dataLab2RGB = require('./dataLab2RGB.json'),
dataRGB2Lab = require('./dataRGB2Lab.json'),
iq = require('../../../dist/iq'),
rgb2lab = iq.conversion.rgb2lab,
lab2rgb = iq.conversion.lab2rgb;
describe('lab2xyz and back', function () {
dataLab2RGB.forEach(item => {
it(`Colors: Lab = ${item.Lab.L}, ${item.Lab.a}, ${item.Lab.b}`, () => {
var xyz = iq.conversion.lab2xyz(item.Lab.L, item.Lab.a, item.Lab.b),
lab = iq.conversion.xyz2lab(xyz.x, xyz.y, xyz.z);
expect(deepRound(lab, 4)).to.be.deep.equal(deepRound(item.Lab, 4));
});
});
});
describe('rgb2xyz and back', function () {
dataRGB2Lab.forEach(item => {
it(`Colors: rgb = ${item.rgb.r}, ${item.rgb.g}, ${item.rgb.b}`, () => {
var xyz = iq.conversion.rgb2xyz(item.rgb.r, item.rgb.g, item.rgb.b),
rgb = iq.conversion.xyz2rgb(xyz.x, xyz.y, xyz.z);
expect(deepRound(rgb, 4)).to.be.deep.equal(deepRound(item.rgb, 4));
});
});
});
describe('xyz2lab and back', function () {
dataLab2RGB.forEach(item => {
const xyz = iq.conversion.lab2xyz(item.Lab.L, item.Lab.a, item.Lab.b);
it(`Colors: xyz = ${xyz.x}, ${xyz.y}, ${xyz.z}`, () => {
var lab = iq.conversion.xyz2lab(xyz.x, xyz.y, xyz.z),
newXyz = iq.conversion.lab2xyz(lab.L, lab.a, lab.b);
expect(deepRound(newXyz, 4)).to.be.deep.equal(deepRound(xyz, 4));
});
});
});
describe('xyz2rgb and back', function () {
dataRGB2Lab.forEach(item => {
const xyz = iq.conversion.rgb2xyz(item.rgb.r, item.rgb.g, item.rgb.b);
it(`Colors: xyz = ${xyz.x}, ${xyz.y}, ${xyz.z}`, () => {
var rgb = iq.conversion.xyz2rgb(xyz.x, xyz.y, xyz.z),
newXyz = iq.conversion.rgb2xyz(rgb.r, rgb.g, rgb.b);
expect(deepRound(newXyz, 4)).to.be.deep.equal(deepRound(xyz, 4));
});
});
});
describe('rgb2lab', function () {
dataRGB2Lab.forEach(item => {
const Lab = item.Lab,
rgb = item.rgb;
it(`Colors: Lab = ${Lab.L}, ${Lab.a}, ${Lab.b}, rgb = ${rgb.r}, ${rgb.g}, ${rgb.b}`, () => {
var labCalculated = rgb2lab(rgb.r, rgb.g, rgb.b);
expect(deepRound(labCalculated, 4)).to.be.deep.equal(deepRound(Lab, 4));
});
});
});
describe('lab2rgb', function () {
dataLab2RGB.forEach(item => {
const Lab = item.Lab,
rgb = item.rgb;
it(`Colors: Lab = ${Lab.L}, ${Lab.a}, ${Lab.b}, rgb = ${rgb.r}, ${rgb.g}, ${rgb.b}`, () => {
var rgbCalculated = lab2rgb(Lab.L, Lab.a, Lab.b);
expect(deepRound(rgbCalculated, 4)).to.be.deep.equal(deepRound(rgb, 4));
});
});
});
function deepRound (obj, digitsAfterPoint) {
Object.keys(obj).forEach(propertyName => {
var n = obj[propertyName],
multiplier = Math.pow(10, digitsAfterPoint);
obj[propertyName] = Math.round(n * multiplier) / multiplier;
})
}
function roundLike (n, likeThisValue) {
var s = likeThisValue.toString(),
pointPosition = s.indexOf('.');
if (pointPosition < 0) {
return Math.round(n);
}
var digitsAfterPoint = s.length - pointPosition - 1,
multiplier = Math.pow(10, digitsAfterPoint);
return Math.round(n * multiplier) / multiplier;
}