Parse raw_rgb

This commit is contained in:
Pieter Sheth-Voss 2015-04-17 10:21:48 -04:00
parent 08e648bbb5
commit 51b7cabd2c
4 changed files with 122 additions and 87 deletions

View File

@ -32,6 +32,11 @@ function parse_fills(t, opts) {
if (y.indexed) fill.bgColor.indexed = parseInt(y.indexed, 10);
if (y.theme) fill.bgColor.theme = parseInt(y.theme, 10);
if (y.tint) fill.bgColor.tint = parseFloat(y.tint);
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
fill.bgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, fill.fgColor.tint || 0);
}
/* Excel uses ARGB strings */
if (y.rgb) fill.bgColor.rgb = y.rgb;//.substring(y.rgb.length - 6);
break;
@ -44,6 +49,11 @@ function parse_fills(t, opts) {
if (!fill.fgColor) fill.fgColor = {};
if (y.theme) fill.fgColor.theme = parseInt(y.theme, 10);
if (y.tint) fill.fgColor.tint = parseFloat(y.tint);
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
fill.fgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, fill.fgColor.tint || 0);
}
/* Excel uses ARGB strings */
if (y.rgb) fill.fgColor.rgb = y.rgb;//.substring(y.rgb.length - 6);
break;
@ -123,6 +133,9 @@ function parse_fonts(t, opts) {
if (!font.color) font.color = {};
if (y.theme) font.color.theme = y.theme;
if (y.tint) font.color.tint = y.tint;
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
font.color.raw_rgb = rgb_tint(themes.themeElements.clrScheme[font.color.theme].rgb, font.color.tint || 0);
}
if (y.rgb) font.color.rgb = y.rgb;
break;
case '<color/>':
@ -188,6 +201,10 @@ function parse_borders(t, opts) {
case '<color':
sub_border.color = {};
if (y.theme) sub_border.color.theme = y.theme;
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
sub_border.color.raw_rgb = rgb_tint(themes.themeElements.clrScheme[sub_border.color.theme].rgb, sub_border.color.tint || 0);
}
if (y.tint) sub_border.color.tint = y.tint;
if (y.rgb) sub_border.color.rgb = y.rgb;
if (y.auto) sub_border.color.auto = y.auto;

View File

@ -44,11 +44,13 @@ function parse_zip(zip, opts) {
strs = [];
if(dir.sst) strs=parse_sst(getzipdata(zip, dir.sst.replace(/^\//,'')), dir.sst, opts);
styles = {};
// parse themes before styles so that we can reliably decode theme/tint into rgb when parsing styles
themes = {};
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,''), true),dir.themes[0], opts);
styles = {};
if(dir.style) styles = parse_sty(getzipdata(zip, dir.style.replace(/^\//,'')),dir.style, opts);
themes = {};
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,''), true),dir.themes[0], opts);
}
var wb = parse_wb(getzipdata(zip, dir.workbooks[0].replace(/^\//,'')), dir.workbooks[0], opts);

159
test.js
View File

@ -577,12 +577,6 @@ function cmparr(x){ for(var i=1;i!=x.length;++i) assert.deepEqual(x[0], x[i]); }
function deepcmp(x,y,k,m,c) {
var s = k.indexOf(".");
m = (m||"") + "|" + (s > -1 ? k.substr(0,s) : k);
console.log("======================================")
console.log(k)
console.log(JSON.stringify(x))
console.log(JSON.stringify(y))
console.log(x[k]);
console.log(y[k])
if(s < 0) return assert[c<0?'notEqual':'equal'](x[k], y[k], m);
return deepcmp(x[k.substr(0,s)],y[k.substr(0,s)],k.substr(s+1),m,c);
}
@ -751,82 +745,85 @@ describe('parse features', function() {
assert.equal(X.utils.sheet_to_csv(ws1),X.utils.sheet_to_csv(ws2));
});
});
});
describe('should correctly handle styles', function() {
var wsxls, wsxlsx, rn, rn2;
before(function() {
wsxls=X.readFile(paths.cssxls, {cellStyles:true,WTF:1}).Sheets.Sheet1;
wsxlsx=X.readFile(paths.cssxlsx, {cellStyles:true,WTF:1}).Sheets.Sheet1;
rn = function(range) {
var r = X.utils.decode_range(range);
var out = [];
for(var R = r.s.r; R <= r.e.r; ++R) for(var C = r.s.c; C <= r.e.c; ++C)
out.push(X.utils.encode_cell({c:C,r:R}));
return out;
};
rn2 = function(r) { return [].concat.apply([], r.split(",").map(rn)); };
});
var ranges = [
'A1:D1,F1:G1', 'A2:D2,F2:G2', /* rows */
'A3:A10', 'B3:B10', 'E1:E10', 'F6:F8', /* cols */
'H1:J4', 'H10' /* blocks */
];
var exp = [
{ patternType: 'darkHorizontal',
fgColor: { theme: 9},
bgColor: { theme: 5 } },
{ patternType: 'darkUp',
fgColor: { theme: 3},
bgColor: { theme: 7} },
{ patternType: 'darkGray',
fgColor: { theme: 3},
bgColor: { theme: 1} },
{ patternType: 'lightGray',
fgColor: { theme: 6},
bgColor: { theme: 2} },
{ patternType: 'lightDown',
fgColor: { theme: 4},
bgColor: { theme: 7} },
{ patternType: 'lightGrid',
fgColor: { theme: 6},
bgColor: { theme: 9} },
{ patternType: 'lightGrid',
fgColor: { theme: 4},
bgColor: { theme: 2} },
{ patternType: 'lightVertical',
fgColor: { theme: 3},
bgColor: { theme: 7} }
];
ranges.forEach(function(rng) {
it('XLS | ' + rng,function(){cmparr(rn2(rng).map(function(x){ return wsxls[x].s; }));});
it('XLSX | ' + rng,function(){cmparr(rn2(rng).map(function(x){ return wsxlsx[x].s; }));});
});
it('different styles', function() {
for(var i = 0; i != ranges.length-1; ++i) {
for(var j = i+1; j != ranges.length; ++j) {
diffsty(wsxlsx, rn2(ranges[i])[0], rn2(ranges[j])[0]);
/* TODO */
//diffsty(wsxls, rn2(ranges[i])[0], rn2(ranges[j])[0]);
}
}
});
it('correct styles', function() {
var stylesxls = ranges.map(function(r) { return rn2(r)[0]; }).map(function(r) { return wsxls[r].s; });
var stylesxlsx = ranges.map(function(r) { return rn2(r)[0]; }).map(function(r) { return wsxlsx[r].s; });
for(var i = 0; i != exp.length; ++i) {
[
"fgColor.theme","fgColor.raw_rgb",
"bgColor.theme","bgColor.raw_rgb",
"patternType"
].forEach(function(k) {
deepcmp(exp[i], stylesxlsx[i].fill, k, i + ":"+k);
/* TODO */
//deepcmp(exp[i], stylesxls[i], k, i + ":"+k);
});
}
});
});
describe('should correctly handle styles', function() {
var wsxls, wsxlsx, rn, rn2;
before(function() {
wsxls=X.readFile(paths.cssxls, {cellStyles:true,WTF:1}).Sheets.Sheet1;
wsxlsx=X.readFile(paths.cssxlsx, {cellStyles:true,WTF:1}).Sheets.Sheet1;
rn = function(range) {
var r = X.utils.decode_range(range);
var out = [];
for(var R = r.s.r; R <= r.e.r; ++R) for(var C = r.s.c; C <= r.e.c; ++C)
out.push(X.utils.encode_cell({c:C,r:R}));
return out;
};
rn2 = function(r) { return [].concat.apply([], r.split(",").map(rn)); };
});
var ranges = [
'A1:D1,F1:G1', 'A2:D2,F2:G2', /* rows */
'A3:A10', 'B3:B10', 'E1:E10', 'F6:F8', /* cols */
'H1:J4', 'H10' /* blocks */
];
var exp = [
{ patternType: 'darkHorizontal',
fgColor: { theme: 9, "tint":-0.249977111117893, raw_rgb: 'B9B9B9' },
bgColor: { theme: 5, "tint":0.3999755851924192, raw_rgb: '909090' } },
{ patternType: 'darkUp',
fgColor: { theme: 3, "tint":-0.249977111117893,raw_rgb: 'B3B3B3' },
bgColor: { theme: 7, "tint":0.3999755851924192,raw_rgb: '606060' } },
{ patternType: 'darkGray',
fgColor: { theme: 3, raw_rgb: 'EEECE1' },
bgColor: { theme: 1, raw_rgb: 'FFFFFF' } },
{ patternType: 'lightGray',
fgColor: { theme: 6, "tint":0.3999755851924192, raw_rgb: 'C3C3C3' },
bgColor: { theme: 2, "tint":-0.499984740745262, raw_rgb: '797979' } },
{ patternType: 'lightDown',
fgColor: { theme: 4, raw_rgb: '3B3B3B' },
bgColor: { theme: 7, raw_rgb: '606060' } },
{ patternType: 'lightGrid',
fgColor: { theme: 6, "tint":-0.249977111117893, raw_rgb: '747474' },
bgColor: { theme: 9, "tint":-0.249977111117893, raw_rgb: 'B9B9B9' } },
{ patternType: 'lightGrid',
fgColor: { theme: 4, raw_rgb: '4F81BD' },
bgColor: { theme: 2, "tint":-0.749992370372631, raw_rgb: '1F497D' } },
{ patternType: 'lightVertical',
fgColor: { theme: 3, "tint":0.3999755851924192, raw_rgb: 'F5F5F5' },
bgColor: { theme: 7, "tint":0.3999755851924192, raw_rgb: 'B3B3B3' } }
];
ranges.forEach(function(rng) {
it('XLS | ' + rng,function(){cmparr(rn2(rng).map(function(x){ return wsxls[x].s; }));});
it('XLSX | ' + rng,function(){cmparr(rn2(rng).map(function(x){ return wsxlsx[x].s; }));});
});
it('different styles', function() {
for(var i = 0; i != ranges.length-1; ++i) {
for(var j = i+1; j != ranges.length; ++j) {
diffsty(wsxlsx, rn2(ranges[i])[0], rn2(ranges[j])[0]);
/* TODO */
//diffsty(wsxls, rn2(ranges[i])[0], rn2(ranges[j])[0]);
}
}
});
it('correct styles', function() {
var stylesxls = ranges.map(function(r) { return rn2(r)[0]; }).map(function(r) { return wsxls[r].s; });
var stylesxlsx = ranges.map(function(r) { return rn2(r)[0]; }).map(function(r) { return wsxlsx[r].s; });
for(var i = 0; i != exp.length; ++i) {
var props = [
"fgColor.theme","fgColor.raw_rgb",
"bgColor.theme","bgColor.raw_rgb",
"patternType"
];
props.forEach(function(k) {
deepcmp(exp[i], stylesxlsx[i].fill, k, i + ":"+k);
});
}
});
});
});
function seq(end, start) {
var s = start || 0;

25
xlsx.js
View File

@ -4668,6 +4668,11 @@ function parse_fills(t, opts) {
if (y.indexed) fill.bgColor.indexed = parseInt(y.indexed, 10);
if (y.theme) fill.bgColor.theme = parseInt(y.theme, 10);
if (y.tint) fill.bgColor.tint = parseFloat(y.tint);
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
fill.bgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.bgColor.theme].rgb, fill.fgColor.tint || 0);
}
/* Excel uses ARGB strings */
if (y.rgb) fill.bgColor.rgb = y.rgb;//.substring(y.rgb.length - 6);
break;
@ -4680,6 +4685,11 @@ function parse_fills(t, opts) {
if (!fill.fgColor) fill.fgColor = {};
if (y.theme) fill.fgColor.theme = parseInt(y.theme, 10);
if (y.tint) fill.fgColor.tint = parseFloat(y.tint);
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
fill.fgColor.raw_rgb = rgb_tint(themes.themeElements.clrScheme[fill.fgColor.theme].rgb, fill.fgColor.tint || 0);
}
/* Excel uses ARGB strings */
if (y.rgb) fill.fgColor.rgb = y.rgb;//.substring(y.rgb.length - 6);
break;
@ -4759,6 +4769,9 @@ function parse_fonts(t, opts) {
if (!font.color) font.color = {};
if (y.theme) font.color.theme = y.theme;
if (y.tint) font.color.tint = y.tint;
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
font.color.raw_rgb = rgb_tint(themes.themeElements.clrScheme[font.color.theme].rgb, font.color.tint || 0);
}
if (y.rgb) font.color.rgb = y.rgb;
break;
case '<color/>':
@ -4824,6 +4837,10 @@ function parse_borders(t, opts) {
case '<color':
sub_border.color = {};
if (y.theme) sub_border.color.theme = y.theme;
if (y.theme && themes.themeElements && themes.themeElements.clrScheme) {
sub_border.color.raw_rgb = rgb_tint(themes.themeElements.clrScheme[sub_border.color.theme].rgb, sub_border.color.tint || 0);
}
if (y.tint) sub_border.color.tint = y.tint;
if (y.rgb) sub_border.color.rgb = y.rgb;
if (y.auto) sub_border.color.auto = y.auto;
@ -11418,11 +11435,13 @@ function parse_zip(zip, opts) {
strs = [];
if(dir.sst) strs=parse_sst(getzipdata(zip, dir.sst.replace(/^\//,'')), dir.sst, opts);
styles = {};
// parse themes before styles so that we can reliably decode theme/tint into rgb when parsing styles
themes = {};
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,''), true),dir.themes[0], opts);
styles = {};
if(dir.style) styles = parse_sty(getzipdata(zip, dir.style.replace(/^\//,'')),dir.style, opts);
themes = {};
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,''), true),dir.themes[0], opts);
}
var wb = parse_wb(getzipdata(zip, dir.workbooks[0].replace(/^\//,'')), dir.workbooks[0], opts);